Android Spinner in Toolbar

Source: Internet
Author: User

As the title of the The The post suggest in this tutorial we'll see what spinner widgets inside the toolbar in the Previou S series of tutorial we have seen many example on how to set up the Android spinner Widget and also we have seen How to has Android Material Design toolbar , in this tutorial we'll see the combination of the both.

String Constant

Lets has Array of string constant which'll be set to spinner

File:string.xml

<Resources><stringname= "App_name">Spinnertoolbar</string><stringname= "Action_settings">Settings</string><!--Category -<String-arrayname= "category"><Item>Top News</Item><Item>Business</Item><Item>Politics</Item><Item>Sports</Item><Item>Technology</Item><Item>Movies</Item></String-array></Resources>
XML Layout

Create a Layout/activity_main.xml file which contains Toolbar widget.

File:activity_main.xml

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Tools:context=". Mainactivity "><Android.support.v7.widget.ToolbarAndroid:id= "@+id/toolbar"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:background= "? Attr/colorprimary"Android:minheight= "? Attr/actionbarsize" /></LinearLayout>

Create a spinner_dropdown_item.xml which contains TextView which represents view for each items of spinner.

File:spinner_dropdown_item.xml

<?XML version= "1.0" encoding= "Utf-8"?><TextViewxmlns:android= "Http://schemas.android.com/apk/res/android"Android:id= "@android: Id/text1"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:background= "@color/colorprimary"android:gravity= "Center_vertical"Android:minheight= "? Android:attr/listpreferreditemheightsmall"Android:paddingleft= "12DP"Android:paddingright= "12DP"android:textappearance= "? Android:attr/textappearancelistitemsmall" />

Mainactivity

Create a class mainactivity which extends Appcompatactivity, now inside OnCreate () Add spinner to toolbar dyanamically an D also set Onitemselectlistener.

File:MainActivity.java

 PackageCom.tutorialsbuzz.spinnertoolbar;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.os.Bundle;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.support.v7.widget.Toolbar;ImportAndroid.view.View;ImportAndroid.widget.AdapterView;ImportAndroid.widget.ArrayAdapter;ImportAndroid.widget.Spinner;ImportAndroid.widget.SpinnerAdapter;ImportAndroid.widget.Toast; Public classMainactivityextendsappcompatactivity {PrivateToolbar toolbar=NULL;PrivateString[] Category=NULL; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_main); category=getresources (). Getstringarray (r.array.category); toolbar=(Toolbar) Findviewbyid (R.id.toolbar); Setsupportactionbar (Toolbar); Getsupportactionbar (). Setdisplayshowtitleenabled (false); Toolbar.setlogo (R.drawable.ic_drawer); Spinneradapter Spinneradapter=Arrayadapter.createfromresource (Getapplicationcontext (), r.array.category, R.layout.spinner_dropdown_item); Spinner Navigationspinner=NewSpinner (Getsupportactionbar (). Getthemedcontext ()); Navigationspinner.setadapter (Spinneradapter); Toolbar.addview (Navigationspinner,0); Navigationspinner.setonitemselectedlistener (NewAdapterview.onitemselectedlistener () {@Override Public voidOnitemselected (adapterview<?> Parent, view view,intPositionLongID) {toast.maketext (mainactivity. This,"You selected:" +category[position],toast.length_short). Show ();} @Override Public voidOnnothingselected (adapterview<?>parent) {}});}}

Android Spinner in Toolbar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.