Toggle button visibility in android activity

久未见 提交于 2019-11-29 10:54:05

I Use for any view:

 public void toggleView(View view){
 if(view.getVisibility()==View.GONE)
   view.setVisibility(View.VISIBLE);
 else if(view.getVisibility()==View.VISIBLE)
   view.setVisibility(View.GONE);
  }
d.danailov
startMedia = (Button) findViewById(R.id.button1);
pauseMedia = (Button) findViewById(R.id.button2);

//Onclick Event ...

//Logic
if (flag === 'start') {
startMedia.setVisibility(View.VISIBLE);
pauseMedia .setVisibility(View.INVISIBLE);
} else {
startMedia.setVisibility(View.INVISIBLE);
pauseMedia .setVisibility(View.VISIBLE);
}

GONE will make the layout manager ignore the dimensions of the widget. INVISIBLE will make it so the widget is not visible, but the layout manager will still treat it as if it is there.

If you are familiar with CSS,

  • setVisibility(View.INVISIBLE) is like CSS opacity: 0
  • setVisibility(View.GONE) is like CSS display: none
  • setVisibility(View.VISIBLE) is like CSS display: block

You can set your pauseMedia's visibility to invisible with the below code:

pauseMedia.setVisibility(View.INVISIBLE);

or to set it as visible:

pauseMedia.setVisiblity(View.VISIBLE);

Maybe try something like this:

in the context of this function

controls

is the view that was inflated when the class was instantiated

private void toggleControls(){

    //private variable to store whether or not the view is showing
    if(controlsAreShowing){
       if(controls != null) {
           controls.setVisibility(View.INVISIBLE);
           controlsAreShowing = false;
       }
    }else{
        controls.setVisibility(View.VISIBLE);
        controlsAreShowing = true;
    }
}

I think following code may work,

play.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "Play", Toast.LENGTH_SHORT).show();
            mediaPlayer.start();
            pause.setEnabled(true);
            play.setEnabled(false);
        }
    });

For disabling pause button,

pause.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "pause", Toast.LENGTH_SHORT).show();
            mediaPlayer.pause();
            pause.setEnabled(false);
            play.setEnabled(true);
        }
    });

I think this code may work for your scenario.

Use this Simple Logic to Toggle Visibility...

 if (rvLocation.getVisibility()==View.VISIBLE)
        rvLocation.setVisibility(View.GONE);
 else
        rvLocation.setVisibility(View.VISIBLE);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!