Strange XML layout bug when targeting Android 4.3
Asked Answered
L

3

3

I'm experiencing a really peculiar bug with an XML layout file when building my application while targeting API level 18. It doesn't happen with API level 17. I'm running the application on Android 4.3 devices, and the bug persists on all three devices.

Here's what it looks like:

API 17 (correct):

API 17

API 18 (incorrect):

API 18

I'm using the StickyGridHeaders library, and the following is my getHeaderView() method:

@Override
public View getHeaderView(int position, View convertView, ViewGroup parent) {
    RowItem holder;

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.seasons_row_header, parent, false);
        holder = new RowItem();
        holder.title = (TextView) convertView.findViewById(R.id.seasonTitle);
        holder.episodeCount = (TextView) convertView.findViewById(R.id.episodeCount);
        convertView.setTag(holder);
    } else {
        holder = (RowItem) convertView.getTag();
    }

    if (seasons.get(position).equals("00")) {
        holder.title.setText(R.string.stringSpecials);  
    } else {
        holder.title.setText(getString(R.string.showSeason) + " " + seasons.get(position));
    }

    int size = seasonEpisodeCount.get(Integer.valueOf(seasons.get(position)));
    holder.episodeCount.setText(size + " " + getResources().getQuantityString(R.plurals.episodes, size, size));

    convertView.setClickable(false);
    convertView.setFocusable(false);

    return convertView;
}

Here's the layout XML file:

<?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="wrap_content"
    android:background="#80000000"
    android:padding="@dimen/list_padding" >

    <TextView
        android:id="@+id/seasonTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="@dimen/list_padding"
        android:layout_toLeftOf="@+id/episodeCount"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textIsSelectable="false"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/episodeCount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/seasonTitle"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/seasonTitle"
        android:gravity="bottom"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textIsSelectable="false" />

</RelativeLayout>

Does anyone else have a clue as to what's going on here? I find it really strange that it's working when targeting API level 17 and not working when targeting the latest API level (18).

Update:

This is what it looks like in the visual layout editor with Android 4.3 as the target:

Visual layout editor

Littlefield answered 11/9, 2013 at 20:55 Comment(8)
Have you tried removing this: android:layout_toLeftOf="@+id/episodeCount" I know sometimes cross referencing positions causes weird behaviors...Scopas
I just tried. It makes the TextView appear, but the other one is rendered on top of it :-(Littlefield
Its a start... :) Try removing the '+' from both @+id/seasonTitle in the episodeCount textviewScopas
Without trying, I doubt that'll work. The + is needed to create the ID.Littlefield
I was referring to the android:layout_alignBottom="@+id/seasonTitle" and android:layout_alignTop="@+id/seasonTitle"Scopas
Just tried it - that didn't work either :-(Littlefield
Just a heads up for anyone encountering this issue (or something similar): it appears to be related to the updated behaviour around how a RelativeLayout deals with MeasureSpec.UNSPECIFIED in a scroll container (i.e. ScrollView or ListView). Read the note in the RelativeLayout documentation for more details, and refer to #59700 on the Android issue tracker.Albino
Cheers, MH! It did seem quite strange.Littlefield
L
2

I managed to solve the issue myself.

Like I said, I am using the StickyGridHeaders library and referencing it in my application. It appears that the library was targeting API level 17. I changed it to level 18, compiled and ran the application - great success!

In conclusion: Make sure your application and libraries target the same API level.

Littlefield answered 12/9, 2013 at 14:50 Comment(0)
E
1

I have the same issue, too. My experiences have shown that crossed alignments are the reason of the problem (TextView @+id/seasonTitle has the property android:layout_toLeftOf="@+id/episodeCount"; TextView @+id/episodeCount has the properties android:layout_alignBottom="@+id/seasonTitle" and layout_alignTop="@+id/seasonTitle". One of the Layouts won't refresh its childs correct. Changing the Layout-XML that crossed alignments are prevented, you no longer have this problem.

Eliason answered 21/1, 2014 at 14:52 Comment(0)
S
0

I have the same issue :

  • a ListView with a white background
  • each child has a View with a gray background and width and height to match_parent, within a RelativeLayout

on API 17, every is OK on API 18+, the ListView is white

instead of setting a fixed height, I put a min_height to the View, and the problem is (temporary) solved!

hope it helps

Southing answered 27/11, 2013 at 15:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.