RadioButton Example

RadioButton Example in Android

CLASS FILE

package com.example.malik.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends Activity {

RadioGroup radio_g;
RadioButton radio_b;
Button select;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

addListenerOnButton();

}

private void addListenerOnButton() {

radio_g = (RadioGroup) findViewById(R.id.rgAnimals);
select = (Button) findViewById(R.id.bSelect);

select.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int selected_id = radio_g.getCheckedRadioButtonId();
radio_b = (RadioButton) findViewById(selected_id);

Toast.makeText(getApplicationContext(), radio_b.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
}
}

 

XML FILE

package com.example.malik.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends Activity {

RadioGroup radio_g;
RadioButton radio_b;
Button select;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

addListenerOnButton();

}

private void addListenerOnButton() {

radio_g = (RadioGroup) findViewById(R.id.rgAnimals);
select = (Button) findViewById(R.id.bSelect);

select.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int selected_id = radio_g.getCheckedRadioButtonId();
radio_b = (RadioButton) findViewById(selected_id);

Toast.makeText(getApplicationContext(), radio_b.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
}
}

 

 

 

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.