Android developmentAndroid tutorial

Android how to use a static spinner dropdown list

dropdown
1.8kviews

Android how to use a static spinner dropdown list

in this article, I am sharing how to use a spinner in android.  spinner is like a dropdown list to select an item dropdown list. Spinners provide a quick way to select one value from a set. In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one

Step 1:- Open your android studio and create an activity in your project.

Step 2:- Open your Activity XML file

Add spinner in your XML.

<Spinner
    android:id="@+id/spinnerQuantity"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:layout_weight="1.3"
    android:gravity="center"
    android:spinnerMode="dropdown"
    android:textColor="@color/secondryText"
    android:background="@drawable/editbox"
    />

Add spinner in your design screen where you can use the dropdown list and add it.

Step 3:-  Open your Java file.

public class CompleteCondition extends AppCompatActivity { 
private Spinner QuantitySpinner;
public static String QuantityID="";

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_complete_condition); 
 QuantitySpinner=(Spinner)findViewById(R.id.spinnerQuantity);
 Quantity(); 

 } 

 public void Quantity(){
    // Spinner Drop down elements
    List<String> categories = new ArrayList<String>();
    categories.add("1");categories.add("2");categories.add("3");categories.add("4");categories.add("5");categories.add("6");
    categories.add("7");categories.add("8");categories.add("9");categories.add("10");categories.add("11");categories.add("12");
    categories.add("13");categories.add("14");categories.add("15");categories.add("16");categories.add("17");categories.add("18");
    // Creating adapter for spinner
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
    // Drop down layout style - list view with radio button
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // attaching data adapter to spinner
    QuantitySpinner.setAdapter(dataAdapter);
    QuantitySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            // On selecting a spinner item
            ((TextView) parent.getChildAt(0)).setTextColor(Color.BLACK);
            QuantityID = parent.getItemAtPosition(position).toString();
            // Showing selected spinner item
//            Toast.makeText(parent.getContext(), QuantityID, Toast.LENGTH_LONG).show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
        }
    });
}

dropdown

 

 

Welcome to my blog! I’m Ritu Malik, and here at Codeplayon.com, we are dedicated to delivering timely and well-researched content. Our passion for knowledge shines through in the diverse range of topics we cover. Over the years, we have explored various niches such as business, finance, technology, marketing, lifestyle, website reviews and many others.