Android: Session One: Part1: Configuring Eclipse
Android: Session One: Part1: Configuring Eclipse
GITM-MASTERS-P2013-VERDIER
If you are not familiar with the procedure, follow the instructions in the link below: http://developer.android.com/sdk/index.html
PAGE 1 OF 14!
ANDROID: TP1 !
GITM-MASTERS-P2013-VERDIER
PAGE 2 OF 14!
ANDROID: TP1 !
GITM-MASTERS-P2013-VERDIER
PAGE 3 OF 14!
ANDROID: SESSION-1 !
GITM-MASTERS-P2013-VERDIER
Part2: HelloClick
We are going to make a simple HelloWorld application that can also count how many times you click on a button (such an amazing functionality).
For the other screens you can leave the default values and just click Next until you reach the Finish button.
PAGE 4 OF 14!
ANDROID: SESSION-1 !
GITM-MASTERS-P2013-VERDIER
PAGE 5 OF 14!
ANDROID: SESSION-1 !
GITM-MASTERS-P2013-VERDIER
We are going to customize the labels that are displayed. Switch from graphical editor to the XML editor. As you can see, your activity_main.xml does not uses a static string, but instead a link to a string resource.
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" tools:context=".MainActivity" />
That is because Android uses XML resource les for internationalization, thats why you should never use static string in your activity_main.xml, but instead reference a string resource from the res/ values/string.xml le. Lets change the application name to MyHelloWorld and the greeting message to Hello Word, how are you doing ?.
PAGE 6 OF 14!
ANDROID: SESSION-1 !
GITM-MASTERS-P2013-VERDIER
Dont forget to create the corresponding string resources in the string.xml le.
PAGE 7 OF 14!
ANDROID: SESSION-1 !
GITM-MASTERS-P2013-VERDIER
/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { ! super.onCreate(savedInstanceState); ! setContentView(R.layout.main); ! ! } incrementButton = (Button) findViewById(R.id.buttonIncrement); incrementButton.setOnClickListener(this);
To be sure we get the event, we want to display a message on the console. Use the Log.d method to display a debug message. It takes 2 strings: the rst one is a tag you can lter afterward, and the second one is the message you want to log. You can display all the message from your application with the LogCat utility [Window->Show view>other...->Android->LogCat]
PAGE 8 OF 14!
ANDROID: SESSION-1 !
GITM-MASTERS-P2013-VERDIER
Now here is your todo list to get the application to do its work:
declare the clearButton and the countTextView get the button and textView view from their id set the clearButton ClickListener make the incrementButton increment the counter make the clearButton reset the counter
PAGE 9 OF 14!
ANDROID: SESSION-1 !
GITM-MASTERS-P2013-VERDIER
2.
(you can use RGB value directly in the XML; for example #FFF is white)
Finally, using the Toast.makeText() method, display notication when the user reset the counter.
PAGE 10 OF 14!
ANDROID: SESSION-1 !
GITM-MASTERS-P2013-VERDIER
PAGE 11 OF 14!
ANDROID: SESSION-1 !
GITM-MASTERS-P2013-VERDIER
Resource le The resource le for the menu is already part of the template so we dont need to add. Just remember that if you have to add a menu to another activity, youll have to add an android XML resource to your project. [File!New!Other ...!Android XML File]. Select the menu for the resource type. Display the activity_main.xml menu le located in res/menu folder. Than add a new item by click on Add ... then select Item. You have to set the following parameters:
Id: so you can reference your menu item and access it afterwards Title: the title of your menu item (create a new string in your strings.xml and reference it) Icon: the icon to display for your menu item (use a reference to an existing image resource)
Menu creation Your menu is already loaded in your MainActivity in the onCreateOptionsMenu method. This method is called the rst time the menu button is pressed by the user.
PAGE 12 OF 14!
ANDROID: SESSION-1 !
GITM-MASTERS-P2013-VERDIER
Event handling Finally, you have to handle the event that is red when the user clicks on the menu item by overriding another method. Use the onOptionsItemSelected method to clear the counter and display a toast message to the user.
@Override public boolean onOptionsItemSelected(MenuItem item) { ! switch (item.getItemId()) ! { ! ! case R.id.menu_reset: ! ! ! //TODO: clear the counter and display a toast ! ! ! break; ! ! default: ! ! ! break; ! } ! return true; }
Reverse mode Remove the Settings menu item and add a new Reverse ! menu item to switch between increase and decrease when you click on the click me button. You can use the @android:drawable/ ic_menu_revert icon.
PAGE 13 OF 14!
ANDROID: SESSION-1 !
GITM-MASTERS-P2013-VERDIER
Part4: Granularity
We want to be able to choose how much we add or subtract to the counter. Add 2 buttons, the Normal has to set the step to one and the Big has to set the step to ten.
PAGE 14 OF 14!