3

my problem is that I want a Radio Group that has 3 Radio Buttons, using the scheme below. The three choices are: 1. [] Male 2. [] Female 3. [] Custom: (self-described identity)

However, the problem is that I want the user to type in their self-described identity into an EditText for me to retrieve.

So the following code is from my XML page, with some elements blocked out by "####".

<RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="####"
    android:id="@+id/male_female_custom_choice"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true">
    <RadioButton android:id="@+id/radio_button_male"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_button_male"
        android:checked="true" />
    <RadioButton android:id="@+id/radio_button_female"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_button_female"
        android:checked="false" />
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="####"
        android:weightSum="1">
            <RadioButton
                android:id="@+id/radio_button_custom"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/radio_button_custom"
                android:checked="false" />
            <EditText
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:inputType="text"
                android:ems="10"
                android:id="####"
                android:hint="####"
                android:focusableInTouchMode="true"
                android:gravity="center"
                android:layout_weight="1.05"
                android:textSize="14sp" />
            <TextView
                android:layout_width="42dp"
                android:layout_height="43dp"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="####"
                android:id="####"
                android:singleLine="true"
                android:gravity="center"
                android:layout_marginLeft="0dp"
                android:textColor="#000000" />
    </LinearLayout>
</RadioGroup>

As you can see, I have tried to use a LinearLayout to isolate the custom option. However, there are unintended and undesired side effects. 1. The custom option can be selected in addition to the other 2 predefined genders. 2. The custom option cannot be selected on its own.

In the actual Java file for the activity, I have the following code:

// button, radio button, editText, and Spinner fields
public EditText mEdit;
public RadioButton rButton;
public RadioGroup rSexGroup;
rSexGroup = (RadioGroup)findViewById(R.id.male_female_custom_choice);
// get selected radio button from RadioGroup
int selectedId = rSexGroup.getCheckedRadioButtonId();
// find radio button by returned id
rButton = (RadioButton)findViewById(selectedId);
// assign gender based on id of radio button
if (selectedId == 1) {
    pat.gender = "male";
}
if (selectedId == 2) {
    pat.gender = "female";
}
if (selectedId == 3) {
    mEdit = (EditText)findViewById(R.id.####);
    pat.gender = (mEdit.getText().toString());
}

Since I am a bit rusty with Java, it may be possible that I have some really newbish errors. Please advise.

Once again, I am looking for a way to get a set of 3 RadioButtons, each on an individual line, with the last RadioButton with an EditText adjacent to it from which I obtain the desired information.

EDIT: Here's a picture of what I want it to look like: (http://i68.tinypic.com/ao2oow.png)

Unfortunately I need 10 reputation to post images. :(

Mohit's answer gives the EditText on a different line than the custom input. (http://i63.tinypic.com/68u88x.png)

Please note that the orientation of the EditText is adjacent to the custom, and not below. I apologize for not clearly specifying enough what I wanted.

DLuciferin
  • 71
  • 2
  • 7

2 Answers2

4
  1. Because selectedId will not be 1,2 or 3....debug it you will get value..

The custom option cannot be selected on its own.

  1. Remove your 3rd RadioButton from LinearLayout and replace below 2nd RadioButton and put your EditText and TextView inside LinearLayout..

On you listener get getCheckedRadioButtonId and getText() of that RadioButton and check it accordingly...

I dont no what is your task but here is how can get all three RadioButton working and get custom text too....

xml...

UPDATE

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

 <RadioGroup
    android:id="@+id/male_female_custom_choice"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <RadioButton
        android:id="@+id/radio_button_male"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="Male" />

    <RadioButton
        android:id="@+id/radio_button_female"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="false"
        android:text="FeMale" />

    <RadioButton
        android:id="@+id/radio_button_custom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="false"
        android:text="Custom" />
</RadioGroup>

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/male_female_custom_choice"
    android:layout_toRightOf="@+id/male_female_custom_choice"
    android:orientation="horizontal"
    android:weightSum="1" >

    <EditText
        android:id="@+id/edit"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".8"
        android:ems="10"
        android:focusableInTouchMode="true"
        android:gravity="center"
        android:hint="aa"
        android:inputType="text"
        android:textSize="14sp" />

    <TextView
        android:id="@+id/text"
        android:layout_width="42dp"
        android:layout_height="43dp"
        android:layout_marginLeft="0dp"
        android:layout_weight=".2"
        android:gravity="center"
        android:singleLine="true"
        android:text="aaa"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000" />
 </LinearLayout>

 <Button
    android:id="@+id/but"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/male_female_custom_choice"
    android:text="Get" />

</RelativeLayout>

.java file..

public class MainActivity extends Activity {

 public EditText mEdit;
 public RadioButton rButton;
 public RadioGroup rSexGroup;
 public Button but;
 public String str = "";

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    rSexGroup = (RadioGroup)findViewById(R.id.male_female_custom_choice);
    but= (Button) findViewById(R.id.but);

    but.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            int selectedId = rSexGroup.getCheckedRadioButtonId();
            rButton = (RadioButton)findViewById(selectedId);
            if (rButton.getText().toString().equals("Male")) {
                str = "Male";
            }
            if (rButton.getText().toString().equals("FeMale")) {
                str = "FeMale";
            }
            if (rButton.getText().toString().equals("Custom")) {
                mEdit = (EditText)findViewById(R.id.edit);
                str = mEdit.getText().toString();
            }
            Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
        }
    });
  }
}

you can also set visibility of LinearLayout so that it only visible when custom in checked....

Hope it help..

Iamat8
  • 3,888
  • 9
  • 25
  • 35
  • I can understand the reasoning behind the java code and it is very helpful. However, the XML code does not suit my purposes (I have attached pictures to show what I want, and compared to what is displayed from your XML code). Is there a way to adjust the code and move the elements around so that it is more compact like my desired display? – DLuciferin Feb 10 '16 at 19:51
  • The problem with the RelativeLayout seems to be that I can't put the EditText on the same line as the "Custom" RadioButton. I was looking for a way to put them on the same line. – DLuciferin Feb 10 '16 at 19:54
  • you can achieve you want (in picture) by putting LinearLayout Rightof Radiobutton and alignBottom with it.... – Iamat8 Feb 10 '16 at 20:50
  • yes, thank you. I've marked it with a check mark. I have a different problem though that's related... if I add more info/text to the Male/Female Options, the space provided for the EditText decreases, which is something I don't really want. [] Male (****************info***************) << line 1 [] Female (********* more info **********) << line 2 [] Custom (____edittext____)(**info**) << line 3 is what I'm aiming for. However, since you've answered my question and resolved one of the problems listed, I gave you the green check mark. :) – DLuciferin Feb 11 '16 at 19:20
  • Yes, that is correct but the textview forces a lack of space for the edittext. To be precise, I get an onMeasure error even though I modify the text size to be small. – DLuciferin Feb 11 '16 at 20:33
  • you might be having large text – Iamat8 Feb 11 '16 at 20:35
  • If I could make the LinearLayout a bit smaller, I might be able to fit it in. – DLuciferin Feb 11 '16 at 20:38
  • you can define the width,height? – Iamat8 Feb 11 '16 at 20:39
  • Yes, I got exactly what I was looking for now, thank you for your help and patience! – DLuciferin Feb 11 '16 at 20:52
1
  • put radio group in RelativeLayout
  • set third radiobutton text as empty/null
  • add EditText as layout_alignParentBottom="true"
  • now programmatically call EditText's onFocusChangeListener

    edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    
        @Override
    
        public void onFocusChange(View view, boolean b) {
            if(b)
              thirdRadio.setCheched(true);
        }
    });
    
Matthias Bö
  • 449
  • 3
  • 12
Aks4125
  • 4,522
  • 4
  • 32
  • 48