fengdai/AlertDialogPro

On GingerBread, ListView's background looks wried with material theme when one of its item is pressed.

fengdai opened this issue · 19 comments

On GingerBread emulator. Choose Material theme(or Material light theme), and show "List Dialog"(or any other dialog contains ListView) . Then press an item and all the other items look like being pressed.

17

ummm... I don't see this issue. Are you sure it's there?

Yes, I'm sure. My emulator's Android version is 2.3.3.

But I can't reproduce it on the emulator. Odd.

Press one item, and don't release your finger.

oops. I missed this part. How could it be?

I also want to know…

Here's what I did in my app. It avoids this issue by having a simple ListView.
Maybe it could help.

Single selection:

final ArrayAdapter<String> adapter=new ArrayAdapter<String>(activity,//
    android.R.layout.simple_list_item_single_choice,itemsStrs);
final AlertDialogPro.Builder builder=new AlertDialogPro.Builder(dialogStyle);
builder.setTitle(...);
final ListView listView=new ListView(activity);
builder.setView(listView);
final AlertDialogPro dialog=builder.setCancelable(true).create();
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listView.setAdapter(adapter);
listView.setItemChecked(selectedIndex,true);
listView.setOnItemClickListener(new OnItemClickListener()
  {
    @Override
    public void onItemClick(final AdapterView<?> parent,final View view,final int position,final long id)
      {
      ...
      }
  });
dialog.show();

Multi-selection:

final AlertDialogPro.Builder builder=new AlertDialogPro.Builder(activity,dialogStyle);
builder.setTitle(...);
builder.setNegativeButton(android.R.string.cancel,null);   
final ArrayAdapter<String> adapter=new ArrayAdapter<String>(activity,//
    android.R.layout.simple_list_item_multiple_choice,//
    itemsStr);
final ListView listView=new ListView(activity);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setAdapter(adapter);
builder.setPositiveButton(android.R.string.ok,new OnClickListener()
  {
    @Override
    public void onClick(final DialogInterface dialog,final int which)
      {
      final SparseBooleanArray checkedItemPositions=listView.getCheckedItemPositions();
      ...
      }
  });
builder.setView(listView);

Thank you @AndroidDeveloperLB,What's the style xml of your ListView.

I didn't set a style for the listView.
Only the style of the dialog is written, and that's just those that you use in your library.
Do you think that the "adapter" variable uses the theme of the activity ?

Your ListView use the activity's theme:

final ListView listView=new ListView(activity);

So what's your activity's theme?

I created a dialog as your way. It looked like this("Male" item is pressing):
17_

It doesn't has this issue, because the list selector is a drawable with "Holo" color. And this is its xml file:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_window_focused="false" android:drawable="@android:color/transparent" />

<!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
<item android:state_focused="true"  android:state_enabled="false" android:state_pressed="true" android:drawable="@drawable/abc_list_selector_disabled_holo_light" />
<item android:state_focused="true"  android:state_enabled="false"                              android:drawable="@drawable/abc_list_selector_disabled_holo_light" />
<item android:state_focused="true"                                android:state_pressed="true" android:drawable="@drawable/abc_list_selector_background_transition_holo_light" />
<item android:state_focused="false"                               android:state_pressed="true" android:drawable="@drawable/abc_list_selector_background_transition_holo_light" />
<item android:state_focused="true"                                                             android:drawable="@drawable/abc_list_focused_holo" />

This is my material item background:

 <?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/adp_mtrl_ripple_light" android:state_pressed="true" />
    <item android:drawable="@android:color/transparent" />
</selector>

I think the difference is that the above uses image resource as its drawable:

<item android:state_focused="false"  android:state_pressed="true" android:drawable="@drawable/abc_list_selector_background_transition_holo_light" />

And mine, use color:

<item android:drawable="@color/adp_mtrl_ripple_light" android:state_pressed="true" />

When I replaced "@color/adp_mtrl_ripple_light" with a "Shape" drawable, the issue gone:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
    <solid android:color="@color/adp_mtrl_ripple_light" />
</shape> 

So I think this would be the point. But I still don't know the actual reason...

Well the activity has the theme of the library, with just a few adjustments for other stuff, but as I remember they shouldn't affect ListViews in general...
Here are some parts of it, that use the native attributes:

    <item name="colorPrimary">@color/normal_window_background__light</item>
    <item name="android:textColorPrimary">@android:color/black</item>
    <item name="android:textColorSecondary">@android:color/black</item>
    <item name="android:textColorTertiary">@android:color/black</item>
    <item name="android:windowBackground">@color/normal_window_background__light</item>

The rest is just customized attributes that I've created for very specific usages (because my app has multiple themes the user can choose from).

Same issue here.

@RaymondBakkerMoonlightMultimedia I think this is caused by an issue of old platform's ListView(Not very sure). Here is a workaround: 3ab68ad

what exactly have you changed?

@AndroidDeveloperLB Seems old Android verions behave differently when using @color or @drawable resources. I have yet to test to verify that it has been fixed.

Ohhh... I remember I read about it somewhere. in order to use a color, you have to use a drawable that has it inside a "shape" xml tag.

Just tested it, can confirm that it has been fixed!

Could you please publish the new version on Maven? Thanks in advance

@marbat87 0.2.1 is available now.