5

I have a button that basically looks like this:

 <Button
    android:id="@+id/admin_new_questions"        
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="See Asked Questions"
    />    

and I try to display it only in some cases like this:

if ( clause )
{
        Button admin_see_questions = (Button)findViewById(R.id.admin_new_questions);   
        admin_see_questions.setOnClickListener(new Button.OnClickListener() 
        {  
            public void onClick(View v) 
            {
           ....    
            }
        });        
}

But for some reason, the button is displayed for all cases, but the listenter is not being listened to if the clause is an error.

How can I make the button show up only when the clause is true?

Thanks!

GeekedOut
  • 16,905
  • 37
  • 107
  • 185
  • Others has already answered correctly your question but I want to advice you on a UX problem. I think that hide is not the correct way to do so. IMHO the correct way is to always show the button but disable it if the clause is false and enable it if it's true :) – StErMi Apr 03 '12 at 14:35

9 Answers9

9

Your button is in the XML layout, so you can hide it or show it by just changing its visibility

NB: You only need do these operations once:

  1. Get a reference to your button, with findViewById()
  2. Set the OnClickListener of the button

    <Button
    android:id="@+id/admin_new_questions"        
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="See Asked Questions"
    
    android:visibility="invisible" //Initially hide the button
    
    />    
    

-

Button admin_see_questions = (Button)findViewById(R.id.admin_new_questions);   
admin_see_questions.setOnClickListener(new Button.OnClickListener() 
{  
    public void onClick(View v) 
    {
        ....       
    }
});  

if ( clause )
{
    admin_see_questions.setVisibility(View.VISIBLE); //SHOW the button
}
Sébastien
  • 13,831
  • 10
  • 55
  • 70
1

Set the initial state of your button to Gone/Invisible in xml and then check the condition in code.

<Button
    android:id="@+id/admin_new_questions"        
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="See Asked Questions"
    android:visibility="gone"
    />

and if the condition is true or false, then:

Button admin_see_questions = (Button)findViewById(R.id.admin_new_questions);
admin_see_questions.setOnClickListener(new Button.OnClickListener() 
{  
    public void onClick(View v) 
    {
       ....    
    }
});

if ( clause )
    admin_see_questions.setVisibility(View.VISIBLE);  
else
    admin_see_questions.setVisibility(View.GONE);
waqaslam
  • 67,549
  • 16
  • 165
  • 178
  • 1
    Your code will never compile. Your button is not within scope of the else clause. – onit Apr 03 '12 at 14:17
1

All UI components declared in he XML are displayed by default. You need to either:

  1. Specify that it should not be visible in the layout XML

    <Button ...
     android:visibility="gone"
     />
    
  2. Hide it if the condition evaluated as false:

    Button admin_see_questions = (Button)findViewById(R.id.admin_new_questions);
    if ( !clause ) {
        admin_see_questions.setVisibility(View.GONE);
    }
    
MByD
  • 135,866
  • 28
  • 264
  • 277
1

You can use View.GONE and View.VISIBLE in setVisibility to control this, as a starting point.

Whether you use View.GONE or another similar value depends on whether you want this button accounted for in the layout or not.

You can look at this question for more information:

Android: why setVisibility(View.GONE); or setVisibility(View.INVISIBLE); do not work

You could also just disable it, so it is seen, but not active, and that may be a better option.

Community
  • 1
  • 1
James Black
  • 41,583
  • 10
  • 86
  • 166
1

Use admin_see_questions.setVisibility(View.GONE) or admin_see_questions.setVisibility(View.VISIBLE)

Khawar
  • 5,197
  • 4
  • 28
  • 52
1

You are not setting the buttons visibility. Their are three visibility options, visible, invisible, and gone. The following code should work, either showing or hiding the button based on the clause.

Button admin_see_questions = (Button)findViewById(R.id.admin_new_questions); 
if ( clause )
{  
        admin_see_questions.setVisibility(View.VISIBLE);
        admin_see_questions.setOnClickListener(new Button.OnClickListener() 
        {  
            public void onClick(View v) 
            {
           ....    
            }
        });        
} else admin_see_questions.setVisibility(View.INVISIBLE); //may want to use View.GONE here depending on what you want to accomplish
onit
  • 6,306
  • 3
  • 24
  • 31
1

.setVisibility(View.GONE); to hide

ViewGroup group = (ViewGroup)(myView.getParent()); grouo.removeView(myView); to remove

pouzzler
  • 1,800
  • 2
  • 20
  • 32
1

try this out, when the condition is satisfied

if ( clause )
{
admin_see_questions.setVisibility(admin_see_questions.VISIBLE);
}
else
admin_see_questions.setVisibility(admin_see_questions.INVISIBLE);
Ajay
  • 1,796
  • 3
  • 18
  • 22
1

You need to remove or hide the button. Try using admin_see_questions.setVisibility(View.INVISIBLE) or admin_see_questions.setVisibility(View.GONE). To show the button again, call admin_see_questions.setVisibility(View.VISIBLE)

Lie Ryan
  • 62,238
  • 13
  • 100
  • 144