Michael
Michael

Reputation: 439

How to get invisible button on other button click

I have 2 buttons for display. The 1st button is Unpressed - that displayed before the user clicks on it and the 2nd button is pressed that displayed after the user clicks on it.

After User click on the 1st button, he's disappeared (INVISIBLE), and then the 2nd button appears (Visibility.VISIBLE).

The problem is that the 1st button not appear after the user presses the button again. The 1st button does not appear again.

That what I tried.

private void initFunctionality() {
    startPlayPressed.setVisibility(View.INVISIBLE);
    startPlay.setVisibility(View.VISIBLE);
    startPlay.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            startPlay.setVisibility(View.INVISIBLE);
            startPlayPressed.setVisibility(View.VISIBLE);
            imageView.startAnimation(animation);

            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    ActivityUtilities.getInstance().invokeNewActivity(SplashActivity.this, MainActivity.class, true);
                }
            }, 500);
        }
    });
}

I want that the 1st button will appear again after the user presses the button again.

Upvotes: 1

Views: 766

Answers (4)

Anjana
Anjana

Reputation: 933

Your question is somewhat unclear. Assuming that you want to,

1) Hide Button1 and show Button2 when Button1 is pressed.

2) Hide Button2 and show Button1 when Button2 is pressed.

Below code will help you to achieve this.

private void initFunctionality() {
    startPlayPressed.setVisibility(View.INVISIBLE);
    startPlay.setVisibility(View.VISIBLE);

    startPlay.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            startPlay.setVisibility(View.INVISIBLE);
            startPlayPressed.setVisibility(View.VISIBLE);
            imageView.startAnimation(animation);

            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    ActivityUtilities.getInstance().invokeNewActivity(SplashActivity.this, MainActivity.class, true);
                }
            }, 500);
        }
    });

    startPlayPressed.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            startPlay.setVisibility(View.VISIBLE);
            startPlayPressed.setVisibility(View.INVISIBLE);
            imageView.startAnimation(animation);

            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    //Do whatever you want
                }
            }, 500);
        }
    });
}

Upvotes: 1

Sotiris S. Magionas
Sotiris S. Magionas

Reputation: 799

If you want one thing to happen when you press the button and a different thing to happen when you release the button you should not use a ClickListener. You should use an onTouchListener.

Example:

startPlay.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction()==MotionEvent.ACTION_DOWN){
            // User pressed the button. What should happen here?
        }else if (event.getAction()==MotionEvent.ACTION_UP){
            // User released the button. What should happen now?
        }
        return true;
    }
});

Note: Your English is a little bit confusing. I assume this is what you want( press/release button) but maybe I didn't understand you fully...

Upvotes: 3

Udara Abeythilake
Udara Abeythilake

Reputation: 1213

Instead of Using INVISIBLE try to use GONE keyword as below

private void initFunctionality() {
    startPlayPressed.setVisibility(View.GONE);
    startPlay.setVisibility(View.VISIBLE);
    startPlay.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            startPlay.setVisibility(View.GONE);
            startPlayPressed.setVisibility(View.VISIBLE);
            imageView.startAnimation(animation);

            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    ActivityUtilities.getInstance().invokeNewActivity(SplashActivity.this, MainActivity.class, true);
                }
            }, 500);
        }
    });
}

Upvotes: 0

Jasurbek
Jasurbek

Reputation: 2966

Here is the function view.getVisibility(), you can try it. In your case view will be button

Upvotes: 1

Related Questions