Home > Software engineering >  Space between ImageView and TextView in android layouts
Space between ImageView and TextView in android layouts

Time:09-17

I have a problem with space between 2 views. Padding and Margin doesn't work. Can you suggest anything please? Need working code. Thanks in advance.

Edit: I need a space horizontally between the icon and text1 id here:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:padding="10.0dip" android:descendantFocusability="beforeDescendants" android:layout_width="fill_parent" android:layout_height="fill_parent"
  xmlns:android="http://schemas.android.com/apk/res/android">
    <ImageView android:id="@id/icon" android:visibility="gone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="12.0dip" android:layout_marginRight="12.0dip" android:src="@drawable/quantum_gm_ic_sd_storage_black_24" android:layout_alignParentLeft="true" android:contentDescription="Choose" android:paddingRight="16.0dip" />
    <TextView android:textSize="16.0sp" android:ellipsize="marquee" android:id="@id/text1" android:paddingLeft="16.0dip" android:paddingRight="16.0dip" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Choose" android:layout_toRightOf="@id/icon" android:layout_alignParentLeft="false" android:layout_centerVertical="false" android:textColor="@color/textswitch_color" />
    <TextView android:textSize="13.0sp" android:id="@id/txt_size" android:paddingLeft="40.0dip" android:paddingRight="16.0dip" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Choose" android:layout_toLeftOf="@id/txt_date" android:layout_below="@id/text1" android:layout_alignLeft="@id/text1" android:textColor="@color/textswitch_secondary_color" />
    <TextView android:textSize="13.0sp" android:id="@id/txt_date" android:paddingLeft="8.0dip" android:paddingRight="16.0dip" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Choose" android:layout_below="@id/text1" android:layout_alignRight="@id/text1" android:textColor="@color/textswitch_secondary_color" />
</RelativeLayout>

Screenshot of layout

CodePudding user response:

First of all I think your code isn't working. To set the id for a view the correct syntax expects before id.

android:id="@ id/text1"

After that, I think your problem is setting android:layout_width ="fill_parent" on text views. Change it to android:layout_width="wrap_content".

Then you can use android:layout_marginTop="20dp" or android:layout_marginEnd="20dp" to set the spacing.

Here's a simplified, but working, example of your layout.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:padding="10.0dip"
    android:descendantFocusability="beforeDescendants"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView
        android:textSize="16.0sp"
        android:ellipsize="marquee"
        android:id="@ id/text1"
        android:layout_marginTop="70.0dip"
        android:paddingLeft="8.0dip"
        android:paddingRight="16.0dip"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Choose"
        android:layout_alignParentLeft="false"
        android:layout_centerVertical="false"
        android:textColor="?attr/colorPrimary" />
    <TextView
        android:textSize="13.0sp"
        android:id="@ id/txt_size"
        android:layout_marginTop="70.0dip"
        android:paddingLeft="8.0dip"
        android:layout_marginEnd="16.0dip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Choose"
        android:layout_below="@id/text1"
        android:layout_alignEnd="@id/text1"
        android:textColor="?attr/colorPrimary" />
    <TextView
        android:textSize="13.0sp"
        android:id="@ id/txt_date"
        android:paddingLeft="8.0dip"
        android:paddingRight="16.0dip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Choose"
        android:layout_below="@id/txt_size"
        android:textColor="?attr/colorPrimary" />
</RelativeLayout>
  • Related