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

    }

}
  • 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. – Phantômaxx Jul 17 '17 at 17:55
  • you can disable it through xml using `enbale` attribute. –  Jul 17 '17 at 17:56
  • Check this, https://stackoverflow.com/questions/4384890/how-to-disable-an-android-button – UmarZaii Jul 17 '17 at 17:57

4 Answers4

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

Ben P.
  • 52,661
  • 6
  • 95
  • 123
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;
}
SiSa
  • 2,594
  • 1
  • 15
  • 33
1

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

1

Do this:

Button b = (Button) findViewById(R.id.mybutton);
b.setEnabled(false);
Elysian Apps
  • 106
  • 3