Android Studio example of implementing onclicklistener in Javascript for layout

Feedback


Question:

I possess a layout that resembles the following:

Upon clicking on layer1, layer2 becomes invisible; however, clicking on layer2 does not result in it becoming invisible. I have attempted to use the provided code, but it does not work as intended. I am seeking suggestions on how to make layer2 visible again when it is clicked.

 RelativeLayout layout1=(RelativeLayout)findViewById(R.id.hidebg);
         final RelativeLayout layout=(RelativeLayout)findViewById(R.id.hidelayer);
         layout1.setOnClickListener(new OnClickListener() 
         {
            @Override
            public void onClick(View v)
            {
                if(layout.getVisibility()==0)
            {
                layout.setVisibility(4);
            }
            else
            {
                layout.setVisibility(0);
            }
            }
        }); 


Solution 1:

Set up a listener for events on the secondary layout.

layout.setOnClickListener(new OnClickListener() 
         {
            @Override
            public void onClick(View v)
            {
                if(i==0)
                {
                    layout.setVisibility(4);
                    i=1;
                }
                else
                {
                    layout.setVisibility(0);
                    i=0;
                }
            }
        }); 

Then check.


Solution 2:


By default, make layer2 visible and apply the following code:

layer1.setOnClickListener(new OnClickListener() 
         {
            @Override
            public void onClick(View v)
            {
                if(layer2.getvisibility==View.VISIBLE)
                {
                    layer2.setVisibility(View.GONE);
                }
                else
                {
                    layer2.setVisibility(View.VISIBLE);
                }
            }
        }); 

Frequently Asked Questions