Android ListView RadioButton - Androhub

RadioButton List Banner

Android ListView RadioButton

In previous article  we learnt about how to use multiple choices in ListView. Today we are going to learn how to enable single choice/radio button in ListView, GridView and in RecyclerView using RadioButton, you’ll configure the ListView to display a radioButton next to each item. You can combine many components like TextView, RadioButton, ImageView, etc. and can make ListView as you require.

Prerequisite:

  1. Material Tabs Tutorial
  2. ListView Tutorial
  3. GridView Tutorial
  4. RecyclerView Tutorial

Example

In this tutorial, we are going to learn how to enable single choice/radio button in ListView, GridView and in RecyclerView using RadioButton.

VIDEO DEMO

1. Create a new project in Android Studio by navigating to File ⇒ New ⇒ New Project and fill required details. By default my activity is MainActivity.java.

2. Open build.gradle and include this libraries show below:

3. Now create 3 material tabs naming ListView, GridView and RecyclerView. For this you can check this link for reference.

4. After creating tabs lets move to creating xml layouts for individual tabs.

  • list_view_frament.xml

  • grid_view_fragment.xml

  • recycler_view_fragment.xml

5. In all the above xml layouts i am using one common layout i.e. bottom_container.xml. Lets create the common layout xml, this layout contains two buttons for implementing actions over selected row.

6. Now lets create java classes for all the above three xml files.

  • ListViewFragment.java

  • GridViewFragment.java

  • RecyclerViewFragment.java

7. Now after creating all fragment classes we have to create Adapters for ListView, GridView and RecyclerView.

  • GridListAdapter.java

  • RecyclerViewAdapter.java

In both the above adapter the common code to select row is:

8. Finally all things done. Now you can also make your any type of single choice list view.

Thanks. 🙂

 

Post comment

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