Category Archives: Custom Layout in ActionBar

Custom Layout in ActionBarSherlock


Today, we are going to create a IcsSpinner withing the ActionBarSherlock in Android. The snapshot is given below:
device-2013-02-05-141413

First of all, in res > menu > create menu_configuration_activity with following codes

1:  <?xml version="1.0" encoding="utf-8"?>  
2:  <menu xmlns:android="http://schemas.android.com/apk/res/android" >  
3:    <item  
4:      android:id="@+id/menuConfigLanguageSelector"  
5:      android:actionLayout="@layout/layout_config_menu"  
6:      android:icon="@drawable/ic_setting"  
7:      android:showAsAction="always"  
8:      android:title="language"/>  
9:    <item  
10:      android:id="@+id/menuConfigDone"  
11:      android:icon="@drawable/ic_done"  
12:      android:showAsAction="always"  
13:      android:title="OK"/>  
14:  </menu>  

Here, in line number 5, we have assigned a custom layout. This custom layout is created in res > layout > layout_config_menu with following contents:

1:  <?xml version="1.0" encoding="utf-8"?>  
2:  <com.actionbarsherlock.internal.widget.IcsSpinner xmlns:android="http://schemas.android.com/apk/res/android"  
3:    android:id="@+id/languageSelectSpinner"  
4:    android:layout_width="wrap_content"  
5:    android:layout_height="wrap_content" />  

Then in the activity, where you want to place this actionbar, do as given below:

1:  @Override  
2:       public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {  
3:            getSupportMenuInflater().inflate(R.menu.menu_configuration_activity,  
4:                      menu);  
5:            MenuItem itemLanguageSelector = menu  
6:                      .findItem(R.id.menuConfigLanguageSelector);  
7:            IcsSpinner icsSpinner = (IcsSpinner) itemLanguageSelector  
8:                      .getActionView();  
9:          Context subContext = getSupportActionBar().getThemedContext();  
10:            ArrayAdapter<CharSequence> list = ArrayAdapter.createFromResource(  
11:                      subContext, R.array.language_selection,  
12:                      com.actionbarsherlock.R.layout.sherlock_spinner_item);  
13:            list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);  
14:            icsSpinner.setAdapter(list);  
15:            icsSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {  
16:                 @Override  
17:                 public void onItemSelected(IcsAdapterView<?> parent, View view,  
18:                           int position, long id) {  
19:                      //do something with the selected item  
20:                 }  
21:                 @Override  
22:                 public void onNothingSelected(IcsAdapterView<?> parent) {  
23:                      Log.i("Test click", "Inside on nothing selected");  
24:                 }  
25:            });  
26:            return super.onCreateOptionsMenu(menu);  
27:       };  
28:       @Override  
29:       public boolean onOptionsItemSelected(MenuItem item) {  
30:            if (item.getTitle().toString().equals("OK")) {  
31:                 if (selectedCategory != null && sourceAndLanguage != null) {  
32:                      try {  
33:                           // initAppWidget();  
34:                           saveTheUserValueInPref(selectedCategory, sourceAndLanguage);  
35:                           showAppWidget();  
36:                      } catch (Exception e) { // TODO: handle exception }  
37:                      }  
38:                 }  
39:            }  
40:            return super.onOptionsItemSelected(item);  
41:       }  

Dont forget to extend sherlockactivity in your activity to implement actionbarsherlock
Now, your IcsSpinner is ready to be used in ActionBarSherlock 😀
Happy Coding 😀

Advertisements