Differentiate between two radio groups in same XML layout



I am having two radio group inside same xml layout. I am using oncheckedChanged listener in my MainActivity.java file. I want to differentiate between the two Radio groups using its listener. How to make use of parameter of RadioGroup Inside the Listener.



@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {

switch (checkedId) {

case R.id.rexcellent:

Toast.makeText(getApplicationContext(), "execellnt",
Toast.LENGTH_SHORT).show();

break;
case R.id.rverygood:
Toast.makeText(getApplicationContext(), "very good",
Toast.LENGTH_SHORT).show();

break;
case R.id.rgood:
Toast.makeText(getApplicationContext(), "good", Toast.LENGTH_SHORT)
.show();

break;
case R.id.raverage:
Toast.makeText(getApplicationContext(), "average",
Toast.LENGTH_SHORT).show();

break;
case R.id.rmale:
Toast.makeText(getApplicationContext(), "Male",
Toast.LENGTH_SHORT).show();

break;
case R.id.rfemale:
Toast.makeText(getApplicationContext(), "female",
Toast.LENGTH_SHORT).show();

break;

}

}


here is my Xml code :



<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rate Smartherd Tutorials ?"
android:textAppearance="?android:attr/textAppearanceLarge" />

<RadioGroup
android:id="@+id/rgroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<RadioButton
android:id="@+id/rexcellent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Excellent" />

<RadioButton
android:id="@+id/rverygood"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Very Good" />

<RadioButton
android:id="@+id/rgood"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Good" />

<RadioButton
android:id="@+id/raverage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Average" />
</RadioGroup>

<TextView
android1:id="@+id/textView2"
android1:layout_width="wrap_content"
android1:layout_height="wrap_content"
android1:text="How is shreks ?" />

<RadioGroup
android1:id="@+id/rg2"
android1:layout_width="wrap_content"
android1:layout_height="wrap_content" >

<RadioButton
android1:id="@+id/rmale"
android1:layout_width="wrap_content"
android1:layout_height="wrap_content"
android1:checked="true"
android1:text="MALE" />

<RadioButton
android1:id="@+id/rfemale"
android1:layout_width="wrap_content"
android1:layout_height="wrap_content"
android1:text="Female" />

</RadioGroup>

</LinearLayout>


I want to use switch case statement only. And want to know the use of having "RadioGroup group " inside the listener. What is the role of that?


No comments:

Post a Comment