1

I am wondering when i use radio button and make a different value and text when it show. like html.

Example (Html):

<form action="">
  <input type="radio" name="gender" value="male"> MAN<br>
  <input type="radio" name="gender" value="female"> FEMALE<br>
</form>

Like that, the value is "male" and the text is "MAN", how it will be when i use radio botton on android studio, because i just know this code :

  <RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text"
android:id="@+id/myRadioGroup"
android:background="#abf234"
android:checkedButton="@+id/gender" >

  <RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/male"
    android:text="MALE" />

<RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/female"
    android:text="FEMALE" />

so on the android we just know the text (text will be display on the screen) and the value is same, my question is how if i want get a value like "male" but text show on screen is "MAN" like html.

3 Answers3

0

There is no value for RadioButton in Android.

You can get the text using

myRadioButton.getText(); 

and state checked or not using

myRadioButton.isChecked();

which will return true or false depending on whether your RadioButton is checked or not.

the value is "male" and the text is "MAN"

You can do:

String value = "";
if (rbTkiKG.getText().toString().equals("MAN")) {
    value = "male";
}

but as I have already said this is not quite required.

Nongthonbam Tonthoi
  • 12,667
  • 7
  • 37
  • 64
  • how if i want to make a different between a value and a text? – Rizal Ahmad Dec 15 '17 at 13:35
  • @RizalAhmad `how if i want to make a different between a value and a text?` - I don't get this, Maybe I can help you if I know what you are trying to achieve. If you want to change the text you can use `setText()` method like `setText("My new text");` – Nongthonbam Tonthoi Dec 15 '17 at 13:38
  • look at the html code, the value and the text is different, so i ask how i make a code radio botton like html in android? – Rizal Ahmad Dec 15 '17 at 13:44
  • @RizalAhmad as I wrote in the answer, `RadioButton`'s in Android are different from the one's in html, they don't have this value thing, after all it will as you expect a RadioButton to work, And why do you need the value for? – Nongthonbam Tonthoi Dec 15 '17 at 13:47
  • so your answer is impossible when i want get a value like "male" but text show on screen is "MAN" like html in my question?, i just need because i have a multiple choice of question but when people or user choice it i just need the abbreviation of that answer – Rizal Ahmad Dec 15 '17 at 13:52
  • @RizalAhmad you need to change the way you think of RadioButtons in Android, if you really want that you can get the text and assign a local variable value. – Nongthonbam Tonthoi Dec 15 '17 at 13:57
  • i just ask, so do you think it's impossible? – Rizal Ahmad Dec 15 '17 at 14:04
0

in html we use value and in android we use id instead of Try this code for RadioButton

<RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/text"
    android:id="@+id/myRadioGroup"
    android:background="#abf234"
    android:checkedButton="@+id/gender" >

      <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/male"
        android:text="MALE" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/female"
        android:text="FEMALE" />
</RadioGroup>
0

Try this way.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<RadioGroup
    android:layout_width="fill_parent"
    android:layout_height="90dp"
    android:id="@+id/radioGroup">

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Male"
        android:id="@+id/radioButton"
        android:layout_gravity="center_horizontal"
        android:checked="false"
        android:textSize="25dp" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Female"
        android:id="@+id/radioButton2"
        android:layout_gravity="center_horizontal"
        android:checked="false"
        android:textSize="25dp"/>

  </RadioGroup>
</RelativeLayout>

Hope this Help.

Dharmishtha
  • 1,313
  • 10
  • 21
  • how about the VALUE? i want get a value like "male" but text show on screen is "MAN" like html in my question. – Rizal Ahmad Dec 15 '17 at 13:48
  • you can get value of that radio button by doing below code.`int selectedId = radioGroup.getCheckedRadioButtonId(); // find the radiobutton by returned id radioButton = (RadioButton) findViewById(selectedId); Toast.makeText(MyAndroidAppActivity.this, radioButton.getText(), Toast.LENGTH_SHORT).show();` – Dharmishtha Dec 15 '17 at 13:54
  • i know how to get value of radion botton using id but the issue is i want get a value like "male" but text show on screen is "MAN" like html in my question. – Rizal Ahmad Dec 15 '17 at 13:56
  • why you need value? – Dharmishtha Dec 15 '17 at 13:58
  • i just need because i have a multiple choice of question but when people or user choice it i just need the abbreviation of that answer – Rizal Ahmad Dec 15 '17 at 13:59