How to design up arrow in button in xml - android

I want to design like the image given below: when I select any button it should appear up arrow with background color on selected button.

Use button selector for it like this. or follow this link http://www.mkyong.com/android/android-imagebutton-selector-example/
button_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_pressed_yellow"
android:state_pressed="true" />
<item android:drawable="#drawable/button_focused_orange"
android:state_focused="true" />
<item android:drawable="#drawable/button_normal_green" />
</selector>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/imageButtonSelector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/new_button" />
</LinearLayout>

Related

Changing background of button via Selector

I have a buttons and i want when clicked on the button, the button changed image. I think everything is ok but when clicked on the button, background button changed for one moment(while i select this) and then the background button be first background color :( What is my problem??
contact_selector.xml:
<?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/contactpress"></item>
<item
android:state_focused="true"
android:drawable="#drawable/contactpress"></item>
<item
android:drawable="#drawable/contact"></item>
</selector>
activity_test.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="#+id/frame"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/white"
android:layout_above="#id/btn"></FrameLayout>
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/contact_selector"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
Thanks...
// try this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/contactpress" android:state_pressed="true"></item>
<item android:drawable="#drawable/contactpress" android:state_focused="true"></item>
<item android:drawable="#drawable/contact" android:state_enabled="true" android:state_focused="false" android:state_pressed="false"></item>
<item android:drawable="#drawable/contactpress" android:state_enabled="false"></item>
</selector>

Button resizes on > 2.3.6

This is what is happening:
This is the source image:
This is part of the xml layout:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/background_headers"
android:orientation="horizontal" >
<AutoCompleteTextView
android:id="#+id/etNewItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:completionThreshold="2"
android:dropDownHeight="wrap_content"
android:dropDownWidth="wrap_content"
android:hint="#string/et_newitemhint"
android:inputType="text|textCapSentences"
android:layout_centerVertical="true"
style="#style/DialogEditText"
android:layout_marginRight="10dp"
android:singleLine="true" />
<Button
android:id="#+id/btnAddNew"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#drawable/btn_add"
android:layout_centerVertical="true" />
</RelativeLayout>
This is the Selector xml for the button:
<?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/btn_add_pressed"/>
<item android:state_pressed="true"
android:drawable="#drawable/btn_add_pressed"/>
<item android:drawable="#drawable/btn_add_normal"/>
</selector>
Why is this button horizontally resizing in 4.x.x? The source graphic did use to be a 9-patch, but it did exactly the same thing, so I changed it to a normal png, with no result.
The odd thing is is that this is the only graphic which exhibits this behaviour. I have it in an actionbar somewhere: same thing; I have it in a dialog too: same thing.
My 2.2.2 and 2.3.6 test devices show the button as it should be, but any of my test devices of 4.x.x show the elongated button.
First, make these:
res/drawable/button_add_normal.xml
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/btn_add_normal"
android:tileMode="disabled"
android:gravity="top" >
</bitmap>
res/drawable/button_add_pressed.xml
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/btn_add_pressed"
android:tileMode="disabled"
android:gravity="top" >
</bitmap>
Then use this as selector:
<?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/button_add_pressed"/>
<item android:state_pressed="true"
android:drawable="#drawable/button_add_pressed"/>
<item android:drawable="#drawable/button_add_normal"/>
</selector>
I didn't manage to reproduce this on my devices. However, I still have a solution: you can use exact sizes like this:
android:layout_width="50dp"
android:layout_height="50dp"
This way the image should look the same on every device.

My ListView background selector won't highlight the selected row

I used this SO question as a guide, but when I click a row it doesn't stay highlighted. What is wrong with my code?
score_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/score_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/keyboard"
android:listSelector="#drawable/selector"
android:choiceMode="singleChoice"
android:divider="#CCCCCC"
android:dividerHeight="1dp"
android:paddingLeft="15dp"
android:paddingRight="15dp" />
...
</RelativeLayout>
drawable/selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="#color/highlight"/>
<item android:state_pressed="true" android:drawable="#color/highlight"/>
</selector>
score_row_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scoreRowLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:background="#drawable/row_selector" >
...
</LinearLayout>
drawable/row_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="#color/highlight" />
<item android:state_activated="true" android:drawable="#color/highlight" />
<item android:drawable="#color/transparent" />
</selector>
This code now works properly.
Try to use state_activated for your row_selector.

Selector for custom list view "Change ListView background - strange behaviour"

I am using this tutorials and I want to add selectors for this list view. I tried some codes but it doest work. How can I do it.
I used code as list_selector.xml
<?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/list_selector_background_focus" />
<item android:state_pressed="true"
android:drawable="#drawable/list_selector_background_pressed" />
</selector>
and my List view
<ListView
android:id="#+id/select_names_tags_lv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:listSelector="#drawable/list_selector"
android:layout_alignParentTop="false"
android:layout_gravity="center_vertical">
</ListView>
Just change a word here from listSelector to background. Because you want it as the background drawable selector, right?
<ListView
android:id="#+id/select_names_tags_lv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/list_selector"
android:layout_alignParentTop="false"
android:layout_gravity="center_vertical">
</ListView>
EDIT:
try changing your selector file to (I changed the order of the two items):
<?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/list_selector_background_pressed" />
<item android:state_focused="true"
android:drawable="#drawable/list_selector_background_focus" />
</selector>

Android - Image button (XML) isn't working?

I have three images in my drawable folder, and an XML Image Button code:
<?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/happycarrot" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/largerunicornbutton2" /> <!-- focused -->
<item android:drawable="#drawable/largerunicornbutton" /> <!-- default -->
</selector>
In my layout folder, I have my main XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:src="#drawable/button"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
></ImageView>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
</LinearLayout>
When I run it in the emualtor I only get the first image. It doesn't do anything else. Is there anything I can do to solve this?
Using a Linearlayout instead of a relative one fixes the issue.

Categories

Resources