The only image for all button states for Android? - android

I have an image for my button.
In order to make use of it I have to have 1 additional image for each state:
1. disabled
2. selected
3. pressed
etc..
in iOS all those additional states are handled automatically and deferred from original image provided.
Is it possible to accomplish this for Android?

Is it possible to accomplish this for Android?
No, It is NOT. You need to have all states's images with you to define the selector
You can define button_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selected state -->
<item android:drawable="#drawable/button_bg_selected" android:state_selected="true"></item>
<!-- Pressed state -->
<item android:drawable="#drawable/button_bg_pressed" android:state_pressed="true"></item>
<!-- Disabled state -->
<item android:drawable="#drawable/button_bg_disabled" android:state_enabled="false"></item>
<!-- Normal state -->
<item android:drawable="#drawable/button_bg_normal"></item>
</selector>
Then simply assign this selector as a background of a button
<Button
android:id="#+id/button1"
android:background="#drawable/button_selector"
android:layout_width="200dp"
android:layout_height="126dp"
android:text="Hello" />
Refer : Button Selector
Hope it will help you ツ

For selected and pressed you can use an xml file with root for button's background but for disabled I guess you need to handle it in java code. I'm not sure about disabled state.

You will have to use selector for android.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/pressed"
android:state_pressed="true" />
<item android:drawable="#drawable/focused"
android:state_focused="true" />
<item android:drawable="#drawable/normal" />
</selector>
android:background="#drawable/selector_button" />
just have a look of this link.

As far as I know you need to handle this states in a new resource file called cursor: e.g:
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/edittext_selector" android:layout_height="fill_parent"
android:layout_width="fill_parent">
<!-- Image display in background in select state -->
<item
android:state_pressed="true"
android:drawable="#drawable/edittextback1">
</item>
<!-- Image display in background in select state -->
<item
android:state_enabled="true"
android:state_focused="true"
android:drawable="#drawable/edittextback2">
</item>
<!-- Default state -->
<!--<item android:state_enabled="true"-->
<!--android:drawable="#drawable/your_ninepath_image">-->
<!--</item>-->
</selector>

This answer refers to how you can handle button states in Android
You can create a button layout file separately in put that in res > layout folder
For Instance:
if the layout file name is btn.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/btn_pressed" />
<item android:drawable="#drawable/btn_normal"/>
</selector>
You can set BackgroundResource of a Button
yourButton.setBackgroundResource(R.layout.btn);
EDIT
In addition you can refer to this link which is more closer to answer your question

Related

How do you change the drawable image on a button with text to reflect pressed state in XML?

I want to create a Button with text and an image, where both the text and image change when the Button is in the pressed state. All of the other questions about Buttons and images addressed changing the background in the pressed state, but none commented on changing the image drawable in the foreground.
You need a state selector for both the text color of the button and the image used as the drawable.
Here's how you do it:
layout/my_layout.xml:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/bottom_action_delete"
android:drawableLeft="#drawable/test_button_drawable"
android:textColor="#color/link_text_red"
android:text="Test Button"
android:onClick="buttonDelete" />
drawable/test_button_drawable.xml: (bottom_action_delete_image and bottom_action_delete_image_pressed are PNGs in drawable-hdpi/)
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="true" >
<item
android:state_window_focused="false"
android:drawable="#drawable/bottom_action_delete_image" />
<item
android:state_pressed="true"
android:drawable="#drawable/bottom_action_delete_image_pressed" />
<item
android:state_focused="true"
android:drawable="#drawable/bottom_action_delete_image_pressed" />
<item
android:drawable="#drawable/bottom_action_delete_image" />
</selector>
color/link_text_red.xml: (link_text_focused_red_v2, link_text_pressed_red_v2, and link_text_red_v2 are defined in values/colors.xml)
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:color="#color/link_text_red_v2"
android:state_window_focused="false" />
<item
android:color="#color/link_text_focused_red_v2"
android:state_focused="true"/>
<item
android:color="#color/link_text_pressed_red_v2"
android:state_pressed="true"/>
<item
android:color="#color/link_text_red_v2" />
</selector>
If you need something even more complex, you can use the attribute
android:duplicateParentState="true" in child layout elements of the Button, and its pressed state will be passed down the hierarchy.

Android:Styling ListView

I want to put some style in the ListView in such a way that when an item of ListView is hovered, the color of that list item changes. I dont have any idea about how to achieve this.
Please suggest some other ideas about styling the ListView as well.
Regards,
Use selector to define what should happen when you focus the item, select the item and press the item, I doubt you cannot hover over a view, since Android devices use touch screen interface and not a pointing interface like a mouse..
This should help you:
Create a XML file in drawable folder named listselector.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="#color/blue" /> <!-- focused -->
<item android:state_focused="true" android:state_pressed="true" android:drawable="#color/blue" /> <!-- focused and pressed-->
<item android:state_pressed="true" android:drawable="#color/green" /> <!-- pressed -->
<item android:drawable="#color/white" /> <!-- default -->
</selector>
Then set the background of your TextView which you use in ListView to this like
android:background = "#drawable/listselector",
that should do it.

Making a button look pressed in

I'm using Eclipse to write the android application. I've added some standard buttons from the Form Widgets tab and successfully got them opening new windows which display additional buttons.
I would like the button that was pressed, to change appearance and continue to look pressed in after it is selected.
create xml file using the button image like this
<?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/transparent" />
<item
android:drawable="#drawable/closebutton" />
</selector>
add the color folder in values
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="transparent">#00000000</color>
</resources>
can use selector
<?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/button_settings" />
<item android:state_focused="true"
android:state_pressed="true"
android:drawable="#drawable/button_settings" />
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/button_settings_selected" />
<item android:drawable="#drawable/button_settings" />
</selector>
now set this drawable on background property of button in XML, now in coding take a boolean flag when button is pressed set the flag and set the bacground of the button(selected image) and on again click reset flag value and change the imageBackground to the selector again, thats it!!!
Use ImageButton and change the background of the button.
you can use this kind of images depends of you need. Like this https://web.archive.org/web/20160429124711/http://www.devahead.com/blog/2011/08/creating-a-custom-android-button-with-a-resizable-skin/

How do I get the android icons to change state (highlights)?

I am using the Android SDK icon-button for refresh (ic_menu_refresh) in a widget and I need to change the selection state when it is pressed. How is this done? Do I define an XML for the button?
You define the different states in xml via selector.
Sample (esp. see the state-attributes):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/bg_catlocfilter" android:state_pressed="false" />
<item android:drawable="#drawable/bg_catlocfilter_dark" android:state_pressed="true" />
<item android:drawable="#drawable/bg_catlocfilter" android:state_focused="false" />
<item android:drawable="#drawable/bg_catlocfilter_dark" android:state_focused="true" />
</selector>

How to assign an image to a button?

I want to assign an image to one of the buttons in my activity?
How can i achieve that??
And for that where shall I place image.jpeg or any other image file?
Use ImageButton.
Put your image in resources, and use it like this:
<?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/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/button_focused" /> <!-- focused -->
<item android:drawable="#drawable/button_normal" /> <!-- default -->
</selector>
its pretty old question but seems like you didn't get best solution on this post because still haven't accepted any answer.
So if we need a "button with Image" then we have two options
either we can go for Image Button as 'Orsol' mentioned in his answer
or simply you can Add Normal Button and then set Image to Background using Properties
code will be like this
<Button
android:background="#drawable/square_1_up"
android:layout_height="62dp"
android:layout_width="65dp"
android:id="#+id/button1">
</Button>
for this we need to put button image in Drawable folder named "square_1_up.png"
output Button will be like
Save these file under res/drawable/button.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/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/button_focused" /> <!-- focused -->
<item android:drawable="#drawable/button_normal" /> <!-- default -->
</selector>
Suppose your main layout file main.xml which is under res/layout/main.xml
<LinearLayout>
<ImageButton
android:src="#drawable/button.xml"
android:layout_width="fill_parent"
android:layout_heght="wrap_content"
android:id="#+id/button1"
/>
</LinearLayout>

Categories

Resources