Utilizing The Action Bar In Android Applications

Overview
 
Some useful links are given below to start programming in Android applications.
  1. Android Programming - Day One
  2. Android Programming - Day Two
  3. How To Display A Dialog Windows In Android
  4. How To Display A Progress Dialog Window In Android
  5. Intent In Android
  6. Return Data Using Intent Object In Android Applications
  7. Passing Data Using An Intent Object In Android Applications 
  8. Fragments In Android Applications
  9. Adding Fragments Dynamically In Android Application
  10. Fragment Life Cycle In Android Application
  11. Interaction Between Two Fragments
  12. Calling In Built Functions in Android Application
  13. Intent Object and Intent Filters in Android Application
  14. Displaying Notifications in Android Applications
  15. User Interface in Android Applications
  16. Orientation, Anchoring, Resizing And Repositioning Of Views In Android Application
  17. Activity Behavior When Screen Orientation Changes in Android Application 
  18. Persevere with State Information during Changes in Configuration 
  19. Detect Orientation Changes and Controlling the Orientation of the Activity  

Introduction

 
In this article, you will learn about action bar, which is used in Android applications. The action bar displays the application icon together with the activity title. On the right side of the action bar, there are action items.
 
Implementation
 
Create a new project by selecting File->New->New Project.
 
 
Now, debug the application by pressing F11 on the Android emulator. Action bar located at the top of the screen will appear, which actually contains the application icon and the application name “ActionBarApp”.
 
 
If you want to hide the action bar, you need to add the line display given below in bold in the AndroidManifest.xml file.
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.  <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.      package="com.example.administrator.actionbarapp" >  
  4.    
  5.      <application  
  6.          android:allowBackup="true"  
  7.          android:icon="@mipmap/ic_launcher"  
  8.          android:label="@string/app_name"  
  9.          android:theme="@style/AppTheme" >  
  10.          <activity  
  11.              android:name=".MainActivity"  
  12.              android:label="@string/app_name"  
  13.              android:theme ="@android:style/Theme.Holo.NoActionBar">//line to hide action bar.  
  14.              <intent-filter>  
  15.                  <action android:name="android.intent.action.MAIN" />  
  16.    
  17.                  <category android:name="android.intent.category.LAUNCHER" />  
  18.              </intent-filter>  
  19.          </activity>  
  20.      </application>  
  21.    
  22.  </manifest>  
Add the code given below in the styles.xml.
  1. <resources>  
  2.    
  3.      <!-- Base application theme. -->  
  4.      <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">  
  5.          <item name="android:windowNoTitle">false</item>  
  6.          <item name="android:windowActionBar">true</item>  
  7.          <!-- Customize your theme here. -->  
  8.      </style>  
  9.    
  10.  </resources>  
Modify the code in the onCreate() method to hide the ActionBar.
  1. protected void onCreate(Bundle savedInstanceState) {  
  2.      //getSupportActionBar().setDisplayHomeAsUpEnabled(false);  
  3.      super.onCreate(savedInstanceState);  
  4.      setContentView(R.layout.activity_main);  
  5.      ActionBar actionBar = getActionBar();  
  6.      actionBar.hide();  
  7.  }  
  8.    
  9.  @Override  
  10.  public boolean onCreateOptionsMenu(Menu menu) {  
  11.      // Inflate the menu; this adds items to the action bar if it is present.  
  12.      getMenuInflater().inflate(R.menu.menu_main, menu);  
  13.      return true;  
  14.  }  
Debug the Application and you will see the result.
 
 
Now, we see how to add action items to the action bar. Action items are the shortcut to some of the commonly performed operations in the Application. A student registration application can have some action items such as “Add New Student”, “Update Student Info”, “Delete Student Info” etc.
 
Modify the noCreateOptionsMenu() method.
  1. @Override  
  2.  public boolean onCreateOptionsMenu(Menu menu) {  
  3.      // Inflate the menu; this adds items to the action bar if it is present.  
  4.      //getMenuInflater().inflate(R.menu.menu_main, menu);  
  5.      super.onCreateOptionsMenu(menu);  
  6.      CreateMenu(menu);  
  7.      return true;  
  8.  }  
Create 2 methods CreateMenu() and MenuChoice() to add more options in the menu.
  1. private void CreateMenu(Menu menu)  
  2.  {  
  3.      MenuItem menu1=menu.add(0,0,0,"Add New Student");  
  4.      {  
  5.          menu1.setIcon(R.drawable.ic_launcher);  
  6.          menu1.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);  
  7.      }  
  8.      MenuItem menu2=menu.add(0, 1, 1, "Update Student Info");  
  9.      {  
  10.          menu2.setIcon(R.drawable.ic_launcher1);  
  11.          menu2.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);  
  12.      }  
  13.      MenuItem menu3=menu.add(0,2,2,"Delete Student Info");  
  14.      {  
  15.          menu2.setIcon(R.drawable.ic_launcher2);  
  16.          menu2.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);  
  17.      }  
  18.  }  
  19.  private boolean MenuChoice(MenuItem opt)  
  20.  {  
  21.      switch(opt.getItemId()){  
  22.          case 0:  
  23.              Toast.makeText(this,"You clicked Ist option",Toast.LENGTH_LONG).show();  
  24.              return true;  
  25.          case 1:  
  26.              Toast.makeText(this,"You clicked 2nd option",Toast.LENGTH_LONG).show();  
  27.              return true;  
  28.          case 2:  
  29.              Toast.makeText(this,"You clicked 3rd option",Toast.LENGTH_LONG).show();  
  30.              return true;  
  31.      }  
  32.      return false;  
  33.  }  
Debug the application by pressing F11 on the Android emulator or the mobile device. You will see the icons on the right side of the action bar and clicking on each menu item will cause the Toast class to display the name of the item selected, as given below.
 
 
If you click on 2nd option, the result will look, as shown below.
 
 
We can also customize the Action Items and application icon.
  1. private void CreateMenu(Menu menu)  
  2.  {  
  3.      MenuItem menu1=menu.add(0,0,0,"Add New Student");  
  4.      {  
  5.          menu1.setIcon(R.drawable.ic_launcher);  
  6.          menu1.setShowAsAction(MenuItem.SHOW_AS_ACTION_WITH_TEXT);  
  7.      }  
  8.      MenuItem menu2=menu.add(0, 1, 1, "Update Student Info");  
  9.      {  
  10.          menu2.setIcon(R.drawable.ic_launcher1);  
  11.          menu2.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);  
  12.      }  
  13.      MenuItem menu3=menu.add(0,2,2,"Delete Student Info");  
  14.      {  
  15.          menu2.setIcon(R.drawable.ic_launcher2);  
  16.          menu2.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);  
  17.      }  
  18.  }  
 
To make the application icon clickable, we will have to add setDisplayHomeAsUpEnabled() method, as shown below.
  1. protected void onCreate(Bundle savedInstanceState) {  
  2.      //getSupportActionBar().setDisplayHomeAsUpEnabled(false);  
  3.      super.onCreate(savedInstanceState);  
  4.      setContentView(R.layout.activity_main);  
  5.      ActionBar actionBar = getActionBar();  
  6.      actionBar.setDisplayHomeAsUpEnabled(true);  
  7.      //actionBar.hide();  
  8.  }  
Explanation
 
The android:theme attributes defined in the AndroidManifest.xml file, enables you to turn off the display of the action bar for the activity. Setting the attribute to “@android:style/Theme.Holo.NoActionBar” hides the action bar. We can do the same thing programmatically during runtime by using the getActionBar() method. If you want to display it, use show() method but if you want to hide it, use hide() method. Here, it is very important that if you use the android:theme attribute to turn off the action bar, calling the getActionBar() method returns a null during the runtime. Hence, it is always good to turn the action bar on/off programmatically, using the ActionBar class.
 
As far as action items are added into action bar, the action bar populates its action items by calling the onCreateOptionsMenu() method of an activity.
  1. @Override  
  2.  public boolean onCreateOptionsMenu(Menu menu) {  
  3.      // Inflate the menu; this adds items to the action bar if it is present.  
  4.      //getMenuInflater().inflate(R.menu.menu_main, menu);  
  5.      super.onCreateOptionsMenu(menu);  
  6.      CreateMenu(menu);  
  7.      return true;  
  8.  }  
In the section given above, we created 2 methods CreateMenu() method to display a list of the menu items.
  1. private void CreateMenu(Menu menu)  
  2.  {  
  3.      MenuItem menu1=menu.add(0,0,0,"Add New Student");  
  4.      {  
  5.          menu1.setIcon(R.drawable.ic_launcher);  
  6.          menu1.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);  
  7.      }  
  8.      MenuItem menu2=menu.add(0, 1, 1, "Update Student Info");  
  9.      {  
  10.          menu2.setIcon(R.drawable.ic_launcher1);  
  11.          menu2.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);  
  12.      }  
  13.      MenuItem menu3=menu.add(0,2,2,"Delete Student Info");  
  14.      {  
  15.          menu2.setIcon(R.drawable.ic_launcher2);  
  16.          menu2.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);  
  17.      }  
  18.  }  
To make the menu item appear as an action item, we call its setShowAsAction() method, using the SHOW_AS_ACTION_IF_ROOM constant. This actually tells the Android device to display the menu item as an action item, if there is room for it.
 
Hence, when a menu item is selected by the user, the onOptionsItemSelected() method is called.
  1. public boolean onOptionsItemSelected(MenuItem item) {  
  2.            
  3.          return MenuChoice(item);  
  4.      }  
In the method given above, MenuChoice() method is self defined. It is used to check which menu item is clicked and display the message.
  1. private boolean MenuChoice(MenuItem opt)  
  2.  {  
  3.      switch(opt.getItemId()){  
  4.          case 0:  
  5.              Toast.makeText(this,"You clicked Ist option",Toast.LENGTH_LONG).show();  
  6.              return true;  
  7.          case 1:  
  8.              Toast.makeText(this,"You clicked 2nd option",Toast.LENGTH_LONG).show();  
  9.              return true;  
  10.          case 2:  
  11.              Toast.makeText(this,"You clicked 3rd option",Toast.LENGTH_LONG).show();  
  12.              return true;  
  13.      }  
  14.      return false;  
  15.  }  

Conclusion

 
In this article, you learned how to utilize the action bar and action items are added into this. In the next article, I will explain about how to create user interface programmatically.