How to use RadioButton in Android import android.widget.Button; import android.widget.RadioButton; import android.widget.RadioGroup; i...

Radio Button in Android

How to use RadioButton in Android

import android.widget.Button;

import android.widget.RadioButton;

import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private RadioGroup radioGroup;
    private RadioButton radioButton;
    private Button button;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button)findViewById(R.id.button2);
        radioGroup = (RadioGroup) findViewById(R.id.gender);

        button.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                int select_id = radioGroup.getCheckedRadioButtonId();
                radioButton = (RadioButton)findViewById(select_id);

                Toast.makeText(MainActivity.this,radioButton.getText().toString(),Toast.LENGTH_LONG).show();

            }        
});    
}
}



0 comments :