Elad Benda
Elad Benda

Reputation: 36656

a textView refuses to center as a son of LinearLayout

I try to center the refresh sign and text.

But the text is aligned to the right.

Why is that?

XML

<RelativeLayout
    android:id="@+id/layout_status_image"
    android:layout_width="82dp"
    android:layout_height="fill_parent"
    android:layout_gravity="center_vertical"
    android:layout_weight=".2"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/image_status"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="9dp"
        android:src="@drawable/widget_icon_no_data"
        android:visibility="invisible" />

    <!--
  Layout is necessary because the setVisibility of ProgressBar is not working 
  through remote views in 2.1. So wrapped by this layout 
    -->

    <FrameLayout
        android:id="@+android:id/widget_progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginLeft="1dp"
        android:layout_marginTop="1dp" >

        <ProgressBar
            style="?android:attr/progressBarStyle"
            android:layout_width="39dp"
            android:layout_height="39dp"
            android:indeterminateOnly="true"
            android:orientation="vertical" />
    </FrameLayout>
</RelativeLayout> <!-- Status image layout -->


<!--
========================================================================
* Information layout - contains all the texts 
========================================================================
-->

<LinearLayout
    android:id="@+id/layout_information"
    android:layout_width="190dp"
    android:layout_height="fill_parent"
    android:layout_weight=".6"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/text_view_destination"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="8dp"
        android:text="\@ Home in"
        android:textColor="@color/solid_white"
        android:textSize="19sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/text_view_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:paddingLeft="9dp"
        android:textColor="@color/solid_white"
        android:textSize="25sp"
        android:textStyle="normal" />
</LinearLayout> <!-- Information layout -->


<!--
========================================================================
* Action layout - action buttons container
========================================================================
-->

<LinearLayout
    android:id="@+id/layout_action"
    android:layout_width="80dp"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:layout_weight=".2"
    android:gravity="center"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/image_action"
        android:layout_width="wrap_content"
        android:layout_height="wrap_contenth"
        android:src="@drawable/widget_bt_drive_disabled" />

    <TextView
        android:id="@+id/h"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="6dp"
        android:text="Refresh"
        android:textColor="@color/disabled_white"
        android:textSize="15sp"
        android:textStyle="bold" />
</LinearLayout> <!-- Action layout -->

Screen:

enter image description here

Upvotes: 0

Views: 764

Answers (4)

Vikram
Vikram

Reputation: 51571

Since the TextView's width is set to fill_parent(use match_parent instead), setting the TextView's gravity to center will fix this. Try this:

<LinearLayout
    android:id="@+id/layout_action"
    android:layout_width="80dp"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:gravity="center"                     <-- Added
    android:orientation="vertical"
    android:layout_weight=".2" >



    <ImageView
        android:id="@+id/image_action"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/deletelight1" />



    <TextView
        android:id="@+id/h"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="6dp"
        android:text="Refresh"
        android:gravity="center"
        android:textColor="@color/black"
        android:textSize="15sp"
        android:textStyle="bold" />
</LinearLayout>

You can also fix this by changing the TextView's layout_width to wrap_content. In this case, setting the TextView's gravity attribute to center will not be required.

Edit 1:

Copy and paste the following code in a xml layout file and switch to Graphical Layout to see how layout_weight works when one of the components has a fixed height. Since, the following is a LinearLayout with vertical orientation, I've assigned 0dp to height for the TextViews and set a fixed height of 50dp to the ImageView. You will see that the height of the TextViews depend on their respective layout_weights and the space left after assigning 50dp to the ImageView.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_action"
    android:layout_width="80dp"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:gravity="center"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/image_action"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:src="@drawable/widget_bt_drive_disabled" />



    <TextView
        android:id="@+id/h1"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginTop="6dp"
        android:text="Refresh 50"
        android:textColor="@color/disabled_white"
        android:textSize="15sp"
        android:textStyle="bold"
        android:layout_weight="50" />
    <TextView
        android:id="@+id/h2"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginTop="6dp"
        android:text="Refresh 25"
        android:textColor="@color/disabled_white"
        android:textSize="15sp"
        android:layout_weight="25"
        android:textStyle="bold" />
    <TextView
        android:id="@+id/h3"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginTop="6dp"
        android:text="Refresh 10"
        android:textColor="@color/disabled_white"
        android:textSize="15sp"
        android:layout_weight="10"
        android:textStyle="bold" />
    <TextView
        android:id="@+id/h4"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginTop="6dp"
        android:text="Refresh 15"
        android:textColor="@color/disabled_white"
        android:textSize="15sp"
        android:textStyle="bold"
        android:layout_weight="15" />
</LinearLayout>

Upvotes: 1

Salil Dua
Salil Dua

Reputation: 136

Hey just Add padding to your textview. or set gravity as center. It will be fine...!! Make sure u set gravity and not LayoutGravity

Upvotes: -1

DeeV
DeeV

Reputation: 36035

It's because you've set the TextView width to fill_parent. The TextView is now full width of the Layout, so it technically is centered. Change it to wrap_content or set the TextView gravity to center as well.

Upvotes: 2

Pavel Dudka
Pavel Dudka

Reputation: 20934

Add android:gravity="center" to your TextView

Upvotes: 1

Related Questions