Below is my activity_main.xml code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingTop="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="300dp"
android:id="@+id/rgr">
<RadioButton
android:id="@+id/rb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Mechanical" />
<RadioButton
android:id="@+id/rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Electrical" />
<RadioButton
android:id="@+id/rb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Civil" />
<RadioButton
android:id="@+id/rb4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Computer Science" />
</RadioGroup>
</LinearLayout>
Below is MainActivity.java code
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
{ // https://www.youtube.com/watch?v=n7c3bIWcgZo
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RadioGroup radioGroup = (RadioGroup)findViewById(R.id.rgr);
RadioButton radioButton1 = (RadioButton)findViewById(R.id.rb1);
RadioButton radioButton2 = (RadioButton)findViewById(R.id.rb2);
RadioButton radioButton3 = (RadioButton)findViewById(R.id.rb3);
RadioButton radioButton4 = (RadioButton)findViewById(R.id.rb4);
if(radioButton1.isChecked())
{
Toast.makeText(getApplicationContext(), radioButton1.getText(),Toast.LENGTH_LONG).show();
}
else if(radioButton2.isChecked())
{
Toast.makeText(getApplicationContext(), radioButton2.getText(),Toast.LENGTH_LONG).show();
}
else if(radioButton3.isChecked())
{
Toast.makeText(getApplicationContext(), radioButton3.getText(),Toast.LENGTH_LONG).show();
}
else if(radioButton4.isChecked())
{
Toast.makeText(getApplicationContext(), radioButton4.getText(),Toast.LENGTH_LONG).show();
}
}
Now, when i run the app, the radio buttons are getting selected but the toast message is not coming when i select particular button.
i have also tried by removing radiogroup and keeping only radio buttons but still i think isChecked() fucntion is not excuting.
Please help me in solving this issue.
You have to set listener
for your RadioButtons, like below.
radioButton1 .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if(isChecked)
Toast.makeText(getApplicationContext(), radioButton1.getText(),Toast.LENGTH_LONG).show();
}
}
);