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

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>

Related

The only image for all button states for 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

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>

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.

question about change the colours of a button when clicked

My purpose is to change the color of a button when click and my codes are
<?xml version="1.0" encoding="utf-8"?>
selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:drawable="#drawable/btn_askering_active" android:state_selected="true" />
<!-- When not selected, use white-->
item android:drawable="#drawable/btn_askering" />
</selector>
It works but if i make a small change like below :
xmlns:android="http://schemas.android.com/apk/res/android">
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When not selected, use white-->
item android:drawable="#drawable/btn_askering" />
<!-- When selected, use grey -->
item android:drawable="#drawable/btn_askering_active" android:state_selected="true" />
</selector>
It does not work anymore....
I need some help...Any comments are welcomed here.Thanks
I think you need to mention stats in selector like pressed or focused and change image accordingly.
Here i have attached sample selector file,have a look and try accordingly.
<?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/estimator_hover_new" />
<item android:state_focused="true" android:state_pressed="true"
android:drawable="#drawable/estimator_hover_new" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="#drawable/estimator_hover_new" />
<item android:drawable="#drawable/estimator_new" />
</selector>
All The Best....

Android highlight ImageButton onclick

Is there a way to highlight an ImageButton when it's pressed?
You can define a drawable via XML and use the selector, like below, to use different (i.e. highlighted) images for different button states:
i.e. res/drawable/button.xml:
<?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>
Use this resource then for the ImageButton view.

Categories

Resources