I'd like to create a custom View which contains a RadioGroup. Inside of the RadioGroup I'd like the RadioButtons to be set up so that the first RadioButton is at the top left, the 2nd one is below that, the third to the right of the 1st one and the 4th one underneath that. In other words, I want to create a group where the radiobuttons are laid out in a square of sorts. I think if I set the orientation of the group to be vertical, then all the radiobuttons will be in a straight line. If, on the other hand, I set the orientation to horizontal, then, again, the radiobuttons will all be in a straight line, going horizontal. Is there a way to do what I want or am I forced to set up two separate RadioGroups, both to horizontal orientation?
Try processing the RadioButtons without the use of RadioGroup.
Wire-up the individual RadioButtons and hold them in an ArrayList<RadioButton>.
List<RadioButton> radioButtons = new ArrayList<RadioButton>();
radioButtons.add( (RadioButton)findViewById(R.id.button1) );
radioButtons.add( (RadioButton)findViewById(R.id.button2) );
radioButtons.add( (RadioButton)findViewById(R.id.button3) );
etc.
Set an OnCheckedChangeListener for each RadioButton.
for (RadioButton button : radioButtons){
button.setOnCheckedChangeListener(new OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) processRadioButtonClick(buttonView);
}
});
}
Then create a method to uncheck the unselected RadioButtons.
private void processRadioButtonClick(CompoundButton buttonView){
for (RadioButton button : radioButtons){
if (button != buttonView ) button.setChecked(false);
}
}
Using this approach, the RadioButtons can be located anywhere within the XML layout.
You should subclass from RadioGroup and override onLayout() method.
Related
I have a custom view for radio buttons in the XML layout file with ConstraintLayout. I need to group those radio buttons. But when I try to envelop that ConstraintLayout with the Radio group, it does not group the buttons and all options are getting selected.
RadioButtons need to be the direct children of a RadioGroup in order for the grouping functionality to work. A RadioGroup is just a special LinearLayout that implements the grouping functionality for all the children added that are RadioButtons. You can simply implement your own grouping functionality by adding the RadioButtons to a List and then implementing OnCheckedChangeListener and setting it on each of the RadioButtons as follows:
private final List<RadioButton> radioButtons = new ArrayList<>();
private int checkedId;
private void setRadioButton(RadioButton radioButton) {
radioButtons.add(radioButton);
radioButton.setOnCheckedChangeListener(mOnCheckedChangeListener);
}
private final CompoundButton.OnCheckedChangeListener mOnCheckedChangeListener =
new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
checkedId = buttonView.getId();
}
for (RadioButton radioButton : radioButtons) {
if (radioButton.getId() != checkedId) {
radioButton.setChecked(false);
}
}
}
};
you can set false the checked of icon in the defult running like this code in layout
xml
android:checked="false"
and in coding you can call binding.rdiobutton.setonclicklistenert{
binding.btnclick.setonclicklistener)
Use the following code in kotlin
radioGroup.check(id)
First, you write the name of the radio group, then instead of the ID, put the ID of the radio button,
For example, it is like this in my project
rg_licenseType.check(R.id.rb_three)
New to Android,
I am creating a question Answer App. So I want to get the value of checkBoxes.
I know how to get value of radio button present in Radio Group.
But I want to know is it good practice to keep checkBixes in RadioGroup and how to get the Checkboxes value?
What do you mean by getting CheckBox value? You can get the state (checked or not checked) and you can also get the text label for the CheckBox itself (getText()).
As for keeping them in a RadioGroup, it will depend largely on your use case (when grouped, the user might expect that only one CheckBox at the time can be checked). If you were to implement RadioGroup, you will have to implement a listener and then determine which CheckBox was checked. For that you can look at the accepted answer on this closely related question here.
Here is how you can get the Text of the CheckBox that was checked/unchecked:
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
CheckBox checkBox = (CheckBox)findViewById(checkedId);
//here you can check if it was checked or not, including the text
String text = checkBox.getText().toString();
boolean isChecked = checkBox.isChecked();
}
I hope this sheds some light.
Unfortunately as you would expect to utilize radioGroup's getCheckedRadioButtonId() for radio buttons, there is no such thing for check boxes. There are many ways to do this but I think the simplest and cleanest way would be the following:
For CheckBoxes
// Define one listener to use it for all of your CheckBoxes:
CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// You can also use the buttonView.isChecked() instead of isChecked parameter too : if(ButtonView.isChecked)..
if(isChecked) {
// Do stuff with this checkBox for example:
String stringToShow = buttonView.getText();
}
}
};
// Reference your CheckBoxes to your xml layout:
CheckBox checkBox1 = findViewById(R.id.checkBox1);
CheckBox checkBox2 = findViewById(R.id.checkBox2);
// and many more check boxes..
/* Set the above listener to your Check boxes so they would
notify your above piece of code in case their checked status
changed:*/
checkBox1.setOnCheckedChangeListener(listener);
checkBox2.setOnCheckedChangeListener(listener);
// and many more check boxes..
Two buttons getting selected in the radio group.
I do not know where I am getting wrong. Please help me out.
final RadioGroup rg=new RadioGroup(Survay_MainActivity.this);
rg.clearCheck();
rg.setId(Integer.valueOf(entry1.getKey()));
Log.v("rg getid", "rg"+rg.getId());
for(int i =0;i<values.size();i++){
// Create Button
final RadioButton btn = new RadioButton(Survay_MainActivity.this);
btn.setId(i);
btn.setTextColor(Color.parseColor("#000000"));
btn.setBackgroundColor(Color.TRANSPARENT);
btn.setGravity(Gravity.LEFT);
btn.setText(values.get(i));
rg.addView(btn);
btn.setLayoutParams(params);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
JSONObject quesAns = new JSONObject();
String ans=btn.getText().toString().trim();
try {
quesAns.put(String.valueOf(rg.getId()), ans);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
jsonarray.put(quesAns);
Log.v("jsonarray", "jsonarray"+jsonarray);
}
});
}
views.addView(rg);
1) I am creating the RadioGroup out of the loop.
2) Adding radio button to the RadioGroup in the for loop
3) When the loop finishes the RadioGroup is added to the linerlayout.
You just need to change different ids for different radio button.
There may be some id clash in gen file.
Radio button1 : android:id="#+id/one
Radio button2 : android:id="#+id/two"
Radio button3 : android:id="#+id/three"
Hope this would help.
There are some things misplaced these changes you have to make out.
1.Change your OnClickListener by OnCheckChangeListener
2.Clear check your radioGroup after adding all radioButtons and before adding it in LinearLayout.
Your problem comes from the fact that you are setting an id for your items manually
rg.setId(Integer.valueOf(entry1.getKey()));
btn.setId(i);
Android works with id's generated automatically that are different. Setting the id's manually it might happen that you give the same ID to the radio group and the radio button.
I used your code and set the group to ID 3 and radios from 0 to 4.
Needless to say that after I click the button with id 3 it always stays on because the group has the same id.
So, try to remove the id setting part, or, if you insist make sure you always have distinct id's.
I faced the same problem. The cause is the RadioGroup's id is same as one of RadioButtons
the problem is this line. you didn't send where is this code belonged to. when you get out of this scope and come back you will create new RadioGroup. You should have one and only one RadioGroup so the RadioButtons have same RadioGroup and by default one of them will be selected. To this, you can define RadioGroup in an XML and use this to define variable for it RadioGroup rg = (RadioGroup) findViewById(R.id.YOURNAME); or you can define your RadioGroup as Instance variable(Global).
final RadioGroup rg=new RadioGroup(Survay_MainActivity.this);
I am quite new to Android and this is my first application, so please correct if my question is not clear and I will gladly add more information about what I am trying to achieve.
I have a radio group built dynamically. Inside this radio group I would like to have another radio group, depending on the radio button chosen from the first group.
So, let's say I have an array list of items and for each item I have some sizes available (i.e: XS, S, L). If I check the radio button "XS", I would like to have another radio group with the available colors for the selected size, XS.
The way I have built this is by creating a radio group and it's radio buttons dynamically. Inside the method onCheckedChanged(), I am calling the method createRadioButtonsForAvailableColors(). This one creates the radio buttons with the necessary colors for the checked size, but once I check another size in the upper radio group, the new colors available for this size are added to the colors shown for the size selected before.
Thank you.
Here is my on create method:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_product_details);
sizesList = getSizes(getIDFromPreviousActivity());
createRadioButtonsForAvailableSizes(sizesList);
}
Here is my creation for the radio group containing the radio buttons with the available sizes:
// creates the radio buttons with the available sizes
public void createRadioButtonsForAvailableSizes(ArrayList<String> sizeList) {
productDetailsLayout = (LinearLayout) findViewById(R.id.productDetailsLayout);
RadioGroup rg = new RadioGroup(this);
rg.setOrientation(RadioGroup.HORIZONTAL);
int n = sizeList.size();
final RadioButton[] rb = new RadioButton[n];
for(int i=0; i< sizeList.size(); i++) {
colorList = getColors(getIDFromPreviousActivity(),sizeList.get(i));
rg.setOrientation(RadioGroup.HORIZONTAL);
rb[i] = new RadioButton(this);
rg.addView(rb[i]);
rb[i].setText(sizesList.get(i).toString());
rb[i].setId(getIDForRadioButton(sizesList.get(i).toString()));
rb[i].setButtonDrawable(R.drawable.radiobuttonunchecked);
rb[i].setOnCheckedChangeListener(this);
}
productDetailsLayout.addView(rg);
productDetailsLayout.setPadding(50, 50, 50, 50);
}
Here is the creation of the colors (same as for the sizes):
// create radio buttons for available colors
public void createRadioButtonsForAvailableColors(ArrayList<String> colorList) {
Log.d("createRadioButtonsForAvailableColors","");
productDetailsLayout = (LinearLayout) findViewById(R.id.productDetailsLayout);
RadioGroup rg = new RadioGroup(this);
rg.setOrientation(RadioGroup.HORIZONTAL);
int n = colorList.size();
final RadioButton[] rb = new RadioButton[n];
for(int i=0; i< colorList.size(); i++) {
Log.d("color"+i,colorList.get(i));
rg.setOrientation(RadioGroup.HORIZONTAL);
rb[i] = new RadioButton(this);
rg.addView(rb[i]);
rb[i].setText(colorList.get(i).toString());
rb[i].setId(getIDForRadioButton(colorList.get(i).toString()));
rb[i].setButtonDrawable(R.drawable.radiobuttonunchecked);
rb[i].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override public void onCheckedChanged(CompoundButton button, boolean isChecked) {
button.setButtonDrawable(isChecked ? R.drawable.radiobuttonchecked : R.drawable.radiobuttonunchecked);
}
});
}
productDetailsLayout.addView(rg);
productDetailsLayout.setPadding(50, 50, 50, 50);
}
Here is my onCheckedChange method:
#Override
public void onCheckedChanged(CompoundButton button, boolean isChecked) {
button.setButtonDrawable(isChecked ? R.drawable.radiobuttonchecked : R.drawable.radiobuttonunchecked);
String text = button.getText().toString();
int productID = getIDFromPreviousActivity();
ArrayList<String> colorList = getColors(productID, text);
createRadioButtonsForAvailableColors(colorList);
}
The inner group is created right, but upon checking one size in the first group, it builds the subgroup radio buttons normally. If I click one size, it shows the available colors. But, on changing the size checked, the buttons showing the colors of the now selected size are added to the buttons shown before for the previous size selected. How can I cancel the inner buttons created when I selected first time the size and show only the available colors for the currently selected size?
I think this is maybe not the right approach to build it. So, I have 2 questions:
Usually, what would be the best way to do this? Since my inner radio group depends on the radio button chosen from the first group, I assume the creation of the inner group should be called inside the onCheckedChange() method. Should I start another activity from here or can I do it all in one activity?
If my approach is correct, can you please tell me how to delete the inner radio buttons created, in case the radio button from the main group is changed?
Thank you
So, if I understood this, the problem comes when you change the value of the sizes radio button, meaning the colors don't disappear.
Well, Java is all about a lot of boilerplating, and there's not pretty much to say (I'd probably go for a similar way).
You could add this inside the createRadioButtonsForAvailableColors method, just before you start adding content:
productDetailsLayout.removeViews(0, productDetailsLayout.getChildCount());
RemoveViews will remove any amount of child views starting from a position (hence I amb passing it 0 and the total of children).
I have sucessfully made and used RadioGroup's before in xml, but each time there was a set of radio buttons in succession, all within the same LinearLayout. Now I wish to define a set of radio buttons to be part of a group, but they are not in the same layout. My start and end code for the group is:
START:
<RadioGroup android:id="#+id/radioGroup1" >
END:
</RadioGroup>
If I place this around each button individually, then it compiles, but the buttons don't act as radio buttons (i.e. activating one did not de-activate the others). If I try to place the "start" before any of the buttons and put the "end" after the last of them, then I get compilation errors.
Store the RadioButtons in an array. Instead of grouping them in a RadioGroup, you have to enable/disable them yourself. (un-tested so no copying/pasting )
declare these variables
private ArrayList<RadioButton> mGroup = new ArrayList<RadioGroup>();
private CompoundButton.OnCheckedChangeListener mListener = new CompoundButton.OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
for(RadioButton btn : mGroup)
btn.setChecked(false);
buttonView.setChecked(true);
}
}
Somewhere in your activity:
mGroup.add(your radiobuttons); // e.g. (RadioButton)findViewById(R.id.radio_button1);
mGroup.add(another radiobutton);
for(RadioButton btn : mGroup)
btn.setOnCheckedChangeListener(mListener)
Maybe you have to invalidate your Buttons after checking/unchecking them, to cause a redraw
Unfortunately the SDK does not directly support doing this, you would need to create a custom "group" to manager the buttons.
All the mutual exclusion logic for each RadioButton is managed by RadioGroup, so all the buttons have to be added to the same group. RadioGroup is a subclass of LinearLayout, so this inherently means they also need to all be in the same layout. RadioGroup manages each button's checked status by iterating through its children, which is why the two can't be divorced.
Here is a link to the RadioGroup source code as a starting point, you could create a custom Manager that uses the same logic here to manage the status of which button is checked in order to separate them from their layout status. RadioGroup basically just registers itself as an OnCheckedChangeListener for each child that is a RadioButton and then controls the check status of all the buttons based on user events.
HTH
Here's a refined and tested version of the first part of Entreco's code:
final ArrayList<RadioButton> mGroup = new ArrayList<RadioButton>();
CompoundButton.OnCheckedChangeListener mListener = new CompoundButton.OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
for(RadioButton btn : mGroup) {
if (buttonView.getId() != btn.getId() && isChecked) {
btn.setChecked(false);
}
}
}
};
It will uncheck only the other buttons and only if the state of the clicked button changed from unchecked to checked.
#Entreco answer can cause recursive call to onCheckedChanged which is fixed by #Twilite answer.
But #Twilite answer also has a problem and that's the id of views without explicit id may not be unique. If that happen, you may see either multiple selected radio buttons or none of them being checked. So, it's safer to either set a unique id or a unique tag for each one:
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if (isChecked) {
for (RadioButton btn : mGroup)
if (!btn.getTag().equals(compoundButton.getTag()))
btn.setChecked(false);
}
}