Detailed explanation of Spinner usage of Android

  • 2021-12-13 16:56:07
  • OfStack

Directory 1. Two display styles of Spinner 2. Spinner related methods 3. Examples of Spinner usage XML layout file activity_main. xml selected text style item_select. xml list other text styles item_dropdown. xmlJava code MainActivity. java4. List divider in dropdown mode

1. Two display styles of Spinner

There are two ways to display the drop-down list. One is to display the list directly below the current drop-down box. At this time, set the spinnerMode attribute to dropdown;; The other is to display the list as a dialog box in the middle of the page, and set the SpinnerMode property to dialog at this time.

2. Spinner related methods

setPrompt: Set the title text. setAdapter: Sets the adapter for the drop-down list. setSelection: Sets which item is currently selected. Note that this method is called after the setAdapter method. setOnItemSelectedListener: Sets the drop-down list of select listeners that implement the interface OnItemSelectedListener.

3. Examples of Spinner usage

XML Layout File activity_main. xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
 
    <Spinner
        android:layout_width="200dp"
        android:id="@+id/spinner"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:spinnerMode="dropdown"
        android:dropDownVerticalOffset="45dp"
        android:background="@null"/>
</LinearLayout>

Select text style item_select. xml


<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="45dp"
    android:background="@android:color/holo_blue_dark"
    android:textSize="14sp"
    android:textColor="@android:color/holo_red_light"
    android:gravity="center"/>

List other text styles item_dropdown. xml


<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="45dp"
    android:textColor="@android:color/black"
    android:textSize="14sp"
    android:gravity="center"/>

Java code MainActivity. java


public class MainActivity extends AppCompatActivity{
 
    private String[] starArray = {" Mercury "," Venus "," Earth "," Mars "," Jupiter "," Saturn "};
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initSpinner();
    }
 
    private void initSpinner(){
        // Declaration 1 Array adapters for drop-down lists 
        ArrayAdapter<String> starAdapter = new ArrayAdapter<String>(this,R.layout.item_select,starArray);
        // Set the layout style of the array adapter 
        starAdapter.setDropDownViewResource(R.layout.item_dropdown);
        // Get the name from the layout file sp_dialog Drop-down box of 
        Spinner sp = findViewById(R.id.spinner);
        // Set the title of the drop-down box. If you don't set it, there will be no ugly title 
        sp.setPrompt(" Please select a planet ");
        // Set the array adapter for the drop-down box 
        sp.setAdapter(starAdapter);
        // Set the default display of the drop-down box 1 Items 
        sp.setSelection(0);
        // Set the selection listener for the drop-down box, 1 Once the user selects a certain 1 Item, triggers the listener's onItemSelected Method 
        sp.setOnItemSelectedListener(new MySelectedListener());
    }
 
    class MySelectedListener implements AdapterView.OnItemSelectedListener{
 
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            Toast.makeText(MainActivity.this," What you have chosen is: "+starArray[i],Toast.LENGTH_SHORT).show();
        }
 
        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {
 
        }
    }
}

4. List divider in dropdown mode

Just add the following code to style

Add Style


<style name="XSpinnerStyle" parent="android:Widget.ListView.DropDown">
        <!--  Separation line color  -->
        <item name="android:divider">#000000</item>
        <item name="android:dividerHeight">1dp</item>
</style>

Then call in AppTheme


 <item name="android:dropDownListViewStyle">@style/XSpinnerStyle</item>

Note, however, that this dividing line will only be displayed if it is in dropdown style


Related articles: