I am using RadioGroup, added RadioButton rdbut to RadioGroup rdgrp like rdgrp.addView(rdbut).
for(int j=0;j<3;j++)
{
RadioGroup rdgrp = new RadioGroup;
for(int i=0;i<=10;i++)
{
RadioButton rdbut = new RadioButton(this);
rdbut.setText("RadioButtion"+i);
rdbut.setId(i);
rdbut.setTag("somename");
rdgrp.addView(rdbut);
}
}
the above code shows how I initialize the radiogroup and radio button. after I run the this code, in emulator/mobile , i am able to check 2 radio buttons at a time.
What could be the problem?
Change your code like this.
RadioGroup rdgrp[] = new RadioGroup[3];
For(int j=0;j<3;j++)
{
RadioButton rdbut[] = new RadioButton[10];
For(int i=0;i<=10;i++)
{
rdbut[i].setText("RadioButtion"+i);
rdbut[i].setId(j*100+i);
rdbut[i].setTag("somename");
rdgrp[j].addView(rdbut[i]);
}
}
You have created three different Radio Group. You can select only one radio button from a single group.So from three groups you can select three buttons But there is no inter-group relationship. You can select radio buttons from different group simultaneously. In your case you can select three buttons at max.
Thanks
Sunil
Use Some thing like this xml design in user layout file.
<TableLayout
android:id="#+id/tbl_layoutchoice"
style="#style/InfoTableView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dip" >
<RadioGroup
android:id="#+id/SelectLayout_Group"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</RadioGroup>
</TableLayout>
and For Use this RadioGroup in Activity 's OnCreate() Method and findView like this
mRadioGroup = (RadioGroup) this.findViewById(R.id.SelectLayout_Group);
and Then Use Below Code With Your Require Change To Add Radio Button in One RadioGroup.Use Also Below Require Declaration for Create Radio Button Dynamically.
ArrayList<String> layoutlist = new ArrayList<String>(3);
int index = -1;
LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
for (String layout : layoutlist) {
RadioButton r = new RadioButton(this);
index++;
r.setText(layout);
r.setId(index);
r.setLayoutParams(lp);
r.setTextAppearance(this, R.style.TextBase);
mRadioGroup.addView(r);
}
So don't forget to add your String Value in layoutlist before for loop .and R.style is some Require Style for Text Show in RadioButton.
Related
I am trying to shuffle radio button in android, here's is the code what i have done till now, but as I proceed further I am not getting what should be the code or how to do that.
Please correct if I am wrong, or doing some silly mistake as I am new to it.
ArrayList<RadioButton> arrayText = new ArrayList<>();
arrayText.add(ropt0);
arrayText.add(ropt1);
arrayText.add(ropt2);
arrayText.add(ranswer);
Collections.shuffle(arrayText);
I am not getting what next after Collections.shuffle(arrayText);
should be the code
Please suggest
I think you don't quite understand what are you doing and what do you want. In your code you just adding all your RadioButtons to ArrayList and then shuffle this list. This is same as:
ArrayList<Stirng> list = new ArrayList<String>();
list.add("a1");
list.add("a2");
Collections.shuffle(list);
You see, this isn't connected to UI in any way. If you want to shuffle buttons in UI (so user will see it) you have 2 approaches:
Create and add your radio buttons in the code. Define some RadioGroup in view XML file and then create and add your RadioButtons
RadioGroup lay = (RadioGroup) findViewById(R.id.my_radio_group);
List<RadioButton> buttons = new ArrayList<>();
RadioButton rb1 = new RadioButton(this);
RadioButton rb2 = new RadioButton(this);
buttons.add(rb1);
buttons.add(rb2);
Collections.shuffle(buttons);
for(RadioButton rb:
buttons){
lay.addView(rb,new RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}
Shuffle just a right button position. Create N buttons in XML. And then set all texts in your code, so you can decide which button is the right one.
List<String> answers = new ArrayList<>(4); // 4 - button number
//Fill the list here
int rightAnswerPosition = ...; //Decide it when you fill your list
rb1.setText(answers.get(0));
//.. and so on
I have the following radigroup in the xml
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:orientation="horizontal"
android:id="#+id/radiogroup">
</RadioGroup>
and I am checking the number of objects in my json and generate radiobuttons and add them in the radiogroup as follows.
private void createRadioButton(int nImages) {
final RadioButton[] rb = new RadioButton[nImages];
for(int i=0; i<nImages; i++){
rb[i] = new RadioButton(this);
rb[i].setId(i);
radioGroup.addView(rb[i]);
}
}
now I need to know how to know and check selected radio button ?
when I was hardcoded, the following was working,
radioGroup.check(R.id.radioButton0);
but now I am adding radio buttons programmatically, I do not know how to handle
radioGroup.check(??);
If you want to select radio button at a particular position in radiogroup, you can use
radioGroup.check(radioGroup.getChildAt(position).getId());
where position is the position of the radio button in radio group
you set id of radiobutton as index of array in this part of code rb[i].setId(i);
you can use the following code radioGroup.check(i);
This is the layout I want to create but dynamically through java code.
Here parent layout is LinearLayout.
The tableLayout created through java code consists of 5 radio buttons which are created dynamically.
I need to place these radio button in radio group.
How to do that.
So far i tried this which is working fine. But am not able to add all the 5 radio buttons in 1 radio group.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l = (LinearLayout) findViewById(R.id.mainl); //parent layout
t1 = new TableLayout(this);
rb1 = new RadioButton(this);
rb2 = new RadioButton(this);
rb3 = new RadioButton(this);
t1.setLayoutParams(new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
TableRow row = new TableRow(this);
TableRow row1 = new TableRow(this);
row.addView(rb1);
row.addView(rb2);
row1.addView(rb3);
t1.addView(row);
t1.addView(row1);
l.addView(t1);
}
I can't find you'r declaration of a RadioGroup.
You need something like this:
...
RadioGroup rg = new RadioGroup(this);
rg.setOrientation(RadioGroup.HORIZONTAL);
rg.addView(rb1);
rg.addView(rb2);
rg.addView(rb3);
row.addView(rg);
...
RadioGroup inherits from LinearLayout. You'll have to create your own implementation of RadioGroup that inherits from TableLayout.
If you examine the source code of RadioGroup, you'll see it just keep track of its child views which are RadioButton's and makes sure only one of them is checked. Apart from that it provides a listener if checked RadioButton changes.
You can try modifying the source to make it extend from TableLayout etc.
I'm building the layout at run time, i have to show a radio group with 4 radio buttons for the user , but some times i should show an Edit Field with the radio button so the user can write some thing related to that radio button in the edit box.
I want the edit field to appear beside the radio button ,.
I tried to build the edit field but it keeps showing under the radio group, and if i sepereate one of the buttons in a linear layout with the edit text, it become out of the scope for the radio group.
this is the code for building the radio group
RadioGroup radioGroup = new RadioGroup(context);
radioGroup.setContentDescription(id);
for (int i = 0; i < vector.size(); i++) {
RadioButton radioButton = new RadioButton(context);
radioButton.setTextColor(Color.BLACK);
radioButton.setText("" + vector.get(i).getQ_text());
radioButton.setContentDescription(vector.get(i).getA_id());
radioButton.setTextSize(20);
radioButton.setTextColor(Color.parseColor("#A5462E"));
radioGroup.addView(radioButton);
radioGroup.setPadding(20, 0, 0, 0);
How can i build the edit field at run time to be shown next to the radio button.
My suggestion would be to create the layout for each row of your RadioGroup in a xml file like this:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<EditText
android:id="#+id/edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"/>
</LinearLayout>
And then in your loop you can inflate this layout and do what do you want with it:
final LinearLayout root = (LinearLayout) findViewById(R.id.root);
final RadioGroup radioGroup = new RadioGroup(this);
for (int i = 0; i < 3; i++) {
final LinearLayout item = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.item_radio_button, null);
final RadioButton button = (RadioButton) item.findViewById(R.id.button);
final EditText editText = (EditText) item.findViewById(R.id.edit_text);
button.setText("Test");
// Your condition to show or not the editText
if (i % 2 == 0) {
editText.setVisibility(View.VISIBLE);
}
radioGroup.addView(item);
}
root.addView(radioGroup);
If I have a page whose layout is designated by XML code, but I want to possibly create a few radio buttons, say, in the middle, but decide that at runtime, how would I do it? I'm new to Android so I'm taking a stab in the dark. Would something like this work?
In the XML, add a LinearLayout to the middle of the page's XML like this:
<LinearLayout android:id="#+id/LinLayBut"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
And then in the java something like this:
public void setupRadioButtons(){
LinearLayout linLay;
linLay = (LinearLayout) findViewById(R.id.LinLayBut);
RadioGroup radGroup = new RadioGroup(this);
RadioButton radBut = new RadioButton(this);
radGroup.addView(radBut, 0);
radGroup.setText("A button");
}
This is not an efficient way to build dynamic UI. You would be better off defining the optional layout in an XML file and then inflate it when you want to use it:
public void setupRadioButtons() {
final LayoutInflater inflater =
(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout buttons =
(LinearLayout) inflater.inflate(R.layout.LinLayBut, null);
LinearLayout mainLayout = (LinearLayout) findViewById(R.id.main);
mainLayout.addView(buttons);
}
The above code assumes that the radio group and buttons are defined inside the LinearLayout with id LinLayBut and you main layout id is main.
OK, thanks to unluddite, I got it to work. For those tortured souls following the thread, here's the code. The XML doesn't have a layout around it. And if I don't call the method, the radio group takes no vertical space:
<RadioGroup android:id="#+id/radButGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" />
and here's the method:
public void setupRadioButtons(){
RadioGroup radGroup;
radGroup = (RadioGroup) findViewById(R.id.radButGroup);
RadioButton radBut0 = new RadioButton(this);
radGroup.addView(radBut0, 0); //2nd arg must match order of buttons
radBut0.setText("one Button");
RadioButton radBut1 = new RadioButton(this);
radGroup.addView(radBut1, 1);
radBut1.setText("Two Button");
radBut1.setChecked(true); //which button is set