-2

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);

    }

}
3
  • 1
    Set it as disabled in your layout. So, when you'll add it through setContentView() you'll find it already disabled. By doing so, you won't need any extra code. Except that to re-enable it later. Commented Jul 17, 2017 at 17:55
  • you can disable it through xml using enbale attribute. Commented Jul 17, 2017 at 17:56
  • Check this, stackoverflow.com/questions/4384890/… Commented Jul 17, 2017 at 17:57

4 Answers 4

2

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).

Sign up to request clarification or add additional context in comments.

3 Comments

Can I use if statement in onCreate method ? Because I have to take some checks and then have to disable the button
Sure. Or you can write something like accButton.setEnabled(check1 && check2);
Its just giving runtime error after putting if statments in it
1

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

Comments

1

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;
}

13 Comments

Can I use if statement in onCreate method ? Because I have to take some checks and then have to disable the button
@bs140200456AqsaAnum Yes, why not
I just putted if statment and now it is giving runtime error
@bs140200456AqsaAnum Include your if statement
Can I edit above coding and How, I will put my current code there
|
1

Do this:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.