Drop-down box layout a_spinner_checked_text.xml
<?xml version= "1.0" encoding= "Utf-8"?>
<checkedtextview xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:id= "@android: Id/text1"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:gravity= "Center_vertical"
Android:padding= "8DP"
Android:singleline= "true"
Android:textcolor= "#00f"
Android:textsize= "18SP" >
</CheckedTextView>
Drop-down box content layout a_spinner_item_layout.xml
<?xml version= "1.0" encoding= "Utf-8"?>
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
Android:background= "#fff"
Android:padding= "8DP"
>
<textview
Android:id= "@+id/spinner_item_label"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_alignparentleft= "true"
Android:layout_centervertical= "true"
Android:textcolor= "#00f"
Android:textsize= "14sp"/>
<imageview
Android:id= "@+id/spinner_item_checked_image"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_alignparentright= "true"
Android:layout_centervertical= "true"
android:src= "@drawable/a_check_unselect"/>
</RelativeLayout>
Home Layout Activity_main.xml
<linearlayout xmlns: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"
android:orientation= "Vertical"
tools:context= "Com.example.spinner.MainActivity" >
<linearlayout
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:gravity= "Center"
android:orientation= "Horizontal"
android:layout_margin= "15DP" >
<textview
Android:layout_width= "100DP"
android:layout_height= "Wrap_content"
android:layout_marginright= "5DP"
android:gravity= "Center"
android:text= "Nation"
Android:textcolor= "#000"
Android:textsize= "18sp"/>
<spinner
Android:id= "@+id/add_minzsp"
Android:layout_width= "0DP"
android:layout_height= "Wrap_content"
Android:layout_gravity= "Center_vertical|center_horizontal"
android:layout_weight= "1"/>
</LinearLayout>
</LinearLayout>
Mainactivity Code
public class Mainactivity extends Activity {
Private Spinner Minzuspinner;
Private string[] Minzustrings = {"Please select", "Han", "Uighur", "Hui", "Kazak", "Mongolian",
"Kirgiz", "Tajik", "Zhuang", "Uzbek"};
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Minzuspinner = (Spinner) Findviewbyid (R.ID.ADD_MINZSP);
Spinneradapter adapter = new Spinneradapter (This,
R.layout.a_spinner_checked_text, Minzustrings, Minzuspinner);
Adapter.setdropdownviewresource (r.layout.a_spinner_item_layout);
Minzuspinner.setadapter (adapter);
}
}
The Spinneradapter code to use
public class Spinneradapter extends Arrayadapter<string> {
Context context;
string[] items = new string[] {};
Spinner Spinner;
Public Spinneradapter (Final context context,
Final int Textviewresourceid, final string[] objects,final Spinner Spinner) {
Super (context, Textviewresourceid, objects);
This.items = objects;
This.context = context;
This.spinner=spinner;
}
@Override
Public View Getdropdownview (int position, view Convertview,viewgroup parent) {
if (Convertview = = null) {
Layoutinflater Inflater = layoutinflater.from (context);
Convertview = Inflater.inflate (
R.layout.a_spinner_item_layout, parent, false);
}
TextView label = (TextView) Convertview.findviewbyid (R.id.spinner_item_label);
ImageView check = (ImageView) Convertview.findviewbyid (r.id.spinner_item_checked_image);
Label.settext (Items[position]);
if (spinner.getselecteditemposition () = = position) {
Convertview.setbackgroundcolor (Context.getresources (). GetColor (R.color.spinner_green));
Check.setimageresource (R.drawable.a_check_select);
} else {
Convertview.setbackgroundcolor (Context.getresources (). GetColor (R.color.spinner_light_green));
Check.setimageresource (R.drawable.a_check_unselect);
}
return convertview;
}
}
Results
Android Custom drop-down box implementation Spinner