Monday, February 12, 2018

Android Action Bar Example

The action bar is a dedicated place at the top of each screen that is generally persistent throughout the app.

Android action bar was introduced to maintain a consistent navigation across the application. It has the powerful capabilities like adapting to screen configurations (landscape & portrait), prioritizing important actions, adding widgets to action bar (search, sharing etc.), providing navigation between screens (drop-down & tabbed navigation) and much more.


Actions Bar provides several key functions:
  • Makes important actions prominent and accessible in a predictable way (such as New or Search).
  • Supports consistent navigation and view switching within apps.
  • Reduces clutter by providing an action overflow for rarely used actions.
  • Provides a dedicated space for giving your app an identity

In this post we will see how to work with 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.



Create my_menu.xml in menu folder


<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" >

   <item
        android:id="@+id/rateApp"
        android:title="Rate App"/>
    
     <item
        android:id="@+id/shareApp"
        android:title="Share App"/>
     
     <item
        android:id="@+id/policy"
        android:title="Privacy Policy"/>
</menu>



How to handle click event on Actionbar Items



@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
 
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // toggle nav drawer on selecting action bar app icon/title
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
     
      switch (item.getItemId()) 
      {
      
     

      case R.id.shareApp :
     
         // Your code
       
           return true;
           
     
     
           case R.id.rateApp :

          // Your code
        
            return true;           

           case R.id.policy :

             // Your code
        
            return true;


default:
            return super.onOptionsItemSelected(item);  

    }
}
 

No comments:

Post a Comment