get string of radio button in android

Solutions on MaxInterview for get string of radio button in android by the best coders in the world

showing results for - "get string of radio button in android"
Maya
04 Jun 2018
1import android.app.Activity;
2import android.os.Bundle;
3import android.view.View;
4import android.view.View.OnClickListener;
5import android.widget.Button;
6import android.widget.RadioButton;
7import android.widget.RadioGroup;
8import android.widget.Toast;
9
10public class MyAndroidAppActivity extends Activity {
11
12  private RadioGroup radioGroup;
13  private RadioButton radioButton;
14  private Button btnDisplay;
15
16  @Override
17  public void onCreate(Bundle savedInstanceState) {
18    super.onCreate(savedInstanceState);
19    setContentView(R.layout.main);
20
21    addListenerOnButton();
22
23  }
24
25  public void addListenerOnButton() {
26
27    radioGroup = (RadioGroup) findViewById(R.id.radio);
28    btnDisplay = (Button) findViewById(R.id.btnDisplay);
29
30    btnDisplay.setOnClickListener(new OnClickListener() {
31
32        @Override
33        public void onClick(View v) {
34
35                // get selected radio button from radioGroup
36            int selectedId = radioGroup.getCheckedRadioButtonId();
37
38            // find the radiobutton by returned id
39            radioButton = (RadioButton) findViewById(selectedId);
40
41            Toast.makeText(MyAndroidAppActivity.this,
42                radioButton.getText(), Toast.LENGTH_SHORT).show();
43
44        }
45
46    });
47
48  }
49}