Saturday, 18 July 2015

Lesson 2- Action Bars

1) Action bars can be used for easy access of controls.
   (Though it does take up some screen space, I'm not sure how I feel about that. Maybe they could add a swipe feature to pull the action bar down)

2) It's only available from 3.0 or API 11. So update your min sdk to 11 if you want to use it. If you want to use sdk7, you'll have to use the support libraries and use a theme.AppCompat library.

3) You can add action buttons to the action bar by adding <item> to res/menu/main.xml

    ShowasAction is an attribute that determines whether to show the button or not.
   Eg. Ifroom means that, if there's room on the actionbar then display it.

4) You can add the items to the action bar in the onCreateOptionsMenu(Menu menu) method.

5) To specify the action,  use a switch case in onOptionsItemSelected(Menuitem item) to match the clicked buttons id with the ids present and implement a method once the case is found.

6) Instead of creating a button and intents to return back to the home screen, you can define the parent activity in you manifest file. Under your child activity, specify parentActivityName to your parent activity. Then to enable the up button, go to your child class java file and add getActionBar().setDisplayHomeAsUpEnable(true); This is a clean and easy way to return back to your parent activity.

7) You can use a theme by using application android:theme. You have a bunch of predefined themes.

Holo
Holo light etc

Use an appcompat theme if you're on sdk 7


8) To style the action bar, create an XML file, and give your style a name. Reference this name from the theme tag in your manifest file.

Set the parent to the theme you want your application to use. From there, you can specify item such as actionbarstyle and give it a name.

Define the name as a separate tag, giving it the name, parent theme, and item tags such as background.

9) If you want to overlay your action bar just set windowActionBarOverlay to true

 
 

No comments:

Post a Comment