Android Hide and Show buttons from a condition

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
    
    />

    Activity code:

    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
    }

    Or Chec this one:

    if(Cars.payment) {
        btnDone.setVisibility(View.GONE);
        btnPaid.setVisibility(View.VISIBLE);
     } else {
        btnPaid.setVisibility(View.GONE);
        btnDone.setVisibility(View.VISIBLE);
     }

Leave a Reply