This example will show you how to add a Spinner ( Drop Down ) menu list in android action bar.
1. Android ActionBar Spinner Menu List Example.
This example include one spinner style menu list in the ActionBar. There are three menu items.
- News menu item: Click this menu item will show a Toast prompt.
- Sports menu item: Click this menu item will show an AlertDialog.
- FinTech menu item: Click this menu item will open another activity.
If you can not watch the above video, you can see it on the youtube URL https://youtu.be/EtL0iDEpJQs
There are two activity classes.
- ActionBarSpinnerActivity : This activity will show the spinner menu item drop down list.
- ActionBarSpinnerTargetActivity : This activity will be displayed when user click the third FinTech menu item.
2. ActionBarSpinnerActivity Java File.
ActionBarSpinnerActivity.java
package com.dev2qa.example; import android.content.Intent; import android.database.DataSetObserver; import android.graphics.Color; import android.os.Bundle; import android.support.v7.app.ActionBar; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.SpinnerAdapter; import android.widget.TextView; import android.widget.Toast; public class ActionBarSpinnerActivity extends AppCompatActivity { // The drop down menu list text. private String dropDownItemArr[] = {"News", "Sports", "FinTech"}; // The drop down menu list item icon. private int dropDownImageIdArr[] = {R.drawable.icon_news_32, R.drawable.icon_sports_32, R.drawable.icon_finance_32}; private ActionBar actionBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_action_bar_spinner); // Get ActionBar actionBar = getSupportActionBar(); // Set below attributes to add logo in ActionBar. actionBar.setDisplayShowHomeEnabled(true); actionBar.setDisplayUseLogoEnabled(true); actionBar.setLogo(R.drawable.icon_tech_32); actionBar.setTitle("dev2qa.com -> "); // SpinnerAdapter is used to calculate data and view that will be shown in drop down menu list. SpinnerAdapter spinnerAdapter = new SpinnerAdapter() { // This method return the View object for dropdown list item. // The return view contains one ImageView and one TextView. @Override public View getDropDownView(int itemIndex, View view, ViewGroup viewGroup) { // Create a LinearLayout view object. LinearLayout linearLayout = new LinearLayout(ActionBarSpinnerActivity.this); linearLayout.setOrientation(LinearLayout.HORIZONTAL); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER); linearLayout.setLayoutParams(layoutParams); // Create ImageView and set image source by item index. ImageView itemImageView = new ImageView(ActionBarSpinnerActivity.this); int imageViewResourceId = dropDownImageIdArr[itemIndex]; itemImageView.setImageResource(imageViewResourceId); // Add ImageView in return view. linearLayout.addView(itemImageView, 0); // Create TextView and set it's text and color. TextView itemTextView = new TextView(ActionBarSpinnerActivity.this); String itemText = dropDownItemArr[itemIndex]; itemTextView.setText(itemText); itemTextView.setTextSize(20); itemTextView.setBackgroundColor(Color.BLUE); itemTextView.setTextColor(Color.GREEN); // Add TextView in return view. linearLayout.addView(itemTextView, 1); linearLayout.setBackgroundColor(Color.BLUE); return linearLayout; } @Override public void registerDataSetObserver(DataSetObserver dataSetObserver) { } @Override public void unregisterDataSetObserver(DataSetObserver dataSetObserver) { } @Override public int getCount() { // Return drop down menu item count. return dropDownItemArr.length; } @Override public Object getItem(int itemIndex) { // Return drop down menu item text. return dropDownItemArr[itemIndex]; } @Override public long getItemId(int itemIndex) { return itemIndex; } @Override public boolean hasStableIds() { return false; } // This method return the View object for drop down input box. // It only return TextView object. @Override public View getView(int itemIndex, View view, ViewGroup viewGroup) { TextView itemTextView = new TextView(ActionBarSpinnerActivity.this); String itemText = dropDownItemArr[itemIndex]; itemTextView.setText(itemText); itemTextView.setTextSize(20); itemTextView.setBackgroundColor(Color.BLUE); itemTextView.setTextColor(Color.GREEN); return itemTextView; } @Override public int getItemViewType(int i) { return 0; } @Override public int getViewTypeCount() { // This method must return 1, otherwise java.lang.IllegalArgumentException will be thrown. return 1; } @Override public boolean isEmpty() { return false; } }; // Set action bar navigation mode to list mode. actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); // Set action bar list navigation data and item click listener. actionBar.setListNavigationCallbacks(spinnerAdapter, new ActionBar.OnNavigationListener() { @Override public boolean onNavigationItemSelected(int itemPosition, long itemId) { String menuItemText = dropDownItemArr[itemPosition]; String message = "You click menu " + menuItemText; switch (itemPosition) { case 0: // Click News menu item. Toast.makeText(ActionBarSpinnerActivity.this, message, Toast.LENGTH_SHORT).show(); break; case 1: // Click Sports menu item. AlertDialog alertDialog = new AlertDialog.Builder(ActionBarSpinnerActivity.this).create(); alertDialog.setMessage(message); alertDialog.show(); break; case 2: // Click FinTech menu item will show another activity. Intent intent = new Intent(ActionBarSpinnerActivity.this, ActionBarSpinnerTargetActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); break; default: break; } return true; } }); } }
3. ActionBarSpinnerActivity Layout Xml File.
activity_action_bar_spinner.xml
Do not has any view object in this layout xml file.
4. ActionBarSpinnerTargetActivity Java File.
ActionBarSpinnerTargetActivity.java
package com.dev2qa.example; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class ActionBarSpinnerTargetActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_action_bar_spinner_target); setTitle("Spinner menu item target activity."); Button backToPreviousButton = (Button)findViewById(R.id.backToPreviousButton); backToPreviousButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // Close current activity and let previous activity displayed. finish(); } }); } }
5. ActionBarSpinnerTargetActivity Layout Xml File.
activity_action_bar_spinner_target.xml
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This screen will be shown when you click FinTech menu item in previous activity's spinner action bar." android:textSize="20dp"/> <Button android:id="@+id/backToPreviousButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Back To Previous"/> </LinearLayout>
Some of the methods have been deprecated, is there another way to do this?