-1

I want to create an activity which has to edittexts and two buttons ok and cancel, of which ok button should be disabled first and should be enabled when username and password are validated. My code is as-

public class DisableButton extends Activity {
EditText et1,et2;
Button b1,b2;
String et_value, pwd_value;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_disable_button);
    et1=(EditText)findViewById(R.id.editText1);
    et2=(EditText)findViewById(R.id.editText2);
    b1=(Button)findViewById(R.id.button1);
    b1.setEnabled(false);
    b2=(Button)findViewById(R.id.button2);
    et_value=et1.getText().toString();
    pwd_value=et2.getText().toString();

    if((et_value=="pbpathi")&&(pwd_value=="1234"))
    {
        b1.setEnabled(true);
    }

}
public void okClick(View v){
    Toast.makeText(getBaseContext(), "Username is "+et_value+" \n password is
"+pwd_value, Toast.LENGTH_LONG).show();

}

public void cancelClick(View v){

}

}

And my xml file is-

 <LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:gravity="center"
 android:orientation="vertical"
 tools:context="com.example.labs.DisableButton" >

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10" />


<EditText
    android:id="@+id/editText2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textPassword" />


<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="OK" 
    android:onClick="okClick"/>

<Button
    android:id="@+id/button2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Cancel"
    android:onClick="cancelClick" />

</LinearLayout>
MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
Harika
  • 23
  • 2
  • 8
  • what does et_value=et1.getText().toString(); pwd_value=et2.getText().toString(); returns ?? – Manishika Oct 07 '14 at 07:52
  • please compare two strings with a equals method like `value.equals("1234");` – A.S. Oct 07 '14 at 07:53
  • possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Selvin Oct 09 '14 at 07:30

4 Answers4

2

Firstly,

You shouldn't fetch edittexts' values in onCreate, but in onClick

Secondly,

You shouldn't use == for comparing Strings. It will compare objects.

So change your code as:

public void okClick(View v){
    Toast.makeText(getBaseContext(), "Username is "+et_value+" \n password is
"+pwd_value, Toast.LENGTH_LONG).show();
    et_value=et1.getText().toString();
    pwd_value=et2.getText().toString();

    // You can use equalsIgnoreCase for cas insensitive comparison
    if((et_value.equals("pbpathi"))&&(pwd_value.equals("1234"))) 
    {
        b1.setEnabled(true);
    }
}

Hope this help.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
  • This is the correct answer. However, one might wonder if we should mention that this is NOT the right way to implement a login/password field XD – Robin Eisenberg Oct 07 '14 at 07:56
0
if((et_value.equals("pbpathi")&&(pwd_value.equals("1234")))
{
    b1.setEnabled(true);
}

equal mathed in java

Naveen Tamrakar
  • 3,349
  • 1
  • 19
  • 28
0

Use equals instead of ==.

if("pbpathi".equals(et_value) && "1234".equals(pwd_value)) {
    ....
}
sergej shafarenka
  • 20,071
  • 7
  • 67
  • 86
0
public class DisableButton extends Activity {
    EditText et1, et2;
    Button b1, b2;
    String et_value, pwd_value;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et1 = (EditText) findViewById(R.id.editText1);
        et2 = (EditText) findViewById(R.id.editText2);
        b1 = (Button) findViewById(R.id.button1);
        b1.setEnabled(false);`enter code here`
        b2 = (Button) findViewById(R.id.button2);

        et1.addTextChangedListener(mTextWatcher);
        et2.addTextChangedListener(mTextWatcher);

    }

    public void okClick(View v) {
        et_value = et1.getText().toString();
        pwd_value = et2.getText().toString();

        Toast.makeText(getBaseContext(),
                "Username is " + et_value + " \n password is" + pwd_value,
                Toast.LENGTH_LONG).show();

    }

    public void cancelClick(View v) {

    }

    private final TextWatcher mTextWatcher = new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {


        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {


        }

        @Override
        public void afterTextChanged(Editable s) {
            if ((et1.getText().toString().equals("pbpathi"))
                    && (et2.getText().toString().equals("1234"))) {
                b1.setEnabled(true);



            }

        }
    };

}