Presentation is loading. Please wait.

Presentation is loading. Please wait.

Android Development (Basics)

Similar presentations


Presentation on theme: "Android Development (Basics)"— Presentation transcript:

1 Android Development (Basics)
Sina ABedi

2 Getting Started Go to and download the latest version of Android Studio Once installed, open Android Studio and click “File -> New Project” Pick a name for your app and pick any domain name for now. You can use uwaterloo.ca Only check “Phone and Tablet”, set API to API 15 and click next Click on “Blank Activity” and click next Click “Finish”

3 First Activity You will now have 2 files open:
MainActivity.java activity_main.xml These files are responsible for the main page in your activity. Right now, you only have one page. MainActivity.java is responsible for all of the different functions that particular page performs. activity_main.xml is responsible for the layout of that page and how it looks on the phone.

4 MainActivity.java You will see some imports and a package name at the top There should be three methods inside this java file: void onCreate(Bundle savedInstanceState) boolean onCreateOptionsMenu(Menu menu) boolean onOptionsItemSelected(MenuItem item)

5 Methods The onCreate() of an activity gets called on the start of that particular activity The onOptionsItemSelected() deals with the different options available in the action bar The onCreateOptionsMenu() gets called when the action bar is first created. This is the Action Bar: Image Source:

6 Java vs. C# Java has no properties. Instead of properties, Java uses methods to access or change data bool should be boolean in Java In Java, you must declare string as String (Capital S) instead int.Parse or the likes will not work in Java. Must use Integer.parseInt(value) or Double.parseDouble(value) etc. foreach(string s in array) must be written as: for(string s : array) “import” is the equivalent of “using” in C# “final” in Java is the equivalent of “const” in C# To call the base constructor in a child class, you need to use “super(parameters)” instead of “base(parameters)” For a more comprehensive list of differences, check out Google or StackOverFlow

7 Android Manifest You will see AndroidManifest.xml under manifests
The Android Manifest file is responsible for all of the permissions your app uses (like internet access), name of the app, icon of the app, all of the activities that the app contains, etc. Every time you create a new activity, the manifest gets updated by the IDE to include the new activity In the example shown, there are two activities, the name of the app is “beepo” and MainActivity is the main activity(starts when the app is launched)

8 Button Click Listeners
Let’s try making a very basic app that displays a word that the user enters on the screen after a button is pressed First we must make the button inside the xml file for MainActivity Open activity_main.xml. In the “Design” tab, you will see multiple widgets and layouts that you can implement Drag “Button” and put it inside the phone display Now for the user input, drag “Plain Text” from the Text Fields Now go to the “Text” tab and look at android:id for the button and the EditText since we will need these.

9 Declare two global variables
Declare two global variables. One of type EditText and one of type Button (If you get an error for these, press alt + enter on them to import these classes. I called these variables e1 and b1 respectively) You must now instantiate these variables inside the onCreate method with the following code: e1 = (EditText)findViewById(R.id.editText); Remember those ids in the previous page? We use them here.(R.id.whateverIdYouhaveinXML) b1 = (Button)findViewById(R.id.button); Now Android knows what e1 and b1 are. Now we must do b1.setOnClickListener(new OnClickListener). Android Studio will automatically fill this for you if you enter new OnClickListener. This will create an onClick method inside of the setOnClickListener for you. onClick is where all the action happens. Let’s get the text that the user stored by declaring userInput inside the onClick method: String userInput = e1.getText().toString(); getText() will get whatever was in the editText and toString() will convert it to a string for you to use.

10 To use a toast, we must create it and call the show() method on it:
Now that we have onClick method, we need to display userInput onto the phone. This is done by using something called a Toast. To use a toast, we must create it and call the show() method on it: Toast.makeText(MainActivity.this, userInput, Toast.LENGTH_LONG).show(); Toast.LENGTH_LONG is just an integer that says to your phone how long the Toast should be visible The first parameter is the context in which the Toast shows up in and this context has to be the MainActivity. To signify the context of MainActivity, we use MainActivity.this. Your onCreate should look like this: Try Googling and figuring out how to use the different widgets now (Look at CheckBox, ProgressBar, ImageView and Toggle Switch)


Download ppt "Android Development (Basics)"

Similar presentations


Ads by Google