1

I have an xml layout file that is used to show a ListView item. Here is xml code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
    android:id="@+id/task_item_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/list_item_background"
    android:orientation="horizontal"
    android:weightSum="1">

    <RelativeLayout
        android:id="@+id/task_item_icon_container"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.2">

        <ImageView
            android:id="@+id/item_icon"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            app:srcCompat="@android:drawable/stat_notify_sync" />

    </RelativeLayout>

    <TextView
        android:id="@+id/item_text"
        android:layout_width="0dp"
        android:layout_height="75dp"
        android:layout_weight="0.6"
        android:gravity="center_vertical"
        android:paddingLeft="10dp"
        android:text="appName"
        android:textAlignment="textStart"
        android:textSize="20sp" />

    <RelativeLayout
        android:id="@+id/additional_info_container"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.19">

        <TextView
            android:id="@+id/price"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="5dp"
            android:text="0,1 $"
            android:textSize="20sp" />
    </RelativeLayout>
</LinearLayout>

I want TextView with id price to have gravity value set to center. But when I do it, item_text somewhy jumps to the center. This behaviour can be seen only in editor, everything is fine on my device.

Layout when price gravity value is not set:

no gravity

Layout when price gravity value is set to center:

gravity: center

Why is this happening and how do I fix this?

UPD: Do not edit my question: I change gravity of price TextView, and item_text is the one that changes. This is the strange behaviour I am asking about.

Sam Stone
  • 477
  • 2
  • 10
  • 33
  • 2
    Why do you need a RelativeLayout around that imageview or text view? Also see https://stackoverflow.com/questions/3482742/gravity-and-layout-gravity-on-android – OneCricketeer Jul 11 '17 at 13:00
  • @cricket_007 I need RelativeLayout to set margins for `price`. Both TextViews have `gravity` attributes set, so it must change only the contents. – Sam Stone Jul 11 '17 at 13:06
  • Do you really need a RelativeLayout to set margins? – OneCricketeer Jul 11 '17 at 13:11
  • @cricket_007 well, in fact, I don't. I need this RelativeLayout because I am going to add other Views into it and show the needed ones depending on context. – Sam Stone Jul 11 '17 at 13:18

2 Answers2

1

In your TextView with id item_text add

android:layout_gravity="center"

It happens because there is an inconsistent specification between text_alignment and gravity. Older phones ignore text_alignment, which explains why it works on your device.

MSpeed
  • 8,153
  • 7
  • 49
  • 61
  • Exactly the same result. – Sam Stone Jul 11 '17 at 13:08
  • I think you wanted center_vertical? – OneCricketeer Jul 11 '17 at 13:10
  • Are you sure? I can see the issue on mine, and putting this line in fixes it. Make sure everything is up to date and the preview API Version is the "Automatically pick best" – MSpeed Jul 11 '17 at 13:11
  • 2
    @cricket_007 yes, sorry, I used `android:gravity` instead of `android:layout_gravity`. Now I use both(center for `android:layout_gravity` and center_vertical for `android:gravity`). Thanks, this fixes the issue. – Sam Stone Jul 11 '17 at 13:13
1

Make the following changes

<RelativeLayout
            android:id="@+id/additional_info_container"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.19">
            <TextView
                android:id="@+id/price"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" //here
                android:layout_margin="5dp"
                android:text="0,1 $"
                android:layout_centerVertical="true" //add this
                android:textSize="20sp" />
        </RelativeLayout>
debugger
  • 580
  • 1
  • 6
  • 16
  • But I am going to use billynomates` solution, because I am going to add other Views into this RelativeLayout. – Sam Stone Jul 11 '17 at 13:30