1
votes

I have two radio buttons i.e., call and email. When call is selected then the edit box should get enabled and then user may enter the mobile number. I've checked these links: Enable a text box only when 1 of the radio button is clicked

Enable text box based on radio button selected

Making Text box not editable

but am not able to achieve this task.

Here is my code:

     <LinearLayout 
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:layout_marginRight="10dp"
     android:layout_marginLeft="10dp"
     android:layout_marginTop="10dp"  >

            <RadioGroup
            android:id="@+id/radioGroup1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="2" 
            android:orientation="horizontal">

             <RadioButton
             android:id="@+id/radio0"
             android:layout_width="0dp"
             android:layout_height="wrap_content" 
             android:layout_weight="1"
             android:text="@string/call"/>

             <RadioButton
             android:id="@+id/radio1"
             android:layout_width="0dp"
             android:layout_height="wrap_content"
             android:text="@string/email"
             android:layout_weight="1" />
             </RadioGroup>
             </LinearLayout>

    <LinearLayout 
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:layout_marginRight="10dp"
     android:layout_marginLeft="10dp"
     android:layout_marginTop="10dp"
     android:weightSum="1">

    <TextView
        android:id="@+id/textView21"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/mobile"
        android:textColor="#000000"
        android:textSize="15sp"
        android:layout_weight="0.3" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="numberDecimal"
        android:maxLength="10"
        android:layout_weight="0.7"
        android:inputType="none" />
    </LinearLayout> 

.java

public class MainActivity extends Activity {
String mobile;

RadioGroup rg1 = (RadioGroup) findViewById(R.id.radioGroup1);
EditText edit = (EditText) findViewById(R.id.editText1);

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

    edit.setEnabled(false);
    edit.setInputType(InputType.TYPE_NULL);
    edit.setFocusable(false);

    rg1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId) {
            case R.id.radio0:   
                op1="call";
                actv(); 
                break;

            case R.id.radio1:
                op1="email";
                break;

            }   

        }
    });

    public void actv() {
    edit.setEnabled(true);
        edit.setInputType(InputType.TYPE_CLASS_TEXT);
        edit.setFocusable(true);
    }
4
Can you define not working? what happens with this code?MysticMagicϡ
Where are you defining rg1 in java??Syeda Zunaira
@Zoya RadioGroup rg1 = (RadioGroup) findViewById(R.id.radioGroup1); included this before onCreate method @ MagicalPhoenixϡ Not working means the screen is not displayed..this is my 1st screen so as soon as i start the app it says "the application has stopped unexpectedly..please try again.."Prabs
please post your logcatSyeda Zunaira
sorry @zoya...i am downloading .apk and testing..log cat is not generated that's why am not able to trace out the errorPrabs

4 Answers

4
votes

Besides correcting your member initialization (as suggested in each of the previous answers), you need to fix your layout (it is closed too early, resulting in an invalid xml).

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

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:orientation="horizontal"
        android:weightSum="2" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/call" />

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:checked="true"
            android:text="@string/email" />
    </RadioGroup>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:weightSum="1" >

        <TextView
            android:id="@+id/textView21"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.3"
            android:text="@string/mobile"
            android:textColor="#000000"
            android:textSize="15sp" />

        <EditText
            android:id="@+id/editText1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.7"
            android:ems="10"
            android:maxLength="10" />
    </LinearLayout>

</LinearLayout>

Also decide which way you'd like to prevent the user from typing into the edittext (disable / hide / input type tweaks).

And last but not least, put all of these together in a clean manner:

public class MainActivity extends Activity implements OnCheckedChangeListener
{
    private String op1;

    private RadioGroup rg1;
    private EditText edit;

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

        rg1 = (RadioGroup) findViewById(R.id.radioGroup1);
        rg1.setOnCheckedChangeListener(this);

        edit = (EditText) findViewById(R.id.editText1);
        actv(false);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId)
    {
        switch (checkedId)
        {
            case R.id.radio0:
                op1 = "call";
                actv(true);
                break;

            case R.id.radio1:
                op1 = "email";
                actv(false);
                break;
        }
    }

    private void actv(final boolean active)
    {
        edit.setEnabled(active);
        if (active)
        {
            edit.requestFocus();
        }
    }
}
0
votes

@Zoya RadioGroup rg1 = (RadioGroup) findViewById(R.id.radioGroup1); included this before onCreate method @ MagicalPhoenixϡ Not working means the screen is not displayed..this is my 1st screen so as soon as i start the app it says "the application has stopped unexpectedly..please try again.."

It's giving crash.

REASON:

RadioGroup rg1 = (RadioGroup) findViewById(R.id.radioGroup1);
EditText edit = (EditText) findViewById(R.id.editText1);

You have included these before onCreate method.

This won't work, obviously.

Add that after your setContentView call inside onCreate method.

Hope it helps.

0
votes

This is What you need to do

Change this

RadioGroup rg1 = (RadioGroup) findViewById(R.id.radioGroup1);
EditText edit = (EditText) findViewById(R.id.editText1);

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

to this

RadioGroup rg1 = null;
EditText edit = null;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    rg1 = (RadioGroup) findViewById(R.id.radioGroup1);
    edit = (EditText) findViewById(R.id.editText1);
}

Thanks :)

0
votes

add:

rg1 = (RadioGroup) findViewById(R.id.radioGroup1);
edit = (EditText) findViewById(R.id.editText1);

after:

setContentView(R.layout.activity_main);

in your onCreate();

Also make sure to set edit.setEditable(false) when radio1 is checked.

Complete Code:

public class MainActivity extends Activity {
String mobile;

RadioGroup rg1 = null;
EditText edit = null;

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

    rg1 = (RadioGroup) findViewById(R.id.radioGroup1);
    edit = (EditText) findViewById(R.id.editText1);
    edit.setEnabled(false);
    edit.setInputType(InputType.TYPE_NULL);
    edit.setFocusable(false);

    rg1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId) {
            case R.id.radio0:   
                op1="call";
                enableEdit(true); 
                break;

            case R.id.radio1:
                op1="email";
                enableEdit(false); 
                break;

            }   

        }
    });
    }

    public void enableEdit(boolean state) {
        edit.setEnabled(state);
        edit.setFocusable(state);
        if(state){ edit.setInputType(InputType.TYPE_CLASS_TEXT); } 
        else { edit.setInputType(InputType.TYPE_NULL); }
    }
}