0

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

hardartcore
  • 16,886
  • 12
  • 75
  • 101
Elad Benda
  • 35,076
  • 87
  • 265
  • 471
  • Please reformat the XML, it's impossible to read. – Simon Jul 26 '13 at 18:02
  • I can't see what the problem is, but for this kind of layout, I would go with RelativeLayout, every time. I just find it much better for anything except simple, well, "linear" layouts. – Simon Jul 26 '13 at 18:11
  • Your `ImageView` is centered because you have set its `width` to `wrap_content` and it's parent's `gravity` to center. – Vikram Jul 26 '13 at 18:14

4 Answers4

2

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.

DeeV
  • 35,865
  • 9
  • 108
  • 95
  • How can I manage the weight properly? I have tried to change to wrap_content and it all became too alignt to right. – Elad Benda Jul 26 '13 at 18:15
  • You can keep it the way it is. I don't see anything wrong with what your doing. The issue is that the TextView itself is left-aligned currently and at the exact width of the LinearLayout. You can center-align the TextView by setting `android:gravity="center" in the TextView. It'll place the text in the center. – DeeV Jul 26 '13 at 18:19
  • I have tried to make the width of `android:id="@+id/layout_information"` and `android:id="@+id/layout_status_image"` as `fill parent` I have though it possible as I have set weight already. But it broke everything. How come? I'm not sure if weight effects only the axis which has no specific size? – Elad Benda Jul 28 '13 at 07:41
  • When dealing with weight, you actually want to set the attribute to 0dp. Should have mentioned that. My apologies. So if the linearlayout orientation is "vertical", set the height of all its direct children to "0dp". If the orientation is "horizontal", set the width of all its direct children to "0dp". – DeeV Jul 28 '13 at 16:37
  • I have thought to set the parent's hight to 0dp – Elad Benda Jul 28 '13 at 17:45
1

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>
Vikram
  • 51,313
  • 11
  • 93
  • 122
  • @Simon Added some explanation above. – Vikram Jul 26 '13 at 18:12
  • what has precendence? match_parent or specific width in dp? layout_weight relates to hight and width? and if one of them has specific size in dp? – Elad Benda Jul 26 '13 at 18:14
  • `match_parent` and `specific width in dp` define the same property. So, there is no precedence defined here. `layout_weight` works with ratios instead of `height` and `width`(depending on the orientation). I think if you explicitly specify height(or width) for one of the components, that setting will take preference. Rest of the space will be distributed among other components based on their layout weights . – Vikram Jul 26 '13 at 18:29
  • @EladBenda See **Edit 1** above. Hope it clarifies a few things for you. – Vikram Jul 26 '13 at 18:48
  • @vikram `(use match_parent instead)` - no recommended according to this: http://stackoverflow.com/questions/9949387/fill-parent-and-match-parent – Elad Benda Jul 28 '13 at 06:49
  • @EladBenda You are right. I wrongly assumed that you are working with API 8 or above. `fill_parent` will work best for you. – Vikram Jul 28 '13 at 18:14
1

Add android:gravity="center" to your TextView

Pavel Dudka
  • 20,754
  • 7
  • 70
  • 83
  • Then why the image is centered? I know `android:gravity` attribute should appear in parent layout. `android:layout_gravity` is for the son. But I already have `android:gravity` – Elad Benda Jul 26 '13 at 18:12
  • your textView's width matches parent's width. That's why horizontal gravity declared in parent doesn't make sence for this view. In this case text inside TextView needs to be centered separately – Pavel Dudka Jul 26 '13 at 18:16
  • How can I manage the weight properly? I have tried to change to wrap_content and it all became too alignt to right – Elad Benda Jul 26 '13 at 18:16
  • you can change your TextView's width to wrap_content. It should work since your parent's horizontal gravity in 'center' – Pavel Dudka Jul 26 '13 at 18:21
  • `hat's why horizontal gravity declared in parent doesn't make sence for this view.` but yet I know a son can hold `android:layout_gravity` and not `android:gravity`. So I would expect you telling me to add `android:layout_gravity` to my `textView` no? – Elad Benda Jul 28 '13 at 11:12
-1

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

Salil Dua
  • 136
  • 1
  • 1
  • 8