How to Create Drop Down List in Android?

«
»
Here is source code of the Program to Demonstrate Drop Down Navigaton in Android using Java. The program is successfully compiled and run on a Windows system using Eclipse Ide. The program output is also shown below.

Main Activity

package com.example.drop_down_navigation;
 
import android.app.ActionBar;
import android.os.Bundle;
 
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NavUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
 
public class MainActivity extends FragmentActivity implements
    ActionBar.OnNavigationListener {
 
    private static final String item_selected = "navigation_item";
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // setting the action bar foe a drop down naviagtion
 
        final ActionBar actionBar = getActionBar();
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
 
        actionBar.setListNavigationCallbacks(
            new ArrayAdapter<String>(actionBar.getThemedContext(),
            android.R.layout.simple_list_item_1,
            android.R.id.text1, new String[] {
                getString(R.string.title_section1),
                getString(R.string.title_section2),
                getString(R.string.title_section3), }), this);
	}
 
     @Override
     public void onRestoreInstanceState(Bundle savedInstanceState) {
            // restoring the previous drop navigations
         if (savedInstanceState.containsKey(item_selected)) {
             getActionBar().setSelectedNavigationItem( savedInstanceState.getInt(item_selected));
	 }
     }
 
     @Override
     public void onSaveInstanceState(Bundle outState) {
        outState.putInt(item_selected, getActionBar().getSelectedNavigationIndex());
     }
 
     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
         getMenuInflater().inflate(R.menu.main, menu);
         return true;
     }
 
     @Override
     public boolean onNavigationItemSelected(int position, long id) {
         Fragment fragment = new myFragement();
         Bundle args = new Bundle();
         args.putInt(myFragement.string, position + 1);
         fragment.setArguments(args);
         getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
         return true;
    }
 
    public static class myFragement extends Fragment {
        public static final String string = "number";
        public myFragement() {}
 
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View fragement_view = inflater.inflate(
            R.layout.layout_fragement, container, false);
            TextView text_ = (TextView) fragement_view.findViewById(R.id.section_label);
            text_.setText(Integer.toString(getArguments().getInt(string)));
            return fragement_view;
        }
   }
 
}

layout_fragement

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com    ools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity$DummySectionFragment" >
 
    <TextView
        android:id="@+id/section_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
 
</RelativeLayout>

Activity_Main

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com    ools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:ignore="MergeRootFrame" />

Screenshot_2013-11-16-19-53-21

Sanfoundry Global Education & Learning Series – 100+ Java Android Tutorials.

If you wish to look at all Tutorials, go to Java Android Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.