139

In my application, I have 2 LinearLayout's right above each other. Via a menu option, I want to be able to make the bottom one disappear, and have the top one drop down over the disappeared LinearLayout.

The problem is, I have no idea on how to do this in Java.

It doesn't have to be animated, I want to hide the Layout on return of another activity (the menu), in OnActivityResult. The menu activity sets a boolean on which I check in OnActivityResult, and according to it's value I determine if I need to hide or show the bottom Layout:

// Only change value if it is different from what it was.
if(mUseVolumeButtonAsPTT != resultData.getBoolean("UseVolumeButtonAsPTT")){
    mUseVolumeButtonAsPTT = resultData.getBoolean("UseVolumeButtonAsPTT");
    if(!mUseVolumeButtonAsPTT){
        // Hide lower LinearLayout.
    } else {
        // Show lower LinearLayout.
    }
}

Can anybody give me a hint or a link on how I should do this?

3 Answers 3

347

You can call view.setVisibility(View.GONE) if you want to remove it from the layout.

Or view.setVisibility(View.INVISIBLE) if you just want to hide it.

From Android Docs:

INVISIBLE

This view is invisible, but it still takes up space for layout purposes. Use with setVisibility(int) and android:visibility.

GONE

This view is invisible, and it doesn't take any space for layout purposes. Use with setVisibility(int) and android:visibility.

6
  • 18
    Can the user still interact with it if it is set to view.setVisibility(View.INVISIBLE) ? Apr 24, 2013 at 8:24
  • Can an activity, for instance, make changes to a view if it is set to GONE? I'd imagine you could if it's set to INVISIBLE.
    – moatist
    Jul 28, 2014 at 4:53
  • 2
    What about in RelativeLayout? Views positions are depending each other. I wonder what will happen after a view gone. Dec 29, 2014 at 8:49
  • @ErPragatiSingh please always put link from where you copy-paste, and keep the answer similar to how the OP wanted to say. :)
    – Sufian
    Feb 6, 2016 at 2:38
  • @Sufian thanks, it all from android developer official site.
    – Prags
    Feb 6, 2016 at 5:00
19

Try this:

linearLayout.setVisibility(View.GONE);
1

Kotlin Solution

view.isVisible = true
view.isInvisible = true
view.isGone = true

// For these to work, you need to use androidx and import:
import androidx.core.view.isVisible // or isInvisible/isGone

Kotlin Extension Solution

If you'd like them to be more consistent length, work for nullable views, and lower the chance of writing the wrong boolean, try using these custom extensions:

// Example
view.hide()

fun View?.show() {
    if (this == null) return
    if (!isVisible) isVisible = true
}

fun View?.hide() {
    if (this == null) return
    if (!isInvisible) isInvisible = true
}

fun View?.gone() {
    if (this == null) return
    if (!isGone) isGone = true
}

To make conditional visibility simple, also add these:

fun View?.show(visible: Boolean) {
    if (visible) show() else gone()
}

fun View?.hide(hide: Boolean) {
    if (hide) hide() else show()
}

fun View?.gone(gone: Boolean = true) {
    if (gone) gone() else show()
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.