Radio group not setting to radio buttons Android - java

I am fetching data from database. Based on the value, I'm dynamically creating radio buttons. When I try to add radio group to that radio buttons, radio group is not setting up. Bcoz of that I can select many radio buttons at once.
attr_layout[i].addView(radioButton, lp);
attr_layout[i]` is the value for buttons which im getting. The values
are Small, Medium and Large.
Here is the complete code for RadioButton.
RadioGroup radioGroup = new RadioGroup(mMain);
LinearLayout[] attr_layout = new LinearLayout[optionsList.size()];
attr_layout[i] = new LinearLayout(mMain);
attr_layout[i].setOrientation(LinearLayout.HORIZONTAL);
int attr_size = attributes.size();
for (int k = 0; k < attr_size; k++)
{
String price = String.format(Locale.ENGLISH, AppConstants.DECIMAL_POINTS, Float.parseFloat(attributes.get(k).getAttr_price()));
String name_price = attributes.get(k).getAttr_name()
+" ("+ mMain.getString(R.string.currency_code)
+" "+ price +")";
if(!multiSelect.equals("1")) // This multiselect value 1 and 0 is coming from database.
//Based on these value, app will display checkbox or radio button
{
final RadioButton radioButton = new RadioButton(mMain);
radioButton.setText(name_price);
radioButton.setId(i + 6);
radioButton.setTextSize(12);
radioButton.setTag(attributes.get(k));
radioButton.setGravity(Gravity.START | Gravity.CENTER_VERTICAL);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
{
radioButton.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
}
setTextFont(radioButton, "Museo_Slab.otf");
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT,
1f);
//lp.setMargins(10, 10, 0, 10); // llp.setMargins(left, top, right, bottom);
RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
radioGroup.setLayoutParams(params);
attr_layout[i].addView(radioButton, lp);`

Add radioButtons into radio group then add into layouts.
Use the code
radioGroup.addView(radioButton, lp);
attr_layout[i].addView(radioGroup)
instead of
attr_layout[i].addView(radioButton, lp);`

Radio group allows to check only one button at once belonging to it (if you are trying to check all buttons of a radio group).

radioGroup.addView(radioButton, lp); instead of attr_layout[i].addView(radioButton, lp); and then put attr_layout[i].addView(radioGroup); after adding all radioButtons, i.e. outside the for loop.

Related

Looping through specific widgets

Suppose I have a Table layout and i have a button called 'add people' which dynamically adds a row with two EdiTexts in it each time I click it. now there is another button which is supposed to save the first editText values in database and Add(sum) all the values of second editText .in HTML is very straight forward I can simply add class attribute to both inputs and loop through each class name. but I have no idea how i am supposed to do it in android.
What about saving the references in two lists? Then you can simply iterate over the lists to either store the values in a db or to sum them.
original answer check
public void init(){
TableLayout tablelayout = (TableLayout) findViewById(R.id.displayLinear);
for (int i = 0; i <2; i++) {
//Dynamic Button
Button myButton = new Button(this);
myButton.setText("Push Me");
LinearLayout ll = (LinearLayout)findViewById(R.id.buttonlayout);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
ll.addView(myButton, lp);
}
}

how to setChecked radio button in programmatically (radio button created in dynamically ) android

I try that
radioButton.setChecked(true);
but its only work 4th radio button. I try to create radio button dynamically. I create radio buttion within for loop, then store the radio button value. Then restore the radio button value (that means, I have 4 options that time I choose 2nd option and saved it then restore it(setChecked 2nd option) ) but its only setChecked 4th option.
Create radioButton.
for (int k = 0; k < choiceElementList.size(); k++) {
if (choiceElementList.get(k).dataFormatId == 1) {
radioButton = new RadioButton(getContext());
radioButton.setText(choiceElementList.get(k).getDataFormatValue());
radioButton.setLayoutParams(params1);
radioButton.setPadding(0, 5, 0, 5);
Log.e("setid", String.valueOf(choiceElementList.get(k).getId())) ;
radioGroup.addView(radioButton);
}
}
Try to restore that
if(choiceElementList.get(k).getId() == Cons.Id){
radioButton.setChecked(true);
}
First set Ids to your RadioButtons
for (int k = 0; k < choiceElementList.size(); k++) {
if (choiceElementList.get(k).dataFormatId == 1) {
RadioButton radioButton = new RadioButton(getContext());
// Set ID to Radio Button
radioButton.setId(k);
radioButton.setText(choiceElementList.get(k).getDataFormatValue());
radioButton.setLayoutParams(params1);
radioButton.setPadding(0, 5, 0, 5);
Log.e("setid", String.valueOf(choiceElementList.get(k).getId())) ;
radioGroup.addView(radioButton);
}
}
now just use your RadioGroup to check desire RadioButton with its ID
if(choiceElementList.get(k).getId() == Cons.Id){
radioGroup.check(k); // K will be your ID Set for your desire RadioButton
}
Happy Coding...
according to this code segments, your radioButton variable only refer last created element (radiobutton). That's why it only marks fourth one. You need to get correct reference for radio button what u want.
This is because you're adding all your RadioButton in a RadioGroup. When a RadioButton is checked in a RadioGroup, the othe RadioButton will be unchecked. You can see the RadioGroup documentation say it clearly:
This class is used to create a multiple-exclusion scope for a set of radio buttons. Checking one radio button that belongs to a radio group unchecks any previously checked radio button within the same group.
Intially, all of the radio buttons are unchecked. While it is not possible to uncheck a particular radio button, the radio group can be cleared to remove the checked state.
The selection is identified by the unique id of the radio button as defined in the XML layout file.

aligning dynamic buttons in gridlayout

I am trying to align dynamic buttons in Grid layout.
Problem:
However, if text is more than one line the formation of buttons in row
is not as needed.
String item[] = new String[]{"item1", "item2", "item3 yes",
"item4","item5"};
Button[] myButton = new Button[item.length];
GridLayout scrViewButLay = (GridLayout) findViewById(R.id.grid);
scrViewButLay.setColumnCount(4);
scrViewButLay.setRowCount(10);
// GridLayout.LayoutParams leftMarginParams = new GridLayout.LayoutParams(
// );
for (int index = 0; index < item.length ; index++) {
myButton[index] = new Button(this); //initialize the button here
myButton[index].setText(item[index]);
myButton[index].setHeight(100);
myButton[index].setWidth(100);
myButton[index].setTag(index);
scrViewButLay.addView(myButton[index]);
}
}
`
I recommend to use an Adapter and then customize however you want
check out this example
http://androidexample.com/Custom_Grid_Layout_-_Android_Example/index.php?view=article_discription&aid=76
I think using a recyclerView with GridLayoutManager should help you out.
This post https://inducesmile.com/android/android-gridlayoutmanager-with-recyclerview-in-material-design/ should help you out.
You only need to apply a layout_gravity as the default seems to be to align the text to be the same height (this is why it only happens with the item 3 with two lines).
GridLayout.LayoutParams param =new GridLayout.LayoutParams();
param.height = GridLayout.LayoutParams.WRAP_CONTENT;
param.width = GridLayout.LayoutParams.WRAP_CONTENT;
param.setGravity(Gravity.BOTTOM);

How to uncheck all radio buttons in a radio group, when a radio button is checked in Android?

I have created programmatically 5 radio groups with 4 radio buttons each. The problem is, when i first check a radio button from the first radio group and after that i check the second radio button from the same radio group, the first radio button remains checked. What can i do to have the normal behavior for all the radio buttons in the radio group?
This is my code:
radioGroup = new RadioGroup[5];
answer = new RadioButton[4];
int i = 0;
for (Question qn : questions) {
radioGroup[i] = new RadioGroup(this);
int j = 0;
for (Answer an : answers) {
if (qn.getID() == an.getQuestion_id_answer()) {
answer[j] = new RadioButton(this);
answer[j].setText(an.getAnswer());
radioGroup[i].addView(answer[j]);
j++;
}
}
linearLayout.addView(radioGroup[i]);
i++;
}
Thanks!
"Checking one RadioButton that belongs to a RadioGroup unchecks any previously checked RadioButton within the same group." Source: developer.android.com
If I understand right: You are talking about 4 RadioButtons in the same RadioGroup that refuse to uncheck themselves when another button in the same RadioGroup gets checked?
If that's the case, just give a different Resource ID to every RadioButton and that should fix it.

how can I add buttons in array in android

I am new user of android and making slide puzzle game and using eclipse.I made buttons in xml file.In java file I made an array of button type.Now my problem is how I add my buttons in array which I made in the xml file in table layout...
Each button should have a unique ID and you can use findViewById(R.id.ButtonX) to get the buttons. Say you have 5 buttons:
Button[] buttons = new Button[5];
buttons[0] = (Button)findViewById(R.id.Button0);
buttons[1] = (Button)findViewById(R.id.Button1);
buttons[2] = (Button)findViewById(R.id.Button2);
buttons[3] = (Button)findViewById(R.id.Button3);
buttons[4] = (Button)findViewById(R.id.Button4);
Of course the IDs have to match those in the XML. You could also use an ArrayList<Button> if you need to add or remove buttons from this array. Do note that adding or removing from the array will not add or remove them from the activity.
Create an activity. In Eclipse there should be 2 lower tabs with one letting you graphically edit the layout. Then all you have to do is link the Java buttons with the xml buttons.
You can try something like this. Assuming bi,b2 b3 are the button names in the XML
ArrayList<Button> bl = new ArrayList<Button>();
bl.add(new Button("b1"));
bl.add(new Button("b2"));
bl.add(new Button("b3"));
I understood that: You want to create dynamic button(You create button in java code not xml). Try that way:
Button btn1 = new Button(this);
Button btn2 = new Button(this);
Button btn3 = new Button(this);
//your table name : tbl
//you must create TableRow and add button to anyone row.And add row to table
TableRow row1 = new TableRow(this);
TableRow row2 = new TableRow(this);
row1.addView(btn1);
row2.addView(btn2);
row2.addView(btn3);
tbl.addView(row1);
tbl.addView(row2);

Categories