How to Check if a View is Visible or not Visible in Android Java

Check visibility of a view in android java

The code below checks if a view is visible or invisible in android using the java if statement,  it is always a good practice to check the state of a view before executing a code to the view so that you can avoid Nullpointer or another exceptions


 

Checking Visibility of the Item in Java with If Statement

if (imgView.getVisibility() == View.VISIBLE) {
    // The view is visible, do something here
} else {
    // The view is either gone or is invisible, do something
}

 

imgview is an example, you can add views like ImageView,Layout views etc in the place of imgView

, ,

Post navigation

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.