Listview with Spinner in android example

top banner

Listview with Spinner in android example

ListView and Spinner are two commonly used UI widgets in Android Studio that help to display data to the users in a user-friendly manner. A ListView is a view group that displays a list of scrollable items in a vertical list while a Spinner is a dropdown list that allows the user to select one option from a list of options. ListView and Spinner can be easily implemented in Android Studio by creating an ArrayAdapter and setting it as the adapter for the respective widget. This enables the data to be displayed on the screen in a format that is easy for the user to navigate and interact with.
Create a new Android Studio project with an empty activity.
In the activity layout file (activity_main.xml), add the following code:

<?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 -->
   <Spinner
       android:id="@+id/spinner"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:background="@android:drawable/btn_dropdown"
       android:spinnerMode="dropdown" />

   <!-- ListView -->
   <ListView
       android:id="@+id/listview"
       android:layout_width="match_parent"
       android:layout_height="match_parent" />

</LinearLayout>

In the activity class file (MainActivity.java), add the following code:

public class MainActivity extends AppCompatActivity {

   private Spinner spinner;
   private ListView listView;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

       // Initialize views
       spinner = findViewById(R.id.spinner);
       listView = findViewById(R.id.listview);

       // Set spinner options
       ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.spinner_options, android.R.layout.simple_spinner_item);
       adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
       spinner.setAdapter(adapter);

       // Set listview adapter
       ArrayAdapter<String> listAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, android.R.id.text1, getListData());
       listView.setAdapter(listAdapter);

       // Handle spinner item selection
       spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
           @Override
           public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
               String selectedOption = parent.getItemAtPosition(position).toString();
               // Update list data based on selected option
               listAdapter.clear();
               listAdapter.addAll(getListDataForOption(selectedOption));
               listAdapter.notifyDataSetChanged();
           }

           @Override
           public void onNothingSelected(AdapterView<?> parent) {
           }
       });
   }

   // Returns some dummy data for the listview
   private List<String> getListData() {
       List<String> data = new ArrayList<>();
       data.add("Item 1");
       data.add("Item 2");
       data.add("Item 3");
       data.add("Item 4");
       data.add("Item 5");
       return data;
   }

   // Returns filtered data for the listview based on the selected spinner option
   private List<String> getListDataForOption(String option) {
       List<String> data = new ArrayList<>();
       if (option.equals("Option 1")) {
           data.add("Item 1");
           data.add("Item 2");
           data.add("Item 3");
       } else if (option.equals("Option 2")) {
           data.add("Item 4");
           data.add("Item 5");
       }
       return data;
   }
}

This code initializes the Spinner and ListView views, sets the spinner options from a string array in the
res/values/strings.xml file, and sets the listview adapter with some dummy data. It also handles the spinner item selection event and updates the listview data based on the selected option.
you can customize the Spinner background by setting the android:background attribute to a drawable resource. For example, to set a custom background image for the spinner, you can create a new drawable resource file in the res/drawable/ folder (e.g., spinner_background.xml) and add the following code:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
   <solid android:color="#FFFFFF" />
   <corners android:radius="8dp" />
   <stroke android:width="1dp"
    android:color="#CCCCCC" />
</shape>

This creates a rectangle shape with rounded corners and a white background color with a gray stroke around it. You can adjust the values of the attributes to customize the background according to your preference.
Then, in the Spinner element in your layout file (activity_main.xml), you can set the android:background attribute to the drawable resource you just created, like this:

<Spinner
   android:id="@+id/spinner"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:background="@drawable/spinner_background"
   android:spinnerMode="dropdown" />

This will set the custom background for the Spinner.

Leave a Reply

Your email address will not be published. Required fields are marked *