Hello Button
- This example uses more of the Android features: activity stack, menu.
- Create a new project named Hello Button.
- Change the activity_main.xml to contain
this XML code for the app bar and a button.
- Add style support for the app bar
- A new file themes.xml in
res/values by right clicking on values then
"New→Values resource file".
- A new file styles.xml in
res/values by right clicking on values then
"New→Values resource file".
- New colors.xml
- Provide handler for the button click, in
MainActivity.java
- Add an activity called HelloButtonWorld to this application.
- Add items for the menu
- Make a new directory in res called menu
- Put this menu XML in
res/menu/main_menu.xml
- Add code for creating and reacting to the menu in
MainActivity.java
- To allow the switch statement to work, add android.nonFinalResIds=false
to the end of the
gradle.properties
- Edit res/values/strings.xml to contain
the strings needed
- Add permission to use the vibrator to the
AndroidManifest.xml
- Run the application
- Clicking the button will start the HelloWorld activity, akin to a function call (but
not the same).
- The phone's Back button returns from the activity to the previous one.
- Try the menu
- An alternative to adding the HelloWorld activity to this application would be to
call the HelloWorld activity in the existing Hello World application.
- Change the click handler code to reference it.
- Imagine (or test) what happens then if the Hello World
application is not installed on the phone!
- Change the icon