I have a TableLayout and in the third column of every row I want to place a radio group.
I build the RadioButtons like this:
rg = (RadioGroup) findViewById(R.id.radioGroup1);
for (int k = 0; k < size; k++) {
rb[k] = new RadioButton(context);
rg.addView(rb[k]);
}
However this cause my app to crash, any ideas?
You are building a primitive array with the length of megethos, but your loop uses the length size. If megethos and size are different values this can cause many different types of errors... But all of this redundant since a RadioGroup keeps this array up to date for you.
I would try something like this:
RadioGroup group = (RadioGroup) findViewById(R.id.radioGroup1);
RadioButton button;
for(int i = 0; i < 3; i++) {
button = new RadioButton(this);
button.setText("Button " + i);
group.addView(button);
}
And when you want to reference a button at index:
group.getChildAt(index);
Also please always post your logcat errors, it tells us exactly what went wrong and where to look. Otherwise we have to guess like this.
Update
The error is because you are trying to add the same button to two different layouts:
tr[k].addView(rb[k]);
rg.addView(rb[k]);
a view can only have one parent. As far as I know you cannot break a RadioGroup apart into multiple views without a lot of customization first. However a ListView already has the built-in feature setChoiceMode() that behaves like a RadioGroup:
List<String> list = new ArrayList<String>();
list.add("one");
list.add("two");
list.add("three");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, list);
ListView listView = (ListView) findViewById(R.id.list);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listView.setAdapter(adapter);
You can easily adapt simple_list_item_checked to display the SSID and signal strength. Hope that helps. (If you wait long enough imran khan might cut & paste my answer with graphical change, then claim it as his own again.)
I have created one demo app in which I have added Radio buttons dynamically and also handled click events.
public class MainActivity extends AppCompatActivity {
RadioGroup radioGroup2;
ArrayList<String> buttonNames;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup2 = (RadioGroup) findViewById(R.id.radioGroup2);
buttonNames = new ArrayList<>();
buttonNames.add("No Tip");
buttonNames.add("10%");
buttonNames.add("20%");
buttonNames.add("30%");
radioGroup2.setWeightSum(Float.parseFloat(buttonNames.size() + ""));
radioGroup2.setBackgroundColor(Color.BLUE);
for (int i = 0; i < buttonNames.size(); ++i) {
RadioButton radioButton = new RadioButton(this);
radioButton.setId(i);
RadioGroup.LayoutParams childParam1 = new RadioGroup.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1f);
childParam1.setMarginEnd(2);
radioButton.setGravity(Gravity.CENTER);
radioButton.setLayoutParams(childParam1);
radioButton.setBackground(null);
radioButton.setText(buttonNames.get(i));
radioButton.setTextColor(Color.BLUE);
radioButton.setBackgroundColor(Color.WHITE);
radioButton.setButtonDrawable(null);
if (buttonNames.get(i).equals("20%")) {
radioButton.setChecked(true);
radioButton.setBackgroundColor(Color.BLUE);
radioButton.setTextColor(Color.WHITE);
}
radioGroup2.addView(radioButton, childParam1);
}
radioGroup2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i1) {
RadioButton button = null;
for (int i = 0; i < buttonNames.size(); ++i) {
if (i1 == i) {
button = (RadioButton) findViewById(i1);
button.setChecked(true);
button.setBackgroundColor(Color.BLUE);
button.setTextColor(Color.WHITE);
Toast.makeText(getApplicationContext(), button.getText() + " checked", Toast.LENGTH_SHORT).show();
} else {
button = (RadioButton) findViewById(i);
button.setChecked(false);
button.setBackgroundColor(Color.WHITE);
button.setTextColor(Color.BLUE);
}
}
}
});
}
}
Related
I had added radio buttons dynamically like below.
for (int i = 0; i< typeArrayList.size(); i++) {
radioButtons[i] = new RadioButton(MainActivity.this);
radioButtons[i].setId(i);
radioButtons[i].setText(typeArrayList.get(i).toString());
if(i==0) {
radioButtons[i].setChecked(true);
}
typeLayout.addView( radioButtons[i]);
}
I have a button when clicked calls a method to which the selected item (text) of the dynamically added radio buttons should be passed . How can I get the selected radio button text for the dynamically added radio buttons?
Its very important to set your radio button id when adding buttons to a radio group.
RadioButton rb = new RadioButton(context);
rb.setText(option);
rb.setId(rb.hashCode());
radioGroup.addView(rb);
radioGroup.setOnCheckedChangeListener(mCheckedListner);
Now in your click listener check for the unique id.
private RadioGroup.OnCheckedChangeListener mCheckedListner = new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
JSONArray actionArray = new JSONArray();
if(group.findViewById(checkedId)!=null) {
RadioButton rb = ((RadioButton) group.findViewById(checkedId)).getText();
//Your Code here
}
}
}
};
You have stored all RadioButtons that you create dynamically in a radioButtons array.
So if you want to know which radiobutton is selected you can loop all this array and check each RadioButton
for (int i = 0; i< radioButtons.size(); i++) {
if(radioButtons[i].isChecked()){
// selected radio button is here
String text = radioButtons[i].getText();
}
}
First you have to get all childs of your view. Then check if this view is RadioButton. And at last check which button is checked.
int childcount = typeLayout.getChildCount();
for (int i=0; i < childcount; i++){
View view = typeLayout.getChildAt(i);
if (view instanceof RadioButton) {
if(((RadioButton)view).isChecked()) {
RadioButton yourCheckedRadioButton = (RadioButton) typeLayout.getChildAt(i); // this is your checked RadioButton
}
}
}
#Komali Shirumavilla
Add those dynamic radio buttons to radio group
so add following lines in your code
RadioGroup rg = new RadioGroup(this); //create the RadioGroup
rg.setOrientation(RadioGroup.HORIZONTAL);//or RadioGroup.VERTICAL
for (int i = 0; i< typeArrayList.size(); i++) {
radioButtons[i] = new RadioButton(MainActivity.this);
radioButtons[i].setId(i);
radioButtons[i].setText(typeArrayList.get(i).toString());
if(i==0) {
radioButtons[i].setChecked(true);
}
rg.addView(radioButtons[i]); // add dynamic radio buttons to radio group
}
typeLayout.addView(rg); // add radio group to view
Now set setOnCheckedChangeListener on the RadioGroup
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId) {
RadioButton btn = (RadioButton)findViewById(checkedId);
Log.d("Your selected radio button id",btn.getText());
}
}
});
This can be simply achieved using tags.
While adding the RadioButton set a tag that particular object. In your case, set the tag to be the text of the radio button,
radioButtons[i].setTag(typeArrayList.get(i).toString());
This way all your radio buttons will have the text associated to them as a tag.
Now whenever a particular radio button is selected, just get the tag which will give you the text associated with it.
String text = (String) selectedRadioButton.getTag();
Hope it helps.
In my app,i am creating radio buttons dynamically and want to uncheck all other radio buttons when one of them is checked.For this purpose,i am using RadioGroup.clearCheck() but it is not working at all.This is the code:
for (int i=0; i<files.length; i++)
{
rbi = new RadioButton(context);
rb1 = new RadioGroup(context);
rb1.addView(rbi);
nameOfFile = files[i].getName();
rbi.setText(nameOfFile);
ll.addView(rb1);
rbi.setOnClickListener(
new RadioButton.OnClickListener()
{
#Override
public void onClick(View v)
{
rb1.clearCheck();
rbi.setChecked(true);
}
Please help me.Even the alternate solutions for achieving the goal will be welcomed.Thanks in advance.
You create a lot of RadioGroups with just 1 RadioButton inside. That is probably not what you want. A RadioGroup needs to contain several RadioButtons so you can select an active button inside the list. See code below
// create 1 RadioGroup, add it to the layout
RadioGroup rg = new RadioGroup(context);
ll.addView(rg);
// add several RadioButtons to the RadioGroup
for (int i=0; i < files.length; i++) {
String nameOfFile = files[i].getName();
RadioButton rb = new RadioButton(context);
rb.setId(i); // assign an id
rb.setText(nameOfFile);
rg.addView(rb); // add to group
}
// do something when user checks a button
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
// user selected files[checkedId].getName();
}
});
Did you try (with System.out.println("test");) or something, if it even gets into the clearing part? Would test that first.
This is what zapl means
rb1.setOnCheckedChangeListener(
new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup rb1,
int checkedId) {
Log.v("Selected", "New radio item selected: " + checkedId);
}
});
if that works, then you can try this:
if (rbi.isChecked()){
rb1.clearCheck();
rbi.setChecked(true);
}
I have a doubt... In my Android app, I have an Activity that its GUI is created dynamically from certain data inside my SQLite database... I didn't had any kind of trouble with this and is working fine...
In this activity, there are a bunch of TextView's and RadioGroup's and RadioButton's that are created inside a for loop and the amount of RadioButton's varies due to a condition inside this loop... like this:
for(int i=0;i<numFilasFormulario;i++){
TextView pregunta = new TextView(this);
pregunta.setText(c.getString(c.getColumnIndex("texto")));
pregunta.setGravity(Gravity.LEFT);
pregunta.setTextColor(Color.rgb(0, 0, 0));
pregunta.setTextSize(15);
pregunta.setLayoutParams(params);
ll.addView(pregunta);
if(c.getString(c.getColumnIndex("tipopregunta")).equals("Si o No")){
RadioGroup rg = new RadioGroup(this);
ll.addView(rg);
RadioButton b1 = new RadioButton(this);
b1.setText("SI");
rg.addView(b1);
RadioButton b2 = new RadioButton(this);
b2.setText("NO");
rg.addView(b2);
}else{
if(c.getString(c.getColumnIndex("tipopregunta")).equals("Seleccion Simple")){
RadioGroup rg = new RadioGroup(this);
ll.addView(rg);
RadioButton b1 = new RadioButton(this);
b1.setText("SI");
rg.addView(b1);
RadioButton b2 = new RadioButton(this);
b2.setText("NO");
rg.addView(b2);
RadioButton b3 = new RadioButton(this);
b3.setText("N/A");
rg.addView(b3);
}
}
c.moveToNext();
}
So my question is how to obtain the value of the RadioButton selected by the user... I mean, do I have to invoke the method setOnClickListener() for each RadioButton? Or I have to do it for each RadioGroup? And where do I declare this statements: inside the for loop or outside? Or there is another way?
I'm very very lost here! Any kind of help would be apreciated! Thanks!
There are only one way to get it by using OnCheckedChangeListener
You can create each listener and assign it to your RadioGroup in the loop but I am not recomment to do that. You can create multiple ID in your resource and the create just a single listener before loop and in the loop assign it to your RadioGroup ( Your Radiogroup need set the ID that create in the resource ). Here is example:
OnCheckedChangeListener listener = new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (group.getId()) {
case R.id.myradio_group1:
// DO your work here
break;
default:
break;
}
}
};
// your loop is here
for(int i=0;i<numFilasFormulario;i++){
....
RadioGroup rg = new RadioGroup(this);
rg.setId(R.id.myradio_group1); // or simply just using rg.setId(i+1000);
// make sure 1000, 1001, 1002, ... is already create at Resource
....
}
In xml layout I have RadioGroup, Button1, Button2. When user clicks on button1, several radio buttons are programmatically created in RadioGroup (total amount of radio buttons may differ (pocet = count of radio buttons to be created).
final RadioButton[] rb = new RadioButton[pocet];
RadioGroup rg = (RadioGroup) findViewById(R.id.MyRadioGroup);
radiobuttonCount++;
for(int i=0; i<pocet; i++){
rb[i] = new RadioButton(this);
rb[i].setText("Radio Button " + radiobuttonCount);
rb[i].setId(radiobuttonCount+i);
rb[i].setBackgroundResource(R.drawable.button_green);
rg.addView(rb[i]);
}
What I try to do is this: When user selects xy item from RadioGroup, I'll pass selected value to textview and remove all radioButtons.
For deleting purpose I use:
public void onCheckedChanged(RadioGroup rGroup, int checkedId)
{
RadioButton checkedRadioButton = (RadioButton)rGroup.findViewById(checkedId);
boolean isChecked = checkedRadioButton.isChecked();
if (isChecked)
{
RadioGroup rg = (RadioGroup) findViewById(R.id.MyRadioGroup);
for (int i=0; i< rg.getChildCount(); i++){
rg.removeViewAt(i);
}
}
Problem is that this sometimes works well, but sometimes first radio button remains undeleted.
P.S.
Later I want to add button2 that will feed radiogroup with different items and different radio buttons amount. That's why I need to remove all radio buttons after user does selection.
Its really easy, you just do this:
rg.removeAllViews();
because i worked with the for loop but it didn't remove all the RadioButtons.
have fun :)
My first guess is that this portion of code is bad:
for (int i=0; i< rg.getChildCount(); i++){
rg.removeViewAt(i);
}
you can't run over one view's children while removing child at the same time
(rg.getChildCount() will change during the run)
I have about 60 radio groups that I need to register a listener for. Currently this is how I do it:
RadioGroup rg1 = (RadioGroup) getView().findViewById(R.id.rdoGrp1);
rg1.setOnCheckedChangeListener(this);
rg1 = (RadioGroup) getView().findViewById(R.id.rdoGrp2);
rg1.setOnCheckedChangeListener(this);
rg1 = (RadioGroup) getView().findViewById(R.id.rdoGrp3);
rg1.setOnCheckedChangeListener(this);
and so on for the 60 radio groups. Is there a way to set all radio groups on the fragment to register to a common listener in a single statement or two without having to type out this much code? Thanks
If all the RadioGroup elements share the same parent, you could iterate using ViewGroup.getChildAt and ViewGroup.getChildCount. Something like this:
ViewGroup parent = (ViewGroup) findViewById(R.id.ParentViewId);
for (int i = 0; i < parent.getChildCount(); i++) {
View currentView = parent.getChildAt(i);
if (currentView instanceof RadioGroup) {
((RadioGroup) currentView).setOnCheckedChangeListener(this);
}
}
An easy solution would be to create a method in your activity or whatever class you are writing, something like this:
private void registerListenerToGroup(int groupId) {
RadioGroup rg = (RadioGroup) getView().findViewById(groupId);
rg.setOnCheckedChangeListener(this);
}
Then, call this method from your code like this:
registerListenerToGroup(R.id.rdoGrp1);
registerListenerToGroup(R.id.rdoGrp2);
registerListenerToGroup(R.id.rdoGrp3);
//etc.