Menu items are showing on action BAR
Android action bar was used to maintain a consistent navigation across the application. Menu list button show on action bar.
Screen shot
Android - Navigation
Overview of Action Bar
Action bar mainly contains four functional areas. They are app icon, view control, action buttons and action overflow.
App Icon – App branding logo or icon will be displayed here.
View Control – A dedicated space to display app title. Also provides option to switch between views by adding spinner or tabbed navigation.
Action Buttons – Some important actions of the app can be added here.
Action Overflow – All unimportant action will be shown as a menu.
App Icon – App branding logo or icon will be displayed here.
View Control – A dedicated space to display app title. Also provides option to switch between views by adding spinner or tabbed navigation.
Action Buttons – Some important actions of the app can be added here.
Action Overflow – All unimportant action will be shown as a menu.
Here the important xml attributes should be known are
android:icon – Defines the icon of the action item.
android:title – Title for the icon.
android:showAsAction – Defines the visibility of the action item. It accepts following values.
android:title – Title for the icon.
android:showAsAction – Defines the visibility of the action item. It accepts following values.
ifRoom | Displays the icon if there is space available on the screen |
never | Never places this icon on the action bar |
always | Forces to display the icon always irrespective of space available. This way is not suggested. |
withText | Displays a text along with the icon. Normally the text value defined by android:title will be displayed |
collapseActionView | Defines the action layout associated with it. This action view defined usingandroid:actionLayout or android:actionViewClass |
Example
Add menu title set as res/values/strings.xml
<resources>
....
<string name="entries">ENTRIES</string>
<string name="entrieslist">ENTRIES LIST</string>
<string name="salesreport">SALES REPORT</string>
<string name="logout">LOGOUT</string>
</resources>
Create a new xml file under res ⇒ menu named main_actions.xml and add the following code.
Here each <item> indicates each action item.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/entries"
android:title="@string/entries"
app:showAsAction="never"
/>
<item
android:id="@+id/entrieslist"
android:title="@string/entrieslist"
app:showAsAction="never"
/>
<item
android:id="@+id/salesreport"
android:title="@string/salesreport"
app:showAsAction="never"
/>
<item
android:id="@+id/logout"
android:title="@string/logout"
app:showAsAction="never"
/>
</menu>
Now open your main activity class and do the following in onCreateOptionsMenu() method.
public class RptsalesreportActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rptsalesreport);
...............
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_actions, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.entries:
// code here
return true;
case R.id.entrieslist:
//code here
return true;
case R.id.salesreport:
//code here
return true;
case R.id.logout:
//code here
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Handling Action Bar Icon Click Events
Open your main activity and override onOptionsItemSelected() method. This method accepts menu item as a parameter. Selected action item can be identified by using it’s id.
Related Links
Android Programming Tips
Event Handling in Android
Transfer Data Between Activities Using IntentAndroid Programming Tips
No comments:
Post a Comment