5

I have two buttons in android. Btn1 and Btn2. I need to show Btn1 by default and when is is clicked I need to show Btn2 in place of Btn1. Again when Btn2 is clicked, it should be replaced with Btn2. Please help me with some example..

My Code:

btn1 = (ImageView) findViewById(R.id.firstbtn);

        btn2 = (ImageView) findViewById(R.id.secondbutton);
        btn2.setVisibility(ImageView.GONE);

        btn1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                btn2.setVisibility(ImageView.VISIBLE);
            }
        });

        btn2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                btn2.setVisibility(ImageView.GONE);




            }

        });

Thanks in advance

4 Answers 4

6

You can use same button and change its label alternatively and as per level you can put the check and perform appropriate work.

As per your updated code get to know your are using ImageView so try this

   ImageView btn = (ImageView) findViewById(R.id.secondbutton);
    btn.setTag("1");//can use one empty String "" and null instead of "1" and "2" for optimization

        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                Object obj = v.getTag();

                if(obj instanceof String){
                     if("1".equals(obj)){
                         v.setTag("2");
                         //Your first button state 
                     }else if("2".equals(obj)){
                         v.setTag("1");
                         //Your second button state 
                     }

                }

...

If your case is specific for two buttons then you can use two String compare operations

ImageView btn = (ImageView) findViewById(R.id.secondbutton);
  btn.setTag(null);

    btn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            if(v.getTag() instanceof String){
                     v.setTag(null);
                     //Your first button state 
            } else {
                      v.setTag("");
                     //Your second button state 
            }

If you want to make your code work right

btn1 = (ImageView) findViewById(R.id.firstbtn);

        btn2 = (ImageView) findViewById(R.id.secondbutton);
        btn2.setVisibility(ImageView.GONE);

        btn1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                v.setVisibility(ImageView.GONE);
                btn2.setVisibility(ImageView.VISIBLE);

            }
        });

        btn2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                v.setVisibility(ImageView.GONE);
                btn1.setVisibility(ImageView.VISIBLE);

            }

        });
Sign up to request clarification or add additional context in comments.

2 Comments

Please let us know if code required ... I think it's simple you will get button view in onClick(View v)
See my code. By default the first btn is displaying and when i click on it the second btn will display along with first btn and when again the second btn is clicked it will be gone and only first btn is remaining. But everytime i want to display only one btn.help me plz
2

change this

btn2.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            btn2.setVisibility(ImageView.GONE);
        }

    });

by

btn2.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            btn2.setVisibility(View.GONE);
            btn1.setVisibility(View.VISIBLE);
        }

    });

and this

 btn1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            btn2.setVisibility(View.VISIBLE);
        }
    });

by

  btn1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            btn1.setVisibility(View.GONE);
            btn2.setVisibility(View.VISIBLE);
        }
    });

Comments

0

You can create two buttons in layout with same exact positions, then you can set visibility and action of each button that you want

Comments

0

Can use method visible() to fix its visibility. ToggleButton or one button and toggle the text on click event should work better than two buttons.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.