0

I currently have this XML layout for my Android project:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context="bhavik.slidingmenu.activity.LogFragment">
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="495dp">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <RelativeLayout
            android:id="@+id/relLayout1"
            android:layout_marginTop="20dp"
            android:layout_marginBottom="20dp"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <com.rockerhieu.emojicon.EmojiconTextView
                android:id="@+id/txtEmojicon1"
                android:text="\ue11b"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="45dp"
                android:textIsSelectable="true"
                android:layout_centerVertical="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_marginLeft="35dp"
                android:layout_marginStart="20dp" />

            <SeekBar
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/seekBar1"
                android:max="10"
                android:progress="0"
                android:layout_alignTop="@+id/txtEmojicon1"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_marginTop="8dp"
                android:layout_marginLeft="90dp"
                android:layout_marginRight="75dp" />

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/relLayout2"
            android:layout_marginTop="20dp"
            android:layout_marginBottom="20dp"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <com.rockerhieu.emojicon.EmojiconTextView
                android:id="@+id/txtEmojicon2"
                android:text="\ue11b"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="45dp"
                android:textIsSelectable="true"
                android:layout_centerVertical="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_marginLeft="35dp"
                android:layout_marginStart="20dp" />

            <SeekBar
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/seekBar2"
                android:max="10"
                android:progress="0"
                android:layout_alignTop="@+id/txtEmojicon2"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_marginTop="8dp"
                android:layout_marginLeft="90dp"
                android:layout_marginRight="75dp" />

        </RelativeLayout>
    </LinearLayout>
</ScrollView>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/fab_ic_add"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="@dimen/fab_button_margin_bottom"
    android:layout_marginRight="@dimen/fab_button_margin_right"
    app:elevation="6dp"
    app:pressedTranslationZ="12dp"
    app:borderWidth="0dp"/>
</RelativeLayout>

As you see in the code I have 2 relative layouts called relLayout1 and relLayout2. Let's say depending on the conditions, I want to only display relLayout1 and not relLayout2, how would I do that? Or the other way around?

Zoe
  • 23,712
  • 16
  • 99
  • 132

3 Answers3

0

You would set the visibility property to invisible or gone if you want to hide something.

In xml it would be android:visibility="invisible"

Programmatically it would be myView.setVisibility(View.INVISIBLE);

craya
  • 564
  • 4
  • 17
0

You need to set view visibility depending on conditions, let's say you need to display RelativeView1 on certain condition and RelativeView2 otherwise.

if(condition) {
  relView1.setVisibility(View.VISIBLE);
  relView2.setVisibility(View.GONE);
}
else {
  relView2.setVisibility(View.VISIBLE);
  relView1.setVisibility(View.GONE);
}
Community
  • 1
  • 1
Abhishek
  • 2,919
  • 2
  • 14
  • 23
-1
in your Activity
RelativeLayout relLayout1 = (RelativeLayout) findViewById(R.id.relLayout1);
RelativeLayout relLayout2 = (RelativeLayout) findViewById(R.id.relLayout2);
    if (condition == true){
        relLayout1.setVisibility(View.VISIBLE);
    }else{
        relLayout2.setVisibility(View.VISIBLE);
    }
and in your xml
<RelativeLayout
        android:id="@+id/relLayout1"
        android:visibility="gone"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
<RelativeLayout
        android:id="@+id/relLayout2"
        android:visibility="gone"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"> 
dione llorera
  • 317
  • 3
  • 13