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>