De Santa De Santa - 1 year ago 99
Android Question

How to add a TextView that occupies the full width of the phone's layout?

I'm working on an app where I want to have a progress bar and a text view. And the thing is the text view should have a different background. When I use

android:layout_width="match_parent"
, it leaves space on the sides of the textview. When we have a white background, it is okay but when we are putting a different background, it doesn't look good. How can I achieve this?

In short, I'm getting the following view(first image) when I put
match_parent
but I want the page to look like the one in the second image.

When I add match_parent This is how I want the UI to be

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="senti.com.freelaneprogress.MainActivity">

<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_centerInParent="true"
android:max="100"
android:progress="40"
android:rotation="270"
android:progressDrawable="@drawable/circular"/>

<TextView
android:id="@+id/eta"
android:background="#00000000"
android:layout_height="100dp"
android:layout_width="150dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="1.3in"/>

<TextView
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_marginTop="2in"
android:layout_centerHorizontal="true"/>

<TextView
android:background="@drawable/textviewbg"
android:layout_width="match_parent"
android:layout_height="30pt"
android:contextClickable="false"
android:layout_alignParentBottom="true"
android:id="@+id/textView" />
</RelativeLayout>

Answer Source

Set the paddingLeft, paddingRight and paddingBottom to 0dp for the RelativeLayout view. In other words,

<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"
    android:paddingBottom="0dp"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="senti.com.freelaneprogress.MainActivity">


    ...
    ...
</RelativeLayout>

This is the minimum you need to achieve your desired result. But if you want to be absolute, you can set the RelativeLayout's paddingTop to 0dp too.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download