All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Android Spinner Style

Last Updated : Jul 15, 2023

Android Spinner Style

In this article we will show you the solution of android spinner style, an Android Spinner is a UI component used to show the list of options inside a drop-down menu. On the layout, the spinner is shown as a downward arrow. Each of these options can be chosen from and entered by the user.

This article will show you how to change the text styles for the items in the Android Spinner. a view that enables one child selection at a time for the user.

The Spinner's contents are drawn from the Adapter connected to this view.

In order to create a more customized list or display the spinner item with an image, text, etc.

On Android, a custom adapter similar to the base adapter must be used. To customize, we must first construct a unique adapter class but then extend our standard adapter within it.

Here, we override the adapter to display custom spinner routines to generate a custom list.

Step By Step Guide On Android Spinner Style :-

package com.example.spinner;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
class AndroidSpinnerExampleActivity extends Activity implements OnItemSelectedListener{
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      Spinner spinner = (Spinner) findViewById(R.id.spinner);
       spinner.setOnItemSelectedListener(this);
      List<String> categories = new ArrayList<String>();
      categories.add("Automobile");
      categories.add("Business Services");
      categories.add("Computers");
      categories.add("Education");
      categories.add("Personal");
      categories.add("Travel");
      ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
      dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
         spinner.setAdapter(dataAdapter);
   }
   @Override
   public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
      String item = parent.getItemAtPosition(position).toString();
          Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
   }
   public void onNothingSelected(AdapterView<?> arg0) {
       }
}
  1. First we create an import function.
  2. Next, we will work with the MainActivity.java file.
  3. Then, in the MainActivity.java file, add execution code.
  4. Then we create Spinner element
  5. Then we create Spinner click listener
  6. The dropdown number is then added in the code.
  7. In the Drop down layout style - listview with radio button
  8. Then we attaching data adapter to spinner
  9. Then we create On selecting a spinner item
  10. After that we Showing selected spinner item
  11. Finally, we add java code to complete the program's execution.

Conclusion :-

Spinners on Android give you a simple way to choose one value out of a range of values.

Spinners on Android are simply drop-down lists like those used in other programming languages. A spinner displays the value that is currently selected in its default state.

It provides a straightforward mechanism for selecting a value from a value list.

I hope this article on android spinner style helps you and the steps and method mentioned above are easy to follow and implement.