I need to add radio button dynamically. A radio button may be 3, 4, 5 or 6 and it would be added horizontally and one row contains maximum 3 radio button.
If there are more than 3 then it would come below of above row of radio button as in grid view. My code for radio button are below but it display all radio button in a single row, means it's hiding the radiobutton.
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Choose Your Favorite Actress" >
</TextView>
<RadioGroup
android:id="#+id/RadioGroup01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</RadioGroup>
<Button
android:id="#+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" >
</Button>
</LinearLayout>
And Java class is:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DisplayRadioButton();
}
public void DisplayRadioButton() {
for(int i=0;i<10;i++) {
RadioGroup radiogroup = (RadioGroup)findViewById(R.id.RadioGroup01);
RadioButton rdbtn = new RadioButton(this);
rdbtn.setId(i);
rdbtn.setText(text[i]);
radiogroup.addView(rdbtn);
}
}
please try in the following way:
1)in your xml remove the RadioGroup. create it by dynamically
RadioGroup radiogroup[];
RadioButton rdbtn[];
LinearLayout linear[];
radiogroup = new RadioGroup[9/3];
rdbtn = new RadioButton[9];
linear = new LinearLayout[9/3];
......
int count = 0; // integer flag
for(int i=0;i<9;i++){
if the value of i is equal to 3 multiple then increase count by 1
// sett linear[count]'s orientation is horizontal.
root_layout.addView(linear[count]);
radiogroup[count] = new RadioGroup(this);
linear[count].addView(radiogroup[count]); // add radio group to linear layout
add radio button to radio group.
rdbtn[i] = new RadioButton(this);
rdbtn[i].addView(radiogroup[count]);
}
i hope you get solved. be aware of array index out of bound exception.
your xml may look like:
<LinearLayout
android:id= rootlayout
..... // the child linearlayout
.. . radio group
... radio button
</LinearLayout>
Related
I am trying to create a button for each year since the person started using my app. So in my xml document I have
<HorizontalScrollView
android:id="#+id/yearScrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/white"
android:layout_gravity="center">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/currentYear"
android:tag="01"
android:text="2015"
android:paddingLeft="8.0dip"
android:paddingRight="8.0dip"
android:height="24dp"
android:textSize="18sp"
android:textColor="#333333"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/white"
>
</Button>
</LinearLayout>
</HorizontalScrollView>
Then I have the following code
private List<Button> yearButtons;
private static final int[] YEAR_BUTTON_IDS = {
R.id.currentYear,
};
Then I must find out what year it is and overwrite my current button
int firstYear = Integer.parseInt(year);
yearButtons.get(0).setText(String.valueOf(CurrentYear));
then in my init class I substantiate the buttons, I understand I do not need a loop for only 1 button but leaving it like this for consistency with how the months buttons work
for(int id : YEAR_BUTTON_IDS) {
Button button = (Button)findViewById(id);
button.setOnClickListener(this);
yearButtons.add(button);
}
Then I have some logic to find out the first year they started called yearsOfTransactions
Then I have the following loop where I try create the buttons
for (int i =0; i< yearsOfTransactions; i++){
int yearToAdd = CurrentYear-i-1;
Button myButton = new Button(this);
myButton.setText(String.valueOf(yearToAdd));
yearButtons.add(myButton);
}
However this is not working..
Thanks for any help
I am making slight modification in your code :
<HorizontalScrollView
android:id="#+id/yearScrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/white"
android:layout_gravity="center">
<LinearLayout
android:id="#+id/button_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/currentYear"
android:tag="01"
android:text="2015"
android:paddingLeft="8.0dip"
android:paddingRight="8.0dip"
android:height="24dp"
android:textSize="18sp"
android:textColor="#333333"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/white"
>
</Button>
</LinearLayout>
</HorizontalScrollView>
Now make an array of years to add (in onCreate())
int[] yearToAdd = {2000, 2001, 2002, 2003};
LinearLayout parentLayout = (LinearLayout)findViewById(R.id.button_parent);
for (int i =0; i< yearToAdd.lenght; i++){
int yearToAdd = yearToAdd[i];
Button myButton = new Button(this);
myButton.setText(String.valueOf(yearToAdd));
yearButtons.add(myButton);
yearButtons.setOnClickListener(this);
parentLayout.addView(myButton);
}
Let me know in case of more clearation you need, hope it will help :)
you have to add button to a linear layout.
This is a function i use to add dynamic buttons in a linear layout.
public Button createButton(String label) {
Button button = new Button(mContext);
button.setText(label);
mDynamicLayout.addView(button, mLayoutParam);
return button;
}
You should add every button you create to the LinearLayout. First set and id in xml to LinearLayout. Find the view in your class and, finally, add the buttons.
LinearLayout container = findViewById(R.id.container);
//...
for (int i =0; i< yearsOfTransactions; i++){
int yearToAdd = CurrentYear-i-1;
Button myButton = new Button(this);
myButton.setText(String.valueOf(yearToAdd));
yearButtons.add(myButton);
// you missed that
container.addView(myButton);
}
please try this inside your for loop
LinearLayout layout = (LinearLayout) findViewById(R.id.linear_layout_tags);
//set the properties for button
Button btnTag = new Button(this);
btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
btnTag.setText("Button");
btnTag.setId(some_random_id);
//add button to the layout
layout.addView(btnTag);
I am working on quiz application in android. I need to display a questions and its options with radio buttons. When next button is clicked next question and its options will be displayed. I am able to display the questions and options but I am getting one issue.
First time first question and its options are displaying fine. When next button is clicked next question is displaying but for options both first question options and second question options with radio buttons are displaying. Again when next button is clicked third question with 1st,2nd options also displaying. How can I remove the radio buttons of previous question when next button is clicked.
My code:
main.xml
<ScrollView
android:id="#+id/scrl"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/widgetnotes"
android:layout_margin="6dp"
android:background="#FFFFFF"
android:scrollbars="none" >
<RelativeLayout
android:id="#+id/rel"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/tv_question"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000" />
<LinearLayout
android:id="#+id/chklnlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/tv_question"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>
</ScrollView>
Page.java
{
------
//first question----
optionslayout = (LinearLayout) findViewById(R.id.chklnlayout);
tv.setText(question);
tv.setTextColor(Color.parseColor("#000000"));
RadioGroup radiogroup = (RadioGroup) new RadioGroup(Page.this);
optionslayout.addView(radiogroup);
for (int k = 0; k < arr.length; k++) {
RadioButton newRadioButton = new RadioButton(Page.this);
newRadioButton.setText(arr2.get(k));
newRadioButton.setTextColor(Color.parseColor("#000000"));
radiogroup.addView(newRadioButton);
}
//next click--
public void next(View v)
{
if (i < array.length - 1) {
i++;
optionslayout = (LinearLayout) findViewById(R.id.chklnlayout);
tv.setText(question);
tv.setTextColor(Color.parseColor("#000000"));
RadioGroup radiogroup = (RadioGroup) new RadioGroup(Page.this);
optionslayout.addView(radiogroup);
for (int p = 0; p < nextarr.length; p++) {
RadioButton newRadioButton = new RadioButton(Page.this);
newRadioButton.setText(arr6.get(p));
newRadioButton.setTextColor(Color.parseColor("#000000"));
radiogroup.addView(newRadioButton);
}
}
}
I tried putting optionslayout.removeAllViews(); in next click action but when I kept that it is not displaying options of next question. Please help me with this issue.
just try this:
set Radio Group as,
radioGroup.clearCheck();
and Radiobutton as:
.setChecked(false);
This question already has answers here:
Android radio button uncheck
(6 answers)
Closed 2 years ago.
The application is a step sequencer application with 16 radio groups with 8 buttons in each group. It works perfectly except once a group has a button selected I cant turn it off unless I use the clear button I have created to clear all radiogroups. What I would like to add is some code that says when a selected radio button is selected again it simply turns off like a toggle. I tried using toggles but then other issues arose with that method. Below is an attempt at it but it is way off the mark Im guessing
final RadioGroup radioGroup1 = (RadioGroup)findViewById(R.id.RadioGroup1);
RadioButton lC1 = (RadioButton)findViewById(R.id.RadioButtonlowC1);
Button D1 = (Button)findViewById(R.id.RadioButtonD1);
D1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
PdBase.sendFloat("D1", 74);
int selectedTypeId = radioGroup1.getCheckedRadioButtonId();
RadioButton D1 = (RadioButton) findViewById(selectedTypeId);
if(radioGroup1 != null) // This will be null if none of the radio buttons are selected
radioGroup1.clearCheck();
PdBase.sendFloat("D1", 0);
}
});
You need to put the button in a radio group, then clear the group. A radio button can't be unchecked directly, because the idea is that one option in a group is always checked.
Here is an example of how to create radiogroup, radiobutton and how to use in Java code. Hope it will give you complete picture
XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RadioGroup
android:id="#+id/maptype"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/line1"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/map"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:drawableRight="#drawable/ic_launcher"/>
<RadioButton
android:id="#+id/satellite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableRight="#drawable/ic_launcher"/>
</RadioGroup>
</LinearLayout>
In Java Code
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RadioGroup rgrpMapType = (RadioGroup) findViewById(R.id.maptype);
int selectedTypeId = rgrpMapType.getCheckedRadioButtonId();
RadioButton rbMapType = (RadioButton) findViewById(selectedTypeId);
if(rbMapType != null) // This will be null if none of the radio buttons are selected
rgrpMapType.clearCheck();
}
I'm creating a radio group with radio buttons (from an enumeration) at runtime with the following code.
RadioGroup radioGroup = new RadioGroup(this);
List<LocationTypeEnum> warningTypes = preferences.getWarningTypes();
for (LocationTypeEnum enumElement : warningTypes) {
RadioButton radio = new RadioButton(this);
radio.setText(enumElement.toString());
//Check one specific radio by default
radio.setChecked(enumElement.intValue == userDefinedLocation.getType().intValue);
radioGroup.addView(radio, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
}
When it comes to screen and I try to change the radio, both options remain checked:
What's going wrong?
Found the answer. You must add the RadioButton to the RadioGroup before setting it to checked, or else the radio group gets lost. Following, the correct code.
RadioGroup radioGroup = new RadioGroup(this);
List<LocationTypeEnum> warningTypes = preferences.getWarningTypes();
for (LocationTypeEnum enumElement : warningTypes) {
RadioButton radio = new RadioButton(this);
radio.setText(enumElement.toString());
//First, add the radio to the group
radioGroup.addView(radio, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
//Only after that you can check it.
radio.setChecked(enumElement.intValue == userDefinedLocation.getType().intValue);
}
Sounds like a bug, to me. Credits to http://code.google.com/p/android/issues/detail?id=1772
Don't know how to comment to ask you to post your layout .xml file. Make sure that you also group them in your xml file like below:
<RadioGroup
android:id="#+id/radioSex"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/radioMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_male"
android:checked="true" />
<RadioButton
android:id="#+id/radioFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_female" />
</RadioGroup>
Is there a way to set the selected index of a RadioGroup in android, other than looping through the child radiobuttons and selecting checking the radio button at the selected index?
Note: I am populating the Radio Button Group at run time.
If your radio group is defined in a layout xml file, each button can be assigned an id. Then you just check a button like this
radioGroup.check(R.id.myButtonId);
If you created your radio group programmatically (I'm not even sure how you do this...), you might want to consider creating a special layout xml file just for the radio group so that you can assign R.id.* ids to the buttons.
Please see the answer below if you are, in fact, looking to set the radio button group by index, see the answer below.
((RadioButton)radioGroup.getChildAt(index)).setChecked(true);
Question said "set selected INDEX", here's how to set it by index:
((RadioButton)radioGroup.getChildAt(index)).setChecked(true);
........
Additional info: It seems that Google wants you to use id instead of index, because using id is more bug proof. For example, if you have another UI element in your RadioGroup, or if another developer re-orders the RadioButtons, the indices might change and not be what you expected. But if you're the only developer, this is totally fine.
you can do findViewById from the radio group .
((RadioButton)my_radio_group.findViewById(R.id.radiobtn_veg)).setChecked(true);`
Siavash's answer is correct:
((RadioButton)radioGroup.getChildAt(index)).setChecked(true);
But be aware that a radioGroup can contain views other than radioButtons -- like this example that includes a faint line under each choice.
<RadioGroup
android:id="#+id/radioKb"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/kb1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:button="#null"
android:drawableRight="#android:drawable/btn_radio"
android:text="Onscreen - ABC" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#33000000" />
<RadioButton
android:id="#+id/kb2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:button="#null"
android:drawableRight="#android:drawable/btn_radio"
android:text="Onscreen - Qwerty" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#33000000" />
<RadioButton
android:id="#+id/kb3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:button="#null"
android:drawableRight="#android:drawable/btn_radio"
android:text="Standard softkey" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#33000000" />
<RadioButton
android:id="#+id/kb4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:button="#null"
android:drawableRight="#android:drawable/btn_radio"
android:text="Physical keyboard" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#33000000" />
</RadioGroup>
In this case using an index of 1, for example, would generate an error. The item at index 1 is the first separator line -- not a radioButton. The radioButtons in this example are at indexes 0, 2, 4, 6.
This Worked For me, I created radio button dynamically by
private void createRadioButton() {
RadioButton[] rb = new RadioButton[5];
RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
1.0f);
radioGroup.setOrientation(RadioGroup.HORIZONTAL);
for (int ID = 0; ID < 5; ID++) {
rb[ID] = new RadioButton(this);
rb[ID].setLayoutParams(layoutParams);
rb[ID].setText("Button_Text");
radioGroup.addView(rb[ID]); //the RadioButtons are added to the radioGroup instead of the layout
}
}
Now Check a button using,
int radio_button_Id = radioGroup.getChildAt(index).getId();
radioGroup.check( radio_button_Id );
Inside onBindViewHolder set the tag to Button Group
#Override
public void onBindViewHolder(final CustomViewHolder holder, final int position) {
...
holder.ButtonGroup.setTag(position);
}
and in the ViewHolder
ButtonGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
...
int id = radioGroup.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton) radioGroup.findViewById(id);
int clickedPos = (Integer) radioGroup.getTag();
packageModelList.get(clickedPos).getPoll_quest()
}
Using Kotlin you can make it by
(radio_group_id.getChildAt(index) as RadioButton).isChecked = true
or
radio_group_id.check(R.id.radio_button_id)