Search code examples
androidandroid-layoutshow-hide

Android display half TextView and half ImageView if true else display only TextView


I'm trying to display a text inside a TextView in the left side of the screen and an image inside ImageView in the right side when "true":

<RelativeLayout
    android:id="@+id/displayMessageCenter"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/displayMessageTop"
    android:layout_centerHorizontal="true"
    android:background="@android:color/white">
    <TextView
        android:id="@+id/textMessageText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:textColor="#000000"
        android:layout_weight="0.5"
        android:layout_alignBottom="@+id/dividerId" />
    <ImageView
        android:id="@+id/messagePicture"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignBottom="@+id/dividerId"
        android:layout_weight="0.5"
        android:gravity="right"/>
    <View
        android:id="@+id/dividerId"
        android:layout_width="400dp"
        android:layout_height="1dp"
        android:background="#000"
        android:layout_above="@id/leftTextGenericDialog"
        android:gravity="center"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="20dp"/>
    <TextView
        android:id="@+id/leftTextGenericDialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:gravity="center"
        android:layout_centerHorizontal="true"
        android:textColor="#FF72CCCC"
        android:text="SEND RESPONSE"
        android:textSize="40dp"
        android:drawableLeft="@drawable/sendicon"
        android:layout_above="@+id/closeButton"
        android:onClick="sendMessage"/>

    <ImageButton
        android:id="@+id/closeButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="5sp"
        android:layout_gravity="center"
        android:background="@drawable/button_close_animation"
        android:onClick="closeActivity"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="20dp"/>

</RelativeLayout>

and if "false" to hide the image and display just the TextView with the text in the center. The hiding will be something like:

if(true) imageView.setVisibility(View.GONE);
else imageView.setVisibility(View.VISIBLE);

but I don't know how to align the ImageView and the TextView to be easier to hide/show just the ImageView when needed.

I'm new in Android development so please be gentle :)


Solution

  • When using Relative layout on these cases, you can use alignParentLeft=true for the TextView and alignParentRight=true for the ImageView, but if you wanted the TextView to adjust whenever the ImageView is visible or not. Use something like

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        >
    
      <TextView
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:text="text"
          android:layout_weight="0.5"
          android:gravity="center_horizontal"
          />
    
      <ImageView
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_weight="0.5"
          android:visibility="gone"
    
          />
    
    </LinearLayout>