Dawid Walczyk Dawid Walczyk - 16 days ago 5
Java Question

How to align TextView with background in Layout - Android

I've got a small problem.

I've got:

enter image description here

I want: (but the text few dp from the left)

enter image description here

Is it possible? Here is a whole code of my layout: (@+id/txtname is the textview from the screenshot!)

<?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="match_parent"
android:background="@color/md_brown_100"
android:orientation="vertical"
android:gravity="center"
android:paddingTop="10dp"
android:paddingBottom="10dp" >


<LinearLayout android:id="@+id/list_offer_item_container"
android:layout_width="150dp"
android:layout_height="150dp"
android:background="@drawable/shape"
android:gravity="center"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"

android:layout_gravity="center_vertical">

<TextView android:id="@+id/list_offer_badge_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/list_offer_item_container"
android:textColor="@color/md_amber_700"
android:textSize="12sp"
android:visibility="gone"
android:text=""/>
</LinearLayout>

<LinearLayout
android:layout_height="150dp"
android:orientation="vertical"
android:padding="5dp"
android:weightSum="1"
android:layout_width="150dp">

<ImageView
android:id="@+id/imgcreditcompany"
android:scaleType="fitXY"
android:layout_gravity="center_horizontal"

android:layout_above="@+id/txtname"
android:contentDescription="@string/app_name"
android:layout_height="48dp"
android:layout_width="48dp" />


<TextView
android:id="@+id/txtname"
android:gravity="center_vertical"
android:background="@drawable/ic_dollar_ss"
android:layout_height="30dp"
android:layout_marginTop="5dp"
android:textColor="@color/AliceBlue"
android:textSize="18sp"
android:text="Name"
android:textAllCaps="false"
android:textStyle="normal|bold"
android:layout_width="153dp" />

<TextView
android:id="@+id/txtdesc"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_marginTop="1dp"
android:maxLines="3"

android:textColor="#80869c"
android:textSize="@dimen/textsizeearncredit_desc"
android:text="This is a description of the offer and this is just a demo to show off 3 lines stacking correctly on top of each other"
android:layout_weight="0.55"
android:layout_width="140dp" />
</LinearLayout>

<ImageView
android:id="@+id/nextArrow"
android:layout_alignParentRight="true"
android:tint="@color/md_grey_300"
android:rotation="180"
android:layout_width="32dp"
android:visibility="gone"
android:layout_marginRight="16dp"
android:layout_gravity="center"
android:layout_centerVertical="true"
android:layout_height="32dp"
android:contentDescription="@string/app_name"
android:padding="@dimen/two" />

</LinearLayout>


</RelativeLayout>


Please help me guys! Thanks a lot of You can give me any idea of it. Cheers!

Answer

The problem is that your LinearLayout has a padding of 5dp, which keeps your "Invite Friends" TextView away from the edge.

Replace

<LinearLayout
        android:layout_height="150dp"
        android:orientation="vertical"
        android:padding="5dp"
        android:weightSum="1"
        android:layout_width="150dp">

with

<LinearLayout
        android:layout_height="150dp"
        android:orientation="vertical"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:paddingEnd="5dp"
        android:weightSum="1"
        android:layout_width="150dp">

This way you will have no padding on the left. To compensate it on the description text, add android:paddingStart="5dp" to your TextView txtdesc

Comments