Problem with selector for a ListView in Android - android

I have created a ListView and applied a selector to it as follows
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/my_btn_pressed" />
<item android:state_focused="true"
android:drawable="#drawable/my_btn_focussed" />
<item android:drawable="#drawable/my_btn_normal" />
</selector>
When focussed or pressed, the background of the ListView item comes as specified in the selector. But the default background is never applied, can you tell me what is wrong?
By the way, this is the customised row xml I've used:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:textSize="20sp" >
</TextView>
Thanks,
Kiki

The solution was presented as an answer to this question.

Try using this code:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:state_focused="false" android:drawable="#drawable/my_btn_pressed" />
<item android:state_focused="true" android:state_pressed="false" android:drawable="#drawable/my_btn_focussed" />
<item android:state_focused="false" android:state_pressed="false" android:drawable="#drawable/my_btn_normal" />
</selector>

In your Textview, you should try to change this...
android:background="#drawable/<b>my_btn_normal</b>"
to this...
android:background="#drawable/<b>your selector's name</b>"

Related

Create custom flat switch android

I want to make one custom switch as shown below.
I have created two drawable resource for that one is switch_bg and another is tracker_bg.
switch_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/thumb_on" android:state_pressed="true" />
<item android:drawable="#drawable/thumb_off" android:state_enabled="false" />
<item android:drawable="#drawable/thumb_on" android:state_checked="true" />
<item android:drawable="#drawable/thumb_off" />
</selector>
tracker_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_tracker" android:state_enabled="false" />
<item android:drawable="#drawable/ic_tracker" android:state_pressed="true" />
<item android:drawable="#drawable/ic_tracker" android:state_checked="true" />
<item android:drawable="#drawable/ic_tracker" />
</selector>
and in main xml I put following code.
<Switch
android:id="#+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:layout_marginRight="20dp"
android:thumb="#drawable/switch_bg"
android:track="#drawable/track_bg"/>
Please guide me What I am doing wrong or What things are missing from my side.
Try this sample it may help you.Click here

Effect for ImageButton in android

I want to create effects for ImageButton. For example, it will change color when clicked...How I do it? I want to do this in .xml file. Can you help me! Thank you!
I tried to create the state.xml file as follwing:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="#drawable/btn_0" />
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="#drawable/btn_ac" />
</selector>
However, I can't set background for ImageButton. The error like this:
All you have to do it to add the "android:background" attribut to your ImageButton and set a drawable.
Your layout
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/my_btn"
android:background="#drawable/btn_drawable"/>
btn_drawable.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/blue"
/>
<item android:state_focused="true"
android:drawable="#drawable/white"
/>
<item android:drawable="#drawable/green" />
</selector>
In that code above you set a different drawable when your ImageButton is pressed (state_pressed), focused (state_focus) or when is normal (not pressed and not focused).
Here you can find with more detail.
Create a drawable like below and name it as btn_drawable.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="false"
android:drawable="#drawable/btn_disable" />
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="#drawable/btn_click" />
</selector>
This is for an example,Like this you can add the <item/> according to your needs and state of the image button.
Then set the drawable as image button background.
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/btn_drawable"/>

Android TableRow background

I have a problem with a TableRow, which I add dynamically.
private void addRow(String body) {
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
TableRow row = (TableRow) inflater.inflate(R.layout.customrow,null);
TextView name = (TextView) row.findViewById(R.id.customName);
name.setText(body);
row.setOnLongClickListener(this);
}
I would like this row to change color upon onClick and onLongClick.
Code in the customrow.xml file is:
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tableRow1"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:gravity="center_vertical"
android:onClick="showOnClick">
<TextView android:id="#+id/customName"
android:textSize="25px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_weight="5">
</TextView>
</TableRow>
I have tried to use android:background="#drawable/clickedbackground"with the row but it is not working.
Code in the clickedbackground.xml file is:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:state_focused="false"
android:state_pressed="false" android:drawable="#android:color/transparent" />
<item android:state_selected="false" android:state_focused="false"
android:state_pressed="false" android:drawable="#android:color/transparent" />
<item android:state_pressed="true" android:drawable="#color/custom" />
<item android:state_focused="true" android:state_selected="true"
android:state_pressed="false" android:drawable="#color/custom" />
</selector>
Anyone knows what I am doing wrong (color/custom is defined in another xml which works)?
Thank you
You are creating object for tablerow named row. and you have also clickedbackground.xml file. just use below code in addRow method.
row.setBackgroundResource(R.drawable.clickedbackground);
I think it solves your problem.
In your addRow() method you're inflating the row but you're not adding it to any parent layout, and as row is a local variable I think you're not doing it anywhere else, is it a copy/paste problem?
Again, your customrow.xml might be not working because the opening TableRow tag lacks the closing >, but it might be copy/paste problem.
Using android:background="#drawable/bg" with bg being a selector is a common pattern and it should work. You might want to simplify your selector: you don't need to specify all the states for each item and all the combinations. It works with a "first match", so this will do the job:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#color/custom" />
<item android:state_selected="true" android:drawable="#android:color/transparent" />
<item android:state_focused="true" android:drawable="#color/custom" />
<item android:drawable="#android:color/transparent" />
</selector>
Also, notice that selected and focused are two different states, focused being the one you get when moving around with dpad.
If this didn't help please specify what "is not working" means: what do you expect? what's happening instead?
Adding
<resources>
<style name="row" parent="#android:style/Widget.Button">
<item name="android:background">#drawable/rows</item>
</style>
</resources>
in styles.xml and setting
style="#style/row"
in the TableRow did the job.
where the rows.xml in the drawable is
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="false"
android:drawable="#color/blue" />
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="#color/custom" />
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="#color/white" />
<item
android:state_enabled="true"
android:drawable="#android:color/transparent" />
</selector>
do not forget to add to the style
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
Otherwise, you will not be able to use the states of a rowLayout.

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>

Android ImageButton state not changing

I have an issue with my ImageButton not changing state. When I click, or rather touch, the button it stays as the same image. Here is the XML I am using as a selector.
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_focused="true"
android:state_pressed="false"
android:drawable="#drawable/pushed" />
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="#drawable/pushed" />
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/pushed" />
<item
android:drawable="#drawable/default" />
</selector>
I call this selector from my main.xml as
android:background="#drawable/imagechoice"
imagechoice.xml is the file with the selector
I don't understand why this is not working, unless I have to have some java code, but everything I've seen said this should work.
When using an ImageButton, isn't it the 'src' property you should use and not background?
Make sure that you copy the same images and the button XML into every "drawable" folders (hdpi,ldpi,mdpi).
That's how I solved this problem on my app.
Good luck :)
I have nearly the same XML and it works just fine. Are you sure you're not replacing the drawable in code somewhere?
On another note, your XML can be simplified by using the cascading nature of the state matching.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/pushed"
/>
<item android:state_focused="true"
android:drawable="#drawable/pushed"
/>
<item android:drawable="#drawable/default"
/>
</selector>
This is my xml of a button with my own custom image on it and it works great:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="#drawable/btn_off" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/btn_pressed" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/btn_pressed" />
<item android:drawable="#drawable/btn_off" />
</selector>
Make sure you set Image button background as mentioned below.I think you are not setting the selector as background instead you are setting the image as background.
<Button
android:id="#+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button_up_selector"
android:text="1"
android:textColor="#fffafa"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"/>

Categories

Resources