Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Android Lists and Adapters (2015) Using Parcelable Data Updating the Daily Forecast UI

Sergey Valevich
Sergey Valevich
6,041 Points

different screen sizes/gravity

I've tested the look of the daily_list_Item layout on several devices and it looks fine. But on my Galaxy S3 the temperature is not centered inside the TextView. This is how it looks https://www.dropbox.com/s/dccho7l0m3verg1/DailyForecast.png?dl=0.

Here is the 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="wrap_content" android:paddingBottom="4dp" android:paddingTop="4dp" android:paddingLeft="32dp" android:paddingRight="32dp" tools:background = "#ffaa00">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/circleImageView"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:src="@drawable/bg_temperature"/>

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/iconImageView"
    android:src="@drawable/clear_day"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@+id/circleImageView"
    android:paddingLeft="10dp"
    android:layout_toEndOf="@+id/circleImageView"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/dayNameLabel"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@+id/iconImageView"
    android:layout_toEndOf="@+id/iconImageView"
    android:textColor="#ffffffff"
    android:textSize="20sp"
    android:paddingLeft="10dp"
    tools:text = "Wednesday"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/temperatureLabel"
    android:layout_alignTop="@+id/circleImageView"
    android:layout_alignBottom="@+id/circleImageView"
    android:layout_alignStart="@+id/circleImageView"
    android:layout_alignEnd="@+id/circleImageView"
    tools:text = "100"
    android:textColor="#f25019"
    android:layout_alignParentEnd="false"
    android:gravity="center"/>

</RelativeLayout>

The gravity is set to "center", but it doesn't work. :(

1 Answer

You can try

android:layout_centerInParent="true"

or you can try setting it in code

TextView textView = (TextView) findViewById(R.id.temperatureLabel); textView.setGravity(Gravity.CENTER_HORIZONTAL); textView.setGravity(Gravity.CENTER_VERTICAL);

I think the issue might be caused by Samsung altering the Android OS too much so things don't work smoothly.

Sergey Valevich
Sergey Valevich
6,041 Points

Thank you very much, Ozhan Saat! android:layout_centerInParent="true" WORKED!