I am trying to make a view that contains some elements from left to right order and a button, that attached to right top corner with fixed size. My current layout is:
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="2">
<LinearLayout
android:orientation="horizontal"
android:layout_column="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/Image1"
android:layout_height="40dp"
android:layout_width="40dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="24dp"
android:layout_marginBottom="16dp"
android:src="@drawable/image_src"
android:visibility="gone" />
<TextView
android:id="@+id/Text1"
android:text="SampleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginBottom="16dp"
android:layout_marginTop="24dp"
android:textSize="16sp"
android:gravity="center" />
</LinearLayout>
<ImageButton
android:id="@+id/Button1"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="right"
android:layout_column="1"
android:src="@drawable/imageBtn"
style="?android:attr/borderlessButtonStyle" />
</GridLayout>
When text is short everything seems to be ok
But the problem is that text can be very long, and it overlaps button.
How can I make button always visible and text wrapped exactly on the left corner of it?
You can achieve this using RelativeLayout. Check below one in your xml layout.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageButton
android:id="@+id/Button1"
style="?android:attr/borderlessButtonStyle"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentRight="true"
android:layout_marginTop="24dp"
android:src="@mipmap/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/Button1"
android:orientation="horizontal">
<ImageView
android:id="@+id/Image1"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="16dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="24dp"
android:src="@mipmap/ic_launcher"
android:visibility="visible" />
<TextView
android:id="@+id/Text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="24dp"
android:text="SampleText sdfsd sd sdf sdfs sdfsd fsf fsdf sd s dsfs sfs sfs sdf s fsdf sdf fsdf sdf sf sd fsdf sfsdf sfsd ffs sdf sdf sfsdf sfs sdfs sfsdfs sfs sf sfs fsfsf sf ssf "
android:textSize="16sp" />
</LinearLayout>
</RelativeLayout>