How to initialize RadioGroup - android

I need to use RadioGroup in my app. But I can't initialize it in activity. What is the problem here?
my screen for example
screen 2

hi did you try something like this,
RadioGroup mRadioGroup = (RadioGroup) view.findViewById(R.id.amountcollecte_radiogroup);
mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
int id = mRadioGroup.getCheckedRadioButtonId();
View radioButton = mRadioGroup.findViewById(id);
if (radioButton.getId() == R.id.amountcollected_yes_radioButton) {
} else if (radioButton.getId() == R.id.amountcollected_no_radioButton) {
}
}
});
<RadioButton
android:id="#+id/amountcollected_yes_radioButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:buttonTint="#color/black"
android:checked="true"
android:gravity="center"
android:text="Yes"
android:textColor="#color/black"
android:textSize="#dimen/text_large" />
<RadioButton
android:id="#+id/amountcollected_no_radioButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:buttonTint="#color/black"
android:gravity="center"
android:text="No"
android:textColor="#color/black"
android:textSize="#dimen/text_large" />
</RadioGroup>
hope this will help you.

Related

ListView setOnItemClickListener not working when ListView row contain RadioButton

Please do not say that this is duplicate question, because i search on google and try every answer then also not getting result. In my Listview row contain one TextView and three Radio button.
Problem is
When i click on other side in particular row then Listview setOnItemClickListener work fine but problem is when i click on radio button then setOnItemClickListener not called i don't know why ?
I want that when i click or select any redioButton then call one method, I know that this is possible by adapter but i want radio button click event in listview onItemClick() method.
Sorry for bad english, Thanks in advance and help would be appreciate
xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:padding="5dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:background="#drawable/student_row_background">
<TextView
android:id="#+id/tv_attendance_studentName"
android:layout_width="0dp"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_weight=".5"
android:gravity="center_vertical"
android:text="Name"
android:textColor="#color/headerActionbar"
android:textSize="15dp"
android:focusable="false"
android:focusableInTouchMode="false"/>
<RadioGroup
android:id="#+id/rg_1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".5"
android:orientation="horizontal"
android:gravity="center|right"
android:focusable="false"
android:focusableInTouchMode="false"
android:descendantFocusability="blocksDescendants"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="21dp"
android:text="P"
android:id="#+id/radioButton_present"
android:textColor="#color/headerActionbar"
android:textStyle="bold"
android:gravity="center"
android:paddingRight="10dp"
android:textSize="12dp"
android:buttonTint="#color/headerActionbar"
android:focusable="false"
android:focusableInTouchMode="false" />
<View
android:layout_width="1dp"
android:layout_height="21dp"
android:background="#color/dialogTextColor"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="21dp"
android:text="A"
android:textColor="#color/headerActionbar"
android:textStyle="bold"
android:paddingRight="10dp"
android:gravity="center"
android:textSize="12dp"
android:id="#+id/radioButton_absent"
android:buttonTint="#color/headerActionbar"
android:focusable="false"
android:focusableInTouchMode="false"/>
<View
android:layout_width="1dp"
android:layout_height="21dp"
android:background="#color/dialogTextColor"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="21dp"
android:text="L"
android:textColor="#color/headerActionbar"
android:textStyle="bold"
android:paddingRight="10dp"
android:gravity="center"
android:textSize="12dp"
android:id="#+id/radioButton_leave"
android:buttonTint="#color/headerActionbar"
android:focusable="false"
android:focusableInTouchMode="false"/>
</RadioGroup>
</LinearLayout>
</RelativeLayout>
This is java file
lv_studentList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
RadioButton radioButton_present = (RadioButton)view.findViewById(R.id.radioButton_present);
RadioButton radioButton_absent = (RadioButton)view.findViewById(R.id.radioButton_absent);
RadioButton radioButton_leave = (RadioButton)view.findViewById(R.id.radioButton_leave);
if (!radioButton_present.isChecked() && !radioButton_absent.isChecked()
&& !radioButton_leave.isChecked())
{
constant.showSnackBar("Please select at least one attendance option");
}
else if(radioButton_present.isChecked())
{
constant.showSnackBar("Student is present");
}
else if (radioButton_absent.isChecked())
{
constant.showSnackBar("Student is absent");
}
else if (radioButton_leave.isChecked())
{
constant.showSnackBar("Student is leave");
}
}
});
Use View parameter of onItemClick() method to find the RadioGroup or RadioButton view. Then use that view as per requirement.
#Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
RadioGroup group = group = (RadioGroup) view.findViewById(R.id.rg_1);
final RadioButton radioButton_present = (RadioButton)view.findViewById(R.id.radioButton_present);
final RadioButton radioButton_absent = (RadioButton)view.findViewById(R.id.radioButton_absent);
final RadioButton radioButton_leave = (RadioButton)view.findViewById(R.id.radioButton_leave);
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
if (!radioButton_present.isChecked() && !radioButton_absent.isChecked()
&& !radioButton_leave.isChecked())
{
Toast.makeText(MainActivity.this,"Please select at least one attendance option",Toast.LENGTH_LONG).show();
}
else if(radioButton_present.isChecked())
{
Toast.makeText(MainActivity.this,"Student is present",Toast.LENGTH_LONG).show();
}
else if (radioButton_absent.isChecked())
{
Toast.makeText(MainActivity.this,"Student is absent",Toast.LENGTH_LONG).show();
}
else if (radioButton_leave.isChecked())
{
Toast.makeText(MainActivity.this,"Student is leave",Toast.LENGTH_LONG).show();
}
}
});
}

How to set only one RadioButton Can be selected at the time in RadioGroup

I've created a radio button in radiogroup, but when I try running the apps all radio button can be selected all the time, and how to set only one radiobutton can be selected at one time?
I'm using Fragment
RadioGroup radioGroup = (RadioGroup) rootView.findViewById(R.id.RGroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// find which radio button is selected
if(checkedId == R.id.Abdominal) {
Toast.makeText(getActivity().getApplicationContext(), "choice: A",
Toast.LENGTH_SHORT).show();
} else if(checkedId == R.id.Arm) {
Toast.makeText(getActivity().getApplicationContext(), "choice: B",
Toast.LENGTH_SHORT).show();
} else if(checkedId == R.id.Back){
Toast.makeText(getActivity().getApplicationContext(), "choice: C",
Toast.LENGTH_SHORT).show();
} else if(checkedId == R.id.Chest){
Toast.makeText(getActivity().getApplicationContext(), "choice: D",
Toast.LENGTH_SHORT).show();
} else if(checkedId == R.id.Leg){
Toast.makeText(getActivity().getApplicationContext(), "choice: E",
Toast.LENGTH_SHORT).show();
} else if(checkedId == R.id.Shoulder){
Toast.makeText(getActivity().getApplicationContext(), "choice: F",
Toast.LENGTH_SHORT).show();
}
}
});
here my xml code for RG and RB
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/RGroup">
<TableRow android:weightSum="1">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Abdominal"
android:id="#+id/Abdominal"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Arm"
android:id="#+id/Arm"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:id="#+id/Back" />
</TableRow>
<TableRow>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chest"
android:id="#+id/Chest"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Leg"
android:id="#+id/Leg"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Shoulder"
android:id="#+id/Shoulder"/>
</TableRow>
</RadioGroup>
EDITED 1 : Answer :
If you dont want radio button can be selected in one time, so dont use Tablerow
It's not working because of TableRow inside RadioGroup. All RadioButtons are not grouped together because of TableRow between them.
RadioButton should be the direct child of RadioGroup, Otherwise grouping does not work.
Just change your code like this it will work :
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/RGroup">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Abdominal"
android:id="#+id/Abdominal"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Arm"
android:id="#+id/Arm"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:id="#+id/Back" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chest"
android:id="#+id/Chest"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Leg"
android:id="#+id/Leg"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Shoulder"
android:id="#+id/Shoulder"/>
</RadioGroup>
Hope this helps. :)
I have noticed that single selection does not work without setting id to radio buttons.
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/expenseRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/expense" />
<RadioButton
android:id="#+id/incomeRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/income" />
</RadioGroup>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:id="#+id/radioGroup">
<RadioButton
android:layout_width="0dp"
android:layout_weight="50"
android:layout_height="wrap_content"
android:text="Debit"
android:id="#+id/rDebit"
android:checked="false"
/>
<RadioButton
android:layout_width="0dp"
android:layout_weight="50"
android:layout_height="wrap_content"
android:text="Credit"
android:id="#+id/rCredit"
android:checked="false" />
</RadioGroup>
And in java file
RadioGroup radioGroup;
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
And when to do something
if (radioGroup.getCheckedRadioButtonId() == R.id.rCredit)
{
// do something
}
I encountered the same issue and found where I was making mistakes, i'm describing it below:
If you are using RadioButtons in RadioGroup, then RadioButtons should be Direct Child of RadioGroup.
Check first condition, if it is correct then give id to RatioButton and run project, It will solve the problem automatically.
Simple way. onclick of radio button. do code as per below.
public void clearRadioChecked() {
rdopa.setChecked(false);
rdopb.setChecked(false);
rdopc.setChecked(false);
rdopd.setChecked(false);
}
if you wann select rdopa then on click of rdopa do as below.
clearRadioChecked()
rdopa.setChecked(true);
You can use android:checkedButton attribute on RadioGroup, providing the id of the RadioButton you want to be checked initially and selecting another RadioButton will clear the previous selection.
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkedButton="#+id/rbNo"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="50dp"
android:text="Yes" />
<RadioButton
android:id="#+id/rbNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No" />
</RadioGroup>
You can create your own RadioGroup, that is able to find all RadioButtons that are nested in your group regardless of direct child or not. Below the code of my NestedRadioGroup. Using this instead of your RadioGroup in your xml file should do the trick.
public class NestedRadioGroup extends LinearLayout {
private SparseArray<RadioButton> radioButtons;
private int checkedId;
public NestedRadioGroup(Context context) {
this(context, null);
}
public NestedRadioGroup(Context context, AttributeSet attrs) {
super(context, attrs);
radioButtons = new SparseArray<>();
checkedId = -1;
}
#Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
super.addView(child, index, params);
findRadioButtons(child);
}
private void findRadioButtons(View child) {
if (child instanceof RadioButton) {
RadioButton newButton = (RadioButton) child;
newButton.setId(radioButtons.size());
newButton.setOnCheckedChangeListener((buttonView, isChecked) -> {
if (isChecked) {
if (checkedId != -1) {
radioButtons.get(checkedId).setChecked(false);
}
radioButtons.get(buttonView.getId()).setChecked(true);
checkedId = buttonView.getId();
}
});
radioButtons.put(newButton.getId(), newButton);
} else if (child instanceof ViewGroup) {
ViewGroup group = (ViewGroup) child;
for (int i = 0; i < group.getChildCount(); i++) {
this.findRadioButtons(group.getChildAt(i));
}
}
}
}
Any ideas to make the code cleaner?
I hope it works out for you :)
We can use any layout like LinearLayout, RelativeLayout, ConstraintLayout or GridLayout as RadioGroup using the following method in Kotlin.
For the following example I used ConstraintLayout to get fixed width and height for RadioButton.
layout.xml
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="#+id/rbYellow"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="#dimen/space_2x"
android:layout_marginEnd="#dimen/space"
android:gravity="center"
android:text="Yellow"
android:textColor="#android:color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toStartOf="#+id/rbOrange"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<RadioButton
android:id="#+id/rbOrange"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="#dimen/space"
android:layout_marginEnd="#dimen/space"
android:gravity="center"
android:text="Orange"
android:textColor="#android:color/white"
app:layout_constraintBottom_toBottomOf="#id/rbYellow"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toStartOf="#+id/rbRed"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/rbYellow"
app:layout_constraintTop_toTopOf="#id/rbYellow" />
<RadioButton
android:id="#+id/rbRed"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="#dimen/space"
android:layout_marginEnd="#dimen/space"
android:gravity="center"
android:text="Red"
android:textColor="#android:color/white"
app:layout_constraintBottom_toBottomOf="#id/rbYellow"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toStartOf="#+id/rbGreen"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/rbOrange"
app:layout_constraintTop_toTopOf="#id/rbYellow" />
<RadioButton
android:id="#+id/rbGreen"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="#dimen/space"
android:layout_marginEnd="#dimen/space_2x"
android:gravity="center"
android:text="Green"
android:textColor="#android:color/white"
app:layout_constraintBottom_toBottomOf="#id/rbYellow"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/rbRed"
app:layout_constraintTop_toTopOf="#id/rbYellow" />
</androidx.constraintlayout.widget.ConstraintLayout>
Kotlin code
radioGroup.children.forEach { aBtn ->
(aBtn as RadioButton).setOnCheckedChangeListener { button, isChecked ->
if(isChecked) {
radioGroup.children.forEach { bBtn ->
if(bBtn.id != button.id) {
(bBtn as RadioButton).isChecked = false
}
}
}
}
}

Radio Button checking and unchecking programmatically in android

My requirement is to be able to be able to check only 1 radio button from the no of options available.
My layout is simple
User should be able to select only one of p1, p2 and p3.
The IDs for for the buttons are rB1,rB2 and rB3.
PS: I know it is possible to achieve the same with RadioGroup , but in this particular instance I want to go with Radio Button hence the query
Try this..
r1.setOnCheckedChangeListener(this);
r2.setOnCheckedChangeListener(this);
r3.setOnCheckedChangeListener(this);
And CheckedChangeListener
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
if (buttonView.getId() == R.id.rB1) {
r2.setChecked(false);
r3.setChecked(false);
}
if (buttonView.getId() == R.id.rB2) {
r1.setChecked(false);
r3.setChecked(false);
}
if (buttonView.getId() == R.id.rB3) {
r1.setChecked(false);
r2.setChecked(false);
}
}
}
and don't forget to implements OnCheckedChangeListener in your activity or fragment.
Try this way:
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/p1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="P1" />
<RadioButton
android:id="#+id/p2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="P2" />
<RadioButton
android:id="#+id/p3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="P3" />
</RadioGroup>
Use Radio group
XML:
<RadioGroup
android:id="#+id/radiogroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/rB1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="P1"
android:checked="true" />
<RadioButton
android:id="#+id/rB2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="P2" />
<RadioButton
android:id="#+id/rB3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="P3" />
</RadioGroup>
try below code:-
java
public class MyAndroidAppActivity extends Activity {
private RadioGroup radioSexGroup;
private RadioButton radioSexButton;
private Button btnDisplay;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);
btnDisplay = (Button) findViewById(R.id.btnDisplay);
btnDisplay.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// get selected radio button from radioGroup
int selectedId = radioSexGroup.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioSexButton = (RadioButton) findViewById(selectedId);
Toast.makeText(MyAndroidAppActivity.this,
radioSexButton.getText(), Toast.LENGTH_SHORT).show();
}
});
}
}
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" >
<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>
<Button
android:id="#+id/btnDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/btn_display" />
</LinearLayout>
for more detail see below link:-
http://www.mkyong.com/android/android-radio-buttons-example/
http://www.tutorialspoint.com/android/android_radiogroup_control.htm
The solution can be achieved by setting a android:onClick="onRadioButtonClick" in the layout.xml file and creating a onRadioButtonClick method in Activity.java
public void onRadioButtonClick(View v)
{
boolean checked = ((RadioButton) v).isChecked();
RadioButton r1 = (RadioButton) findViewById(R.id.rB1);
RadioButton r2 = (RadioButton) findViewById(R.id.rB2);
RadioButton r3 = (RadioButton) findViewById(R.id.rB3);
switch(v.getId()) {
case R.id.rB1:
if (checked){
r2.setChecked(false);
r3.setChecked(false);
}
break;
case R.id.rB2:
if (checked){
r1.setChecked(false);
r3.setChecked(false);
}
break;
case R.id.rB2:
if (checked){
r1.setChecked(false);
r2.setChecked(false);
}
break;
}
I am posting this soulution because I couldn't find the solution to this query on this site(when I was looking for it earlier in the day) and had to figure it out myself. This might be useful for someone else looking for a similar solution.

RadioButton does not switch from "true" to "false"

I have a problem with my RadioButton which I declerate in my activity_main.xml:
<RadioButton
android:id="#+id/RadioButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/RadioButton00"
android:layout_alignBottom="#+id/RadioButton00"
android:layout_marginLeft="9dp"
android:layout_toRightOf="#+id/RadioButton00" />
<RadioButton
android:id="#+id/RadioButton02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/RadioButton01"
android:layout_alignBottom="#+id/RadioButton01"
android:layout_marginLeft="7dp"
android:layout_toRightOf="#+id/RadioButton01" />
Some of this RadioButtons are set programaticly to "true". But the user should have the right to make it to "false" if he clicks on it. At the moment it doesn't. If I click on it, it just still be true and does not switch to false.
public class MainActivity extends Activity {
private RadioButton RB1, RB2;
int[][] RBTrue = new int[3][16];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RB1 = (RadioButton) findViewById(R.id.RadioButton00);
RB2 = (RadioButton) findViewById(R.id.RadioButton01);
}
public void setRadioButtons()
{
steine sander = new steine();
sander.setSteine();
RBTrue = sander.getSteine();
if(RBTrue[0][0] == 1)
RB1.setChecked(true);
if(RBTrue[0][1] == 1)
RB2.setChecked(true);
}
So it's pretty easy. I gonna give an array and check it if it's 1 or not. If it is the RadioButton switching to true. But sometimes the user need to switch it back to false. At the moment it's not possible.
Declare the RadioButtons inside a RadioGroup(details here).
<RadioGroup
android:id="#+id/radio_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/RadioButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="...."
android:textSize="15sp" />
<RadioButton
android:id="#+id/RadioButton02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="....."
android:textSize="15sp" />
</RadioGroup>
then inside activity,,,
RadioGroup rgGroup=(RadioGroup)findViewById(R.id.radio_group);
int selectedId = rgGroup.getCheckedRadioButtonId();
RadioButton rBtn1 =(RadioButton)findViewById(selectedId);
Toast.makeText(MyAndroidAppActivity.this,
rBtn1.getText(), Toast.LENGTH_SHORT).show();

Radio Button is selected automatically in android

quiz_list.xml : My xml file
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/question"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#43bd00"
android:textSize="20sp"
android:textStyle="bold"
android:paddingTop="4dip"
android:paddingBottom="1dip" />
<RadioGroup
android:id="#+id/radioGroup"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Radio Group"
>
<RadioButton
android:id="#+id/option1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RadioButton
android:id="#+id/option2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RadioButton
android:id="#+id/option3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RadioButton
android:id="#+id/option4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RadioGroup>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:gravity="bottom|center_horizontal"
android:orientation="horizontal" >
<Button
android:id="#+id/loadPrevious"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_marginTop="10dp"
android:text="Load Previous"
android:visibility="invisible" />
<Button
android:id="#+id/loadNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_marginTop="10dp"
android:text="Load Next" />
</LinearLayout>
I have a list of questions and radio buttons have the options for it.
The problems that I face are
1. When i load the next list a button is already selected.
2. When i load the previous list a different button is selected instead of the one i selected.
This is my code containing the questions as a json.
String json = "[{\"question\": \"What?\", \"option1\": \"alpha\" , \"option2\": \"beta\" , \"option3\": \"gamma\" , \"option4\": \"alpha\"},{\"question\": \"What is your name ?\", \"option1\": \"Dinesh\" , \"option2\": \"Boopesh\" , \"option3\": \"Srinath\" , \"option4\": \"JK\"},{\"question\": \"What is the capital of India?\", \"option1\": \"Delhi\" , \"option2\": \"Bombay\" , \"option3\": \"Calcutta\" , \"option4\": \"Chennai\"}]";
Gson gson = new Gson();
JsonParser parser = new JsonParser();
JsonArray array = parser.parse(json).getAsJsonArray();
for(JsonElement obj : array) {
Quiz quiz = gson.fromJson(obj,Quiz.class);
String question = quiz.getQuestion();
System.out.println("Question: "+question);
quizList.add(quiz);
}
I'm adding the questions using a customadapter.
How can i change it to make it work ??
Any Ideas ??? Pls let me know...
Thanks Nishit.
try this
RadioGroup radioGroup;
RadioButton radioButton1;
RadioButton radioButton2;
RadioButton radioButton3;
boolean hack = false;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioButton1 = (RadioButton) findViewById(R.id.r1);
radioButton2 = (RadioButton) findViewById(R.id.r2);
radioButton3 = (RadioButton) findViewById(R.id.r3);
OnClickListener radioClickListener = new OnClickListener()
{
public void onClick(View v)
{
if (v.getId() == radioGroup.getCheckedRadioButtonId() && hack)
{
radioGroup.clearCheck();
}
else
{
hack = true;
}
}
};
OnCheckedChangeListener radioCheckChangeListener = new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
hack = false;
}
};
radioButton1.setOnCheckedChangeListener(radioCheckChangeListener);
radioButton2.setOnCheckedChangeListener(radioCheckChangeListener);
radioButton3.setOnCheckedChangeListener(radioCheckChangeListener);
radioButton1.setOnClickListener(radioClickListener);
radioButton2.setOnClickListener(radioClickListener);
radioButton3.setOnClickListener(radioClickListener);
}

Categories

Resources