Button myButton = findViewById(R.id.myButton); int visibility = myButton.getVisibility(); if (visibility == View.VISIBLE) { Toast.makeText(this, "The button is visible", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "The button is hidden", Toast.LENGTH_SHORT).show(); }
ImageView myImage = findViewById(R.id.myImage); Button myButton = findViewById(R.id.myButton); myButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (myImage.getVisibility() == View.VISIBLE) { myImage.setVisibility(View.INVISIBLE); } else { myImage.setVisibility(View.VISIBLE); } } });The getVisibility method is part of the android.view.View class, which is included in the Android framework. Therefore, it is part of the Android package library.