0

I want to enable radio button in android listview. I don't want to use any custom adapter to display radio button.

Here is my code:

private String[] items = {"Item 1", "Item 2", "Item 3", "Item 4"};
View contentsView = inflater.inflate(R.layout.trusted_credential_list_container, parent, false);

ListView mList = (ListView) contentsView.findViewById(R.id.cert_list);
mList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(),
        android.R.layout.simple_list_item_1, items);
mList.setAdapter(adapter);

In xml,

<ListView
    android:id="@+id/cert_list"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:choiceMode="singleChoice">
</ListView>

Eventhough I used choiceMode as singleChoice, why radio button is not displaying?

Second type I used without radio button in custom adapter that has only textview:

ListView mList = (ListView) contentsView.findViewById(R.id.cert_list);
mList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

/*ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(),
        android.R.layout.simple_list_item_checked, items);
mList.setAdapter(adapter);*/

ArrayList<User> arrayOfUsers = User.getUsers();
// Create the adapter to convert the array to views
CustomUsersAdapter adapter = new CustomUsersAdapter(getActivity(), arrayOfUsers);
mList.setAdapter(adapter);

Here also I tried using mList.setChoiceMode(ListView.CHOICE_MODE_SINGLE); but it is not working

2
  • May I ask why you're not using RecyclerView? ListView is kind of deprecated. Commented May 30, 2023 at 7:25
  • Because as per the requirement, I should not change the code from ListView to RecyclerView Commented May 30, 2023 at 7:27

1 Answer 1

0

The list item layout that you are using (android.R.layout.simple_list_item_1) does not contain a RadioButton, which is why you aren't seeing any RadioButton in your list.

If you want to display a RadioButton for each item in the ListView, you'll need to use a different layout. Android provides a built-in layout for this: android.R.layout.simple_list_item_single_choice.

You can replace android.R.layout.simple_list_item_1 with android.R.layout.simple_list_item_single_choice in your ArrayAdapter.

ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(),
    android.R.layout.simple_list_item_single_choice, items);
mList.setAdapter(adapter);
Sign up to request clarification or add additional context in comments.

3 Comments

So this is the only way? No other way? What if I use custom adapter without radio button?
If you want to use ListView, using a layout with a radio button is the only option. But you can use custom layouts. Open android.R.layout.simple_list_item_single_choice to see how it should look. You can create your own layout resource file, just make sure to use the same IDs.
@Shadow with custom adapter you can add anything, the sky is the limit. But as you used 'android.R.layout.simple_list_item_1' that's why I suggested 'android.R.layout.simple_list_item_single_choice'. Both are Android default resources for ArrayAdapter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.