i am a basic learner in android. What i am trying to achieve from my code is that : i want to dynamically change the value of radio buttons of a radio group inside a for loop. Then, grab the text value so that i can compare, lets say if the value is "Take Out", i want the rb1 to be checked. Here is the radio group
<RadioGroup
android:layout_marginTop="20dp"
android:layout_width="fill_parent"
android:layout_height="120dp"
android:layout_alignBottom="@+id/add"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="20dp"
android:id="@+id/rg1">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Take Out"
android:id="@+id/rb1"
android:checked="true"
android:onClick="selectSomething"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sit down"
android:id="@+id/rb2"
android:checked="false" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delivery"
android:id="@+id/rb3"
android:checked="false" />
</RadioGroup>
What i have tried so far:
int count = rg1.getChildCount();
//Make checked if the value is equal
for(int i = 0; i < count;i++){
String myshit = "rb"+i;
int id = getResources().getIdentifier(myshit, "id", getPackageName());
RadioButton RadioValue = (RadioButton) findViewById(id);
String _RadioValue = RadioValue.getText().toString();
Log.d("abc",_RadioValue);
}
For testing, i wanted to print out the values in Log but it does not compile and throws error. I am just half way thorugh in my code. Is there a better way to solve this problem ? Need your expertise. Thank You !