Prolonging the focusabilty of a View - android

in my application I'm using instead of Buttons, ImageViews which have a selector as background. They are working fine though the focus is lost really quickly and as a result the effect isn't visible to the user 100%. Is there a way that I can delay the focus which the views receive for like a second? another way that I've tried was to change the ImageView's background when it was touched and change it back on the onPause()
Background Selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/actionbar_background" />
<item android:state_focused="false"
android:drawable="#drawable/normal" />
</selector>
normal.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF"/>
</shape>

Ok this is what I did to overcome it I changed the background selector as
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/actionbar_background" />
<item android:state_selected="true"
android:drawable="#drawable/actionbar_background" />
<item android:state_pressed="false"
android:drawable="#drawable/normal" />
</selector>
and on the function bound by the image view
v.setSelected(true);
selectedView = v;
and on onStop()
selectedView.setSelected(false);
Hope this will help anyone who stumbles upon this post

Related

Background change on button when pressed, isn't working properly

This is my xml file for the button:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/play_pressed" />
<item android:drawable="#drawable/play" />
</selector>
But this only works only one time, when I go to another activity and return then this doesn't work.
When I first run the activity the background of the button changes as it should, but when i go to another activity and return back to the previous one, then the background change while button pressed isn't working....
EDIT:
The problem was in my java code. It was replacing the background to a static png image every-time (except the first time)...The above code works as it is expected.
It should be like below:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="false"
android:state_pressed="false"
android:drawable="#drawable/play_default" />
<item android:state_pressed="true"
android:drawable="#drawable/play_pressed" />
<item android:state_selected="true"
android:state_pressed="false"
android:drawable="#drawable/play_pressed" />
</selector>

Android change imageButton when finger is hover on that

i'm create simple application to work with custom buttom if finger is pressed or released and hover on that. in this simple selector project have 4 state for change background image, but i need to change this button when finger pointer is focus or hover on that but that does not work and only press work correctly in my project.
<?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/logo"/>
<item android:state_focused="true"
android:state_pressed="true"
android:drawable="#drawable/gohome" />
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/goran" />
<item android:drawable="#drawable/logo" />
</selector>
In touch UI there is no such thing as "hover". Hover is when your pointer is over a control. Without having a pointer, you cannot reproduce this step.
You can play with "focused", "pressed" and default.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"
android:drawable="#drawable/logo"/>
<item android:state_pressed="true"
android:drawable="#drawable/goran" />
<item android:drawable="#drawable/logo" />
</selector>

Passing multiple source to single file in #drawable

Currently I have a single button with image bound to it and I wanted to add a click effect on that(not click event, a effect). Well it worked fine. I created two xml in my drawable folder and added styles and gradient to it. But now I want to add few more buttons and apply the same click effect on them. One way I can create multiple xml files for as many buttons created. But what happened to code reuse? I want to reuse the same xml files for styling other buttons too. Can it be done? thanks in advance.
My button.xml file is:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="#drawable/stb" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/gradient" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/gradient" />
<item android:drawable="#drawable/stb" />
</selector>
And my gradient.xml is:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<bitmap android:src="#drawable/stb"/>
</item>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:angle="90" android:startColor="#990f0f10"
android:centerColor="#990d0d0f"
android:endColor="#995d5d5e"/>
</shape>
</item>
</layer-list>
You can set the very same xml selector drawable to as many views as you want. Just set the other button's background via XML:
android:background="#drawable/button"
or code:
yourSecondButton.setBackgroundResource(R.drawable.button);

Define drawable for CheckedTextView for checked and pressed state in Android

I have current code
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="true"
android:drawable="#drawable/list_button_selected" />
<item
android:state_pressed="true"
android:drawable="#drawable/list_button_pressed" />
<item
android:drawable="#drawable/list_button" />
</selector>
for my CheckedTextView and it works. But when it's checked and I press on it, it shows default android color for background. How can I overwrite that? I tried
<item
android:state_checked="true"
android:state_pressed="true"
android:drawable="#drawable/list_button_selected" />
But it does not work.
In my case, order is important. I had to switch checked and pressed and its working now.

Can't use default ListView selector

I'm developing a ListView app for Android. I want to use the default ListView selector, for example the one in Market when you touch an item.
I don't get a highlight in my ListView, where's the way to correctly implement it? I've read about the "Touch mode" although I can't get it to work.
Thanks in advance!
<ListView
android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
You have to assign to each row layout a background composed by a selector that assign a different background for each state. Suppose that your item row layout is a RelativeLayout defined in a xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dip"
android:background="#drawable/transparent_selector"> ...
and transparent_selector is defined here: watch that carefully one drawable shape belongs to a single state.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="#drawable/shape_7"/>
<item android:state_pressed="true" android:drawable="#drawable/shape_7" />
<item android:state_pressed="false" android:drawable="#drawable/transparent_shape" />
</selector>
and a shape is defined here:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#FF4FDBD5"
android:endColor="#FF1AA6A1"
android:type="linear"
android:angle="90"/>
</shape>
or in the other way when you construct the ListView items you can assign the selector to the 'convertView'
cheers
For your id attribute, change it to android:id="#android:id/list. This is the default ID for the ListView.
Try this selector. It works for me
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_selected="true" android:drawable="#android:color/transparent" />
<item android:state_selected="true" android:drawable="#android:color/transparent" />
<item android:state_pressed="true" android:state_selected="false" android:drawable="#android:color/transparent" />
<item android:state_selected="false" android:drawable="#drawable/list_item_back" />
</selector>

Categories

Resources