Selector tag in android - android

I am working on the home screen application, where I am trying to do some changes to existing one. I downloaded code from Mydroid folder. And when analyzing that I found that they used selector tag in XML file, but I couldn't understand where exactly they used that to achieve its functionality.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="#drawable/ic_launcher_allshow" />
<item android:state_checked="true" android:drawable="#drawable/ic_launcher_allhide" />
</selector>
It is present in res/drawable folder.

A selector tag basically looks for the state of the UI at the time and displays the appropriate image.
This particular drawable is for a check box, when the checkbox is in the state
android:state_checked="false"
(i.e. when the checkbox is not checked)
it uses this image:
#drawable/ic_launcher_allshow
Therefore , checked:
android:state_checked="true"
uses
#drawable/ic_launcher_allhide
See here:
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
&
http://developer.android.com/reference/android/graphics/drawable/StateListDrawable.html

Related

Android - Enable Button click on disabled state (android:enabled="false")

I have a ListView and a Button. The button should always be clickable, but the button background image should be disabled(grayed) when the ListView is empty and enabled(actual background) when the ListView has items.
I know this can be achieved from code by always setting the state enabled as true and changing the background image. But I am looking to achieve this in XML using selector.
Take a look at the following code:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:constantSize="true" android:dither="true">
<item android:drawable="#drawable/button_enabled" android:state_enabled="true" />
<item android:drawable="#drawable/button_disabled" android:state_enabled="false" />
</selector>
Use this code in your drawables folder (as an XML resource).
Afterwards you use this drawable and set it as a background property to your button. (android:background="#drawable/your_selector_file" without the .xml extension, of course)
If you need more information just consult the following link: http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

Setting button drawable for RadioButton using drawable xml not working for checked state

I've got a group of radio buttons, and I want to set the button's background to a solid color when checked. I created a drawable resource, using a selector and item def's like:
<item android:state_checked="true" android:state_pressed="false"
android:drawable="#color/app_tint"/>
with several variations while trying to get it to work. In the layout containing the buttons, I've tried setting both button and background properties (not at the same time, just one or the other in testing) like:
android1:background="#drawable/radio_state"
OR
android1:button="#drawable/radio_state"
I've read several posts, and I feel I'm close, just missing something to get it done. Thanks.
Here's one we did for an app:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_bcnav_ebilling_focus"
android:state_checked="true" />
<item android:drawable="#drawable/ic_bcnav_ebilling_focus"
android:state_selected="true" />
<item android:drawable="#drawable/ic_bcnav_ebilling_focus"
android:state_pressed="true" />
<item android:drawable="#drawable/ic_bcnav_ebilling_focus"
android:state_focused="true" />
<item android:drawable="#drawable/ic_bcnav_ebilling" />
</selector>
Each state has a different drawable, although in this example, we don't really care about all states being very different - just focus=true get a highlighted drawable (it has "..._focus")

Set image of disable state for custom toggle in android

I make a custom toggle button in android, checked state and unchecked state are work fine with two image, now i want set disable image for disable state of toggle button. How i do that? Thanks!
This is my code
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="false" android:drawable="#drawable/record_off" />
<item android:state_checked="true" android:drawable="#drawable/record_on" />
</selector>
Make these changes in your selector
<item android:drawable="#drawable/record_off" android:state_checked="false" android:state_enabled="true"/>
<item android:drawable="#drawable/record_on" android:state_checked="true" android:state_enabled="true"/>
<item android:drawable="#drawable/record_disabled" android:state_enabled="false"/>
I am using this: #drawable/record_disabled merely as an example. You can have your own name for the drawable which you will have to create just like you must have done with the ON and OFF buttons. ;-)
Note that I have added an android:state_enabled="true" attribute to all the items.
The simplest thing you can do in such cases, is go to the location where you have your SDK placed. Then choose the platform you are building for. Go to the data\res\drawable folder and look at how Google does it.
Since you are customizing a Toggle button, search for this: btn_toggle_holo_dark or btn_toggle_holo_light in the drawable folder.

Android Button lighting border

Is it any way to light the borders of a button whilte pressing on it (you can see the effect when you place an ImageView and click on it). Needed to say, that i have a background on the buttone.
You need to have two images and assign each of them to one button state. You should make a state list drawable: just create an xml file "button.xml" and place it in your res/drawable folder. The xml would look like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/buttonwithglow" />
<item android:state_pressed="false"
android:drawable="#drawable/buttonwithoutglow" />
</selector>
Use a StateListDrawable.

My radio button background is transparent, how do I make it opaque?

Update 1:
I followed the directions provided by Archimedes below. Problem is that my API 9 "drawable" folder already has an XML doc titled "btn_radio.xml" and the code inside is exactly the same as you presented above. Now, when I go back to eclipse and tell it:
`<RadioButton android:background="#drawable/btn_radio" ... />`
I get an error message in return saying Error: No resource found that matches the given name (at 'background' with value '#drawable/btn_radio_on').
So what I did instead is I went to the API 8 folder and looked found the drawable_hdpi and mdpi folders, did a search for btn_radio_off and btn_radio_on and copied those two images into the corresponding API 9 folders. This fixed the issue in Eclipse of radio button backgrounds not showing up. However, when I run my code, the virtual device still displays the old broken (transparent) image.*
Original Post:
Hi, I'm following a Gingerbread 2.3 (I'm running Win7/Eclipse) tutorial from this site and I'm looking at this image here: http://www.vogella.de/articles/Android/images/first50.gif
Each time I add a new radio button to my graphical layout in Main.xml the silver part of the radio button is transparent. So regardless of the background color of the main window, I will only see the outline of a radio button and that radio button will be filled in with the color of the entire background.
If I create a grouped item and put two radio buttons in a group, I can get toggle between the two and the green indicator showing which one is toggled is available but the buttons themselves are transparent.
How do I make my radio button look like the one in the image above? It seems that no matter how many times I start a "new" project, the problem persists. The only thing that affects it is toggling another "theme" but that's not really a solution once I run the app.
Thanks for your help.
This appears to be a new Gingerbread (2.3, API level 9) graphical style for the RadioButton drawable's constituent pngs.
The easiest fix is simply to not use Gingerbread... go to your project properties, select the "Android" tab, and change the "Project Build Target" to API Level 8 or lower.
This is a workaround I believe should work for you:
1). Find 'btn_radio_off.png', 'btn_radio_off_pressed.png', 'btn_radio_off_selected.png', 'btn_radio_on.png', 'btn_radio_on_pressed.png', 'btn_radio_on_selected.png' images at 2.2 sources. You can find them in your SDK subfolders. Note, there are several sets of the images (hdpi, mdpi, etc.). Recreate the file structure related to those files inside of your 'drawable' directory.
2). In your 'drawable' directory create the 'btn_radio.xml' file with the following content:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_window_focused="false"
android:drawable="#drawable/btn_radio_on" />
<item android:state_checked="false" android:state_window_focused="false"
android:drawable="#drawable/btn_radio_off" />
<item android:state_checked="true" android:state_pressed="true"
android:drawable="#drawable/btn_radio_on_pressed" />
<item android:state_checked="false" android:state_pressed="true"
android:drawable="#drawable/btn_radio_off_pressed" />
<item android:state_checked="true" android:state_focused="true"
android:drawable="#drawable/btn_radio_on_selected" />
<item android:state_checked="false" android:state_focused="true"
android:drawable="#drawable/btn_radio_off_selected" />
<item android:state_checked="false" android:drawable="#drawable/btn_radio_off" />
<item android:state_checked="true" android:drawable="#drawable/btn_radio_on" />
</selector>
This is actually the same state list that OS uses for radio buttons.
3). Then inside of your layout xml declare your custom radio button using the above state list drawable by using android:background attribute:
<RadioButton android:background="#drawable/btn_radio" ... />

Categories

Resources