Android Development (Basics) SINA ABEDI. Getting Started Go to and download the latest version of...

10
Android Development (Basics) SINA ABEDI

Transcript of Android Development (Basics) SINA ABEDI. Getting Started Go to and download the latest version of...

Page 1: Android Development (Basics) SINA ABEDI. Getting Started  Go to  and download the latest version of Android.

Android Development(Basics)SINA ABEDI

Page 2: Android Development (Basics) SINA ABEDI. Getting Started  Go to  and download the latest version of Android.

Getting StartedGo to http://developer.android.com/sdk/index.html 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”

Page 3: Android Development (Basics) SINA ABEDI. Getting Started  Go to  and download the latest version of Android.

First ActivityYou will now have 2 files open:

1. MainActivity.java2. 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.

Page 4: Android Development (Basics) SINA ABEDI. Getting Started  Go to  and download the latest version of Android.

MainActivity.java

You will see some imports and a package name at the top

There should be three methods inside this java file:1. void onCreate(Bundle savedInstanceState)2. boolean onCreateOptionsMenu(Menu menu)3. boolean onOptionsItemSelected(MenuItem item)

Page 5: Android Development (Basics) SINA ABEDI. Getting Started  Go to  and download the latest version of Android.

MethodsThe 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: www.androidhive.info

Page 6: Android Development (Basics) SINA ABEDI. Getting Started  Go to  and download the latest version of Android.

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

Page 7: Android Development (Basics) SINA ABEDI. Getting Started  Go to  and download the latest version of Android.

Android ManifestYou 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)

Page 8: Android Development (Basics) SINA ABEDI. Getting Started  Go to  and download the latest version of Android.

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 MainActivityOpen 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.

Page 9: Android Development (Basics) SINA ABEDI. Getting Started  Go to  and download the latest version of Android.

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.

Page 10: Android Development (Basics) SINA ABEDI. Getting Started  Go to  and download the latest version of Android.

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)