-2

I have an xml file where I manage the visibility of a framelayout from the java side. the xml have 2 framelayout as button on a linearlayout . the orientation is horizental. Once I hide the first button on the right , the left one stay at his place but it should move to the right.

any ideas how to fix this ?

here is my xml code :

  <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_gravity="bottom|end">

    <FrameLayout
        android:id="@+id/btn1"
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:background="@drawable/float_button"
        android:visibility="gone">
        <ImageView

            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_gravity="center"
            android:id="@+id/icon2" />
    </FrameLayout>

    <FrameLayout
        android:id="@+id/btn2"
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:background="@drawable/float_button"
        android:visibility="gone">
        <ImageView
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_gravity="center"
            android:id="@+id/icon1"
            >

        </ImageView>
    </FrameLayout>
</LinearLayout>

my java code :

@Override
    public void notifyLayoutReady() {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                if (showUserOption("option1"))
                    mBtn1.setVisibility(View.VISIBLE);
                if (showUserOption("option2"))
                    mBtn2.setVisibility(View.VISIBLE);
            }
        });
    }
summoner
  • 13
  • 2
  • use else statement to hide again that view if (showUserOption("option1")) mBtn1.setVisibility(View.VISIBLE); else{mBtn1.setVisibility(View.GONE);} if (showUserOption("option2")) mBtn2.setVisibility(View.VISIBLE); else{mBtn2.setVisibility(View.Gone);} – Mohit Madaan Jun 18 '18 at 07:13

2 Answers2

1

Seems like while clicking on button and setting visibility you are using View.INVISIBLE , try using View.GONE for the same.

Deep Patel
  • 2,584
  • 2
  • 15
  • 28
  • by default it is gone , on the java side i call View.VISIBLE but it still taking space – summoner Jun 18 '18 at 06:43
  • You don't hide first layout, when second statement is true. Use `if(){} else {}` and in else let hide this layout. – grabarz121 Jun 18 '18 at 06:48
  • thanks that worked but I don't understand , if it was already gone on the xml why it should be called again on the java code ? – summoner Jun 18 '18 at 07:51
  • there is always two stages for any condition. if(){} and else{}... Now when you are showing something on 1 case then on other case it should be hidden. otherwise conditions will get misleading while trying same scenarios more than one time. – Deep Patel Jun 18 '18 at 07:54
0

there are two way mange it used gone to instand of invisiable..

        android:visibility="invisible"

or to set gravity to layout like below..

        android:layout_gravity="left"
        android:layout_gravity="right"

both frame layout because gone is remove all space and invisiable only hide the view but space is remaining.