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) Custom ListViews Creating a Custom Layout - Part 2

George Vardikos
George Vardikos
6,889 Points

Center the temperature does work for 18 API

if u run on a device with 18 API android platform it doesn't align it to the centre of the circle

6 Answers

Víctor Hernández
Víctor Hernández
12,276 Points

It works for me!

<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_alignLeft="@+id/circleImageView"
        android:layout_alignRight="@+id/circleImageView"
        android:gravity="center|center_vertical|center_horizontal"
        android:textColor="#f25019"
        tools:text="100"
        android:layout_alignParentRight="false"
        android:layout_centerInParent="true" />
George Vardikos
George Vardikos
6,889 Points

it works due to "android:layout_centerInParent="true" "

Thank you!

Robert Proulx
Robert Proulx
6,538 Points

Tried that and it didn't work, fussed with it some more and I just realized I had android:layout_centerInParent setting to an @id instead of a "true". Since I had added it into the XML, didn't catch it till I noticed the field was still blank in the design view properties. Still don't know why it worked regardless on the android studio emulator and not Genymotion, but now they are both working properly.

Juan Santiago
Juan Santiago
3,766 Points

Man, your answer fixed my problem. Thnx!!

Robert Proulx
Robert Proulx
6,538 Points

The center in parent didn't work for me. I found out it worked in the Android Emulator, but not the Genymotion. Anyone else experiencing this or know a reason why this might be the case?

Emulators

Javier Alvarado
Javier Alvarado
16,060 Points

I'm also seeing the results on the right in my Genymotion emulator, although I'm not sure why. Setting android:layout_centerInParent to true fixed it for me.

George Vardikos
George Vardikos
6,889 Points

can you paste the code of the xml?

Robert Proulx
Robert Proulx
6,538 Points

There's some additional text views in here since I proceeded through the course.

The Daily Forecast XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin"
                android:background="@drawable/bg_gradient">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@android:id/list"
        android:layout_alignParentTop="false"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/thisWeekLabel"
        android:layout_above="@+id/locationLabel"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:divider="@null"
        android:dividerHeight="0dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/no_daily_forecast_data"
        android:id="@android:id/empty"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:textColor="#ffffffff"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/daily_view_header"
        android:id="@+id/thisWeekLabel"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textColor="#ffffffff"
        android:textSize="30sp"
        android:layout_marginTop="16dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/location_label"
        android:id="@+id/locationLabel"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:textColor="#ffffffff"
        android:textSize="18sp"
        android:textIsSelectable="true"
        android:layout_marginBottom="10dp"/>

</RelativeLayout>

And here is the individual list item elements 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:paddingLeft="64dp"
                android:paddingRight="32dp"
                android:paddingTop="4dp"
                android:paddingBottom="4dp"
                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:layout_centerVertical="true"
        android:layout_toRightOf="@+id/circleImageView"
        android:layout_toEndOf="@+id/circleImageView"
        android:paddingLeft="10dp"
        android:src="@drawable/clear_day"/>

    <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:paddingLeft="10dp"
        android:textColor="#ffffffff"
        android:textSize="20sp"
        android:textIsSelectable="false"
        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_alignLeft="@+id/circleImageView"
        android:layout_alignRight="@+id/circleImageView"
        android:layout_alignStart="@+id/circleImageView"
        android:layout_centerInParent="@+id/circleImageView"
        android:gravity="center"
        android:textColor="#f25019"
        tools:text="100"/>

</RelativeLayout>
George Vardikos
George Vardikos
6,889 Points

try to erase from the daily list item xml " android:layout_alignStart="@+id/circleImageView" "