How to disable a button in onCreate method with if statment

412 Views Asked by At

I have a problem, I want to disable a button in onCreate method, please share the way of disabling any button at runtime in onCreate method.

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_requests_interface);


  Intent intent = new Intent(this, AdminPopup.class);
    startActivity(intent);


    String fName = getIntent().getStringExtra("fName");
    TextView tfName = (TextView)findViewById(R.id.fName);
    tfName.setText(fName);
    String vuEmail = getIntent().getStringExtra("VUEmail");
    TextView tEmail = (TextView)findViewById(R.id.vuEmail);
    tEmail.setText(vuEmail);

    EditText vuEmailTest = (EditText)findViewById(R.id.vuEmail);
    String email = vuEmailTest.getText().toString();
    String str = email.substring(0,2);
    if(str.equals("bc")){
        String str2 = email.substring(3,9);
        boolean digitsOnly = TextUtils.isDigitsOnly(str2);
        if (digitsOnly){
            Button accButton = (Button)findViewById(R.id.accButton);

        }
    }
    else{
        Button accButton = (Button)findViewById(R.id.accButton);

    }

}
4

There are 4 best solutions below

0
On BEST ANSWER

Button button =(Button) findViewById(R.id.buttonid); button.setVisibility(View.GONE);

3
On

Try this:

    Button accButton = (Button) findViewById(R.id.accButton);
    accButton.setEnabled(false);

Note that in your posted code, you are setting the button with findViewbyId(), which should be findViewById() (the By needs to be capitalized).

13
On

Use android:enabled="false" in xml or accButton.setEnabled(false) in code
Also, it's better to check is numeric by this method:

public static boolean isNumeric(String str) {
    try {
        double d = Double.parseDouble(str);
    } catch (NumberFormatException nfe) {
        return false;
    }
    return true;
}
0
On

Do this:

Button b = (Button) findViewById(R.id.mybutton);
b.setEnabled(false);