0

I have this item in my top_navigation_menu layout I would like to enable it in my OnCreate method programmatically:

    <item
    android:id="@+id/action_button"
    android:enabled="false"
    android:visible="false"
    android:clickable = "false" />

Exemple : When User Open New Activity Enable this Item.

Edit : this is a follow up from my previous question so I'm trying another approach.

Edit : after some research I found

   Button btn = (Button) findViewById(R.id.action_button);
    btn.setEnabled(true);
    btn.setClickable(true);

Not sure which one I should use

Jaime Montoya
  • 6,915
  • 14
  • 67
  • 103

2 Answers2

0

in your xml instead of "item" use "Button"

in onCreate():

Button button = (Button) findViewById(R.id.action_button);
button.setVisibility(View.VISIBLE);
button.setEnabled(true); 
AM13
  • 661
  • 1
  • 8
  • 18
0

I'm confused about what you want to achieve. Visible, clickable and enable are same components attr state with different output

  1. visible is your button visibility if you set it value to false it gonna be gone and clear the Rect from the screen also
  2. enable it's mean prevents the user from tapping the Button. The appearance of enabled and non-enabled Button may differ, if the drawables referenced
  3. clickable it's mean how your Button reacts to click events. false mean disable the click events

What can I see is you tried to put them all, which is bad for me. One state is enough depending on what you trying to achieve for. This is examples and my suggestions :

  1. Enable State, XML :

    <Button
        android:id="@+id/action_button"
        android:enabled="false"/>
    
  2. Visibility State, XML :

    <Button
        android:id="@+id/action_button"
        android:visible="false"/>
    
  3. Clickable State, XML :

    <Button
        android:id="@+id/action_button"
        android:clickable="false"/>
    

And then from your OnCreate you can change change their state as follow

Button myButton = findViewById(R.id.action_button);
//for visibility state
myButton.setVisibility(View.VISIBLE);
//for enable state
myButton.setEnable(true);
//for clickable state
myButton.setClickable(true);
hnspaces
  • 44
  • 8