Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use visible and invisible for a button in android

I want to make a button invisible, when i click another button then the invisible button will become visible and then perform onClick() actions on the visible button.

What onClick() actions I can use on the visible button. I used this method shown below:

   donebutton = (Button) findViewById(R.id.done);
    donebutton.setOnClickListener(this);
    donebutton.setVisibility(View.INVISIBLE);

    @Override
     public void onClick(View v) {
    // TODO Auto-generated method stub
         if(v.equals(remove))
           {
             donebutton.setVisibility(View.VISIBLE);
           }
        if(v.equals(donebutton))
            {
                Intent i=new Intent(One.this,Second.class);
                startActivity(i);
                finish();
                donebutton.setVisibility(View.INVISIBLE);
            }

      }

In the above method the invisible and visible propertyes are working but onClick() action is not working. so please tell me an answer for the above problem or tell me if there is any other method for visible and invisible on button and onclick action on that button.

and I also used this method:

       done.setClickable(true);
       done.setOnClickListener(new OnClickListener(){
         public void onClick(View v) {
            Intent i=new Intent(One.this,Second.class);
            startActivity(i);
            finish();
         }
     });
like image 649
Ramakrishna Avatar asked Jan 06 '11 10:01

Ramakrishna


People also ask

How do you make a button invisible and visible?

Based on the state of the Checkbox you can use Button1. setVisibility(true/false); to show/hide the button.

How do I make a button invisible in XML?

xml version = "1.0" encoding = "utf-8" ?> In this code, initially, a variable temp is set to false. Now when the button is clicked, the code satisfies the if condition and makes the button invisible, and the temp value is reversed to true.


3 Answers

DONT USE -

donebutton.setVisibility(4);

Instead use the static constants for this:

donebutton.setVisibility(View.VISIBLE);

What exactly means

done.setVisibility(0);

Isn't is supposed to be

donebutton.setVisibility(View.GONE);
like image 109
Kiril Kirilov Avatar answered Oct 13 '22 10:10

Kiril Kirilov


Here you go:

Button theButton = (Button)findViewById(R.id.theButton);
theButton.setVisibility(View.VISIBLE);
theButton.setBackgroundColor(Color.TRANSPARENT);

phoneButton.setOnClickListener(new OnClickListener()
{ 
 @Override
 public void onClick(View v)
 {
  // DO STUFF
 }
});
like image 35
ingh.am Avatar answered Oct 13 '22 09:10

ingh.am


Hopefully this can help you to hide the buttons as well as show the buttons if they are hidden. You need to have three buttons in your layout file in order to follow this example.

Button b3 = (Button) findViewById(R.id.button3);
     @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        if (b1.isShown() && b2.isShown()) {
                            b1.setVisibility(View.GONE);
                            b2.setVisibility(View.GONE);

                        } else {
                            b1.setVisibility(View.VISIBLE);
                            b2.setVisibility(View.VISIBLE);
                        }
                    }
                });
like image 36
Waqas Khalid Obeidy Avatar answered Oct 13 '22 08:10

Waqas Khalid Obeidy