Android ListView with Custom List Selector causing Flickring problem - android

I'm setting a selector.xml to the ListView Selector :
<item android:state_focused="false" android:state_selected="true"
android:state_pressed="true" android:drawable="#drawable/timeline_selected_rect"/>
<item android:state_focused="false" android:state_selected="true"
android:state_pressed="false" android:drawable="#drawable/timeline_selected_rect"/>
<item android:state_focused="true" android:state_selected="true"
android:state_pressed="true" android:drawable="#drawable/timeline_selected_rect"/>
but setting this is causing the TextView flickr over selection. this thing is not happening when the Default selector is used... whats wrong with this selector.
I have even added android:cacheColorHint="#00000000" as provided info by some other blogs.

I recently ran into this problem as well and found the solution by analyzing the Android source code.
You need to remove the android:state_selected="true" attributes since checking that state is unneeded. Once a list item loses it's focus, it also makes 'selected = false'. Since focus=false & selected=true will never occur, your first 2 <items> in the selector will never be shown.
Think of the tap like a mouse click with an onPressDown and onPressUp event.
So your new selector.xml should looks something like this to avoid the flickering:
<!-- {comment copied directly from Android source code}
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_pressed="true" android:state_focused="true"
android:drawable="#drawable/timeline_selected_rect"/>
<item android:state_pressed="true" android:state_focused="false"
android:drawable="#drawable/timeline_selected_rect" />
<item android:state_focused="true"
android:drawable="#drawable/timeline_selected_rect" />
<item android:state_window_focused="false"
android:drawable="#android:color/transparent" />
As an aside, you should seriously consider creating a separate drawable for the pressed state so that you give a bit of visual feedback. It is always nicer to see "something happen" when interacting with the UI; whether that is drawing a button in a "down" state while it is being pressed or changing the color slightly while tapping on a list item, the visual feedback is good practice.

Related

How do I imitate the behaviour of a pressed button in Android?

I'm trying to make a custom button, that behaves just like a 'pressed button' in Android with my own background images.
Pressing changes my button's image (color is darker), but I can't seem to get the focussed / focussed&pressed states right.
When you release your finger on the default button there is a circle filling the whole button but it won't happen with mine.
That's my code:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="#drawable/fertig_focussed" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/fertig_focussed_pressed" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/fertig_pressed"/>
<item android:drawable="#drawable/fertig_regular" />
</selector>
Thanks in advance!
That's because android check stats in the order and get the one matching first.
You need put "pressed" first, focus pressed second, then focused.

Android selector. How to go to normal state after press?

I've got a list of cards, and when the user presses on them, it goes to different fragments.
I'm using a selector on a card to have a "pressed state". I use the following XML:
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:state_enabled="true"
android:state_pressed="true"
android:drawable="#drawable/hatching" />
<item
android:state_enabled="true"
android:state_focused="true"
android:drawable="#color/transparent" />
<item
android:drawable="#color/transparent" />
</selector>
When I press on a card, if the next fragment takes 1 second to load, the card stays in the "pressed" state, until the fragment is displayed. Does someone know how to do like in recent Google apps (Messenger, Gmail, ...), ie after the press, the card goes to "normal" state, and then after the fragment is ready, the fragment is displayed.
Thanks.
UPDATE: The problem doesn't seem to be in the selector. If instead of going to another fragment I go to display another activity, the card goes to normal state. It's only when going to a fragment. It's like if the fragment takes all the resources and prevent the normal state to be displayed.
Yet, if we go to the onTouchEvent() method of the View class, there's this code:
// Use a Runnable and post this rather than calling
// performClick directly. This lets other visual state
// of the view update before click actions start.
if (mPerformClick == null) {
mPerformClick = new PerformClick();
}
if (!post(mPerformClick)) {
performClick();
}
So I don't know why the normal state is not displayed...
Try with the following Selector for CardView
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/card_on_press"/>
<item android:state_focused="true" android:state_enabled="true"
android:drawable="#drawable/card_on_focus"/>
<item android:state_enabled="true"
android:drawable="#drawable/card_default"/>
<item android:state_enabled="false"
android:drawable="#drawable/card_on_press"/>
</selector>
Note: Drawable default will be fine as a transparent item as CardView provides a default background for all android versions
Try this...
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:state_pressed="true"
android:drawable="#drawable/hatching" />
<item
android:drawable="#color/transparent" />

TabHost indicator color change code not working

I have been reading on Stack to try to change my color of the indicator (the underline of the tab that tells you which tab is selected), but I used code I found here, but it won't work. I get 2 errors:
In view.findViewById(R.id.tabhost), the view and the tabhost are red (cannot resolve symbol).
One is at widget.getChildCount(), it says the method invocation may produce a null pointer exception.
Third error is at v.setBackgroundResource(R.drawable.tab_selector_color); it says the same, it may produce a null pointer exception.
I downloaded all the drawables and changed their color like the above link said, then I put them all in my drawable-mdpi folder, just as it suggested.
I will post a section of my main activity, which has this the code block with errors in it, and also the tab_selector_color.xml, which is in my drawable folder (i think it must be put here, instead of layout folder, since it has selector tags).
How to fix these errors? Thanks in advance, please let me know if anything is not clear or if you need to see more files.
Method with the errors from my main activity:
(I took our the title code below, because I don't have a TextView anywhere (using icons for my indicator drawable part), but whether that code is there or not, the 3 errors are still there).
public void changeTabIndicators() {
FragmentTabHost host = (FragmentTabHost)view.findViewById(R.id.tabhost);
TabWidget widget = host.getTabWidget();
for(int i = 0; i < widget.getChildCount(); i++) {
View v = widget.getChildAt(i);
// // Look for the title view to ensure this is an indicator and not a divider.
// TextView tv = (TextView)v.findViewById(android.R.id.title);
// if(tv == null) {
// continue;
// }
v.setBackgroundResource(R.drawable.tab_selector_color);
}
}
tab_selector_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item android:state_focused="false"
android:state_selected="false"
android:state_pressed="false"
android:drawable="#drawable/tab_unselected_holo" />
<item android:state_focused="false"
android:state_selected="true"
android:state_pressed="false"
android:drawable="#drawable/tab_selected_holo" />
<!-- Focused states -->
<item android:state_focused="true"
android:state_selected="false"
android:state_pressed="false"
android:drawable="#drawable/tab_unselected_focused_holo" />
<item android:state_focused="true"
android:state_selected="true"
android:state_pressed="false"
android:drawable="#drawable/tab_selected_focused_holo" />
<!-- Pressed -->
<!-- Non focused states -->
<item android:state_focused="false"
android:state_selected="false"
android:state_pressed="true"
android:drawable="#drawable/tab_unselected_pressed_holo" />
<item android:state_focused="false"
android:state_selected="true"
android:state_pressed="true"
android:drawable="#drawable/tab_selected_pressed_holo" />
<!-- Focused states -->
<item android:state_focused="true"
android:state_selected="false"
android:state_pressed="true"
android:drawable="#drawable/tab_unselected_pressed_holo" />
<item android:state_focused="true"
android:state_selected="true"
android:state_pressed="true"
android:drawable="#drawable/tab_selected_pressed_holo" />
</selector>
Actually the yellow warnings (errors on # 2 and 3) did not cause problems, but what I did change was the .getChildCount() method. I looked it up in the TabWidget Android pages and could not find this method there, so I used getTabCount() instead. To fix the error in the word view from view.findViewById(R.id.tabhost), I deleted out view. and that fixed it.
I also deleted the TextView code, because my tabs are not using text, but rather icons in the little tab part, so I didn't need that whole method (as you can see it was commented out in my code anyways).
So I would like to affirm again, that the TabWidget current tab bottom line color did mostly work for me, even though downloading the drawables from Github took forever, because you have to download the entire parent folder... but I did, then picked out the ones I needed, placed them in the right folder and it worked! Also note that you have to put the tab indicator xml (given in the answer) into your drawable folder, as this is the convention whenever an xml has <selector> tags.

Setting button drawable for RadioButton using drawable xml not working for checked state

I've got a group of radio buttons, and I want to set the button's background to a solid color when checked. I created a drawable resource, using a selector and item def's like:
<item android:state_checked="true" android:state_pressed="false"
android:drawable="#color/app_tint"/>
with several variations while trying to get it to work. In the layout containing the buttons, I've tried setting both button and background properties (not at the same time, just one or the other in testing) like:
android1:background="#drawable/radio_state"
OR
android1:button="#drawable/radio_state"
I've read several posts, and I feel I'm close, just missing something to get it done. Thanks.
Here's one we did for an app:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_bcnav_ebilling_focus"
android:state_checked="true" />
<item android:drawable="#drawable/ic_bcnav_ebilling_focus"
android:state_selected="true" />
<item android:drawable="#drawable/ic_bcnav_ebilling_focus"
android:state_pressed="true" />
<item android:drawable="#drawable/ic_bcnav_ebilling_focus"
android:state_focused="true" />
<item android:drawable="#drawable/ic_bcnav_ebilling" />
</selector>
Each state has a different drawable, although in this example, we don't really care about all states being very different - just focus=true get a highlighted drawable (it has "..._focus")

Button background change layout on pressed

i'm currently working with the camera api and want to implement a button, which starts and stops video recordings. the button has four different images: video_start_default, video_start_pressed, video_stop_default and video_stop_pressed.
it should change its layout when pressed and after starting/stopping the video.
i created this xml file to do so.
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:state_selected="false"
android:state_pressed="false"
android:drawable="#drawable/video_start_default" />
<item
android:state_selected="false"
android:state_pressed="true"
android:drawable="#drawable/video_start_pressed" />
<item
android:state_selected="true"
android:state_pressed="false"
android:drawable="#drawable/video_stop_default" />
<item
android:state_selected="true"
android:state_pressed="true"
android:drawable="#drawable/video_stop_pressed" />
<item android:drawable="#drawable/video_default" />
</selector>
in order to make the change between start and stop work, i set the select attribute in the onclicklistener
if(videoButton.isSelected())
videoButton.setSelected(false);
else{
videoButton.setSelected(true);
}
basically this works.
while pressing video_start_default, video_start_pressed is correctly shown but the problem is that after releasing video_start_pressed, video_stop_pressed is shown for a millisecond instead of video_stop_default. strangely the problem doesn't occure after clicking video_stop_pressed. here is video_start_default directly visible.
i hope the description is not to confusing
I think you have to make two selectors one for the play state and another for the pause state:
I have a nice tutorial for you that can help you a lot in your project
you have to see this link.
Cheers

Categories

Resources