How to show radio button when click on spinner in android?
11,913
you just need to add android layout select_dialog_singlechoice
on dropdown in spinner adaptor like:..
adapter.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);
Author by
Noman Malik
Updated on June 25, 2022Comments
-
Noman Malik about 2 years
I am working on an android project i want to show spinner data with radio button please suggest how can i do this. This is my code
Spinner spinner = (Spinner) findViewById(R.id.sMonth); spinner.setOnItemSelectedListener(this); ArrayList categories=new ArrayList(); categories.add("Month"); categories.add("Jan"); categories.add("Feb"); categories.add("Mar"); categories.add("Apr"); categories.add("May"); categories.add("Jun"); categories.add("Jul"); categories.add("Aug"); categories.add("Sep"); categories.add("Oct"); categories.add("Nov"); categories.add("Dec"); ArrayAdapter dataAdapter=new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,categories); dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(dataAdapter);
-
Noman Malik about 8 yearsThank you for help but i want radio button in spinner list? is it possible?
-
Aditya Vyas-Lakhan about 8 yearsdid you try with my answer? what output you get?
-
Noman Malik about 8 yearswhat is the meaning of this code. List<String> categories = new ArrayList<String>();
-
Noman Malik about 8 yearsi think &it and > are html tags.
-
Noman Malik about 8 yearsi tried but my output is a simple list. there is no radio button
-
Aditya Vyas-Lakhan about 8 yearsTry this way stackoverflow.com/questions/15731850/…
-
Soren Stoutner almost 7 yearsThis is the perfect answer. In older versions of Android, the radio button was part of
android.R.layout.simple_spinner_dropdown_item
, but it isn't any longer.android.R.layout.select_dialog_singlechoice
is the answer. And for those who are using custom layouts, just addandroid:checkMark="?android:attr/listChoiceIndicatorSingle"
andandroid:gravity="center_vertical"
(which makes the radio button align with the text). -
Soren Stoutner almost 7 yearsThis worked on older version on Android, but for current versions (I tested on 7.1.2) you need to use
setDropDownViewResource(android.R.layout.select_dialog_singlechoice)
as described in the answer above. -
Shivam Sharma almost 7 years@AdityaVyas-Lakhan this code and output image is not matched
-
Shivam Sharma almost 7 years@AdityaVyas-Lakhan dude, this single choice selection custom spinner works for outdated APIs thats no longer been used..Next time smartly copy answer from AndroidHive.com that should work for OP's problem.
-
Aditya Vyas-Lakhan almost 7 years@ShivamSharma questioner dint ask for version as well and it does not matter where code come from..only solution matters .he got his solution so relax and just chill buddy .period..