Default style for all control on Android - android

Is there any method to set the default style for group of control of android like if i set color for checkboxes and also for edittext then it applies like in css
In my app i'm enabling and disabling some control conditionally so i want to change their color can i create a style that change color for all control without applying to each and specific.

I am not aware of such thing in Android but you can create a custom background like this:
<?xml version="1.0" encoding="utf-8"?>
<gradient android:angle="1dp"
android:centerColor="#color/sky_blue"
android:endColor="#color/white"
android:startColor="#color/white" />
Declare this file as say backgrnd.xml in drawable folder and when you want to give background to any view just use: android:background="#drawable/backgrnd"
Each view will then have same background like this.

Have a look at the class ColorStateList

Related

Android Image button selector for all buttons BUT different background in normal state

daily_event_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/daily_event_selected"
android:state_pressed="true" />
This is what I was using for ONE SPECIFIC button but I want to change this for different buttons while using the same selector
<-- <item android:drawable="#drawable/daily_event_normal" />-->
</selector>
So I have this selector and I want to use it on all my ImageButtons
P.S.: it's working
But I want to give a different background to each of my ImageButtons in their "normal" state.
I want to use this as a generic selector to have the same effect on all my Buttons while keeping the background different.
But since the selector is only specified (as per my knowledge) as
android:background="#drawable/daily_event_selector"
in the background property.
So, I can't seem to find a way to add background to the Buttons.
Any idea?
Use different selectors for different buttons.
The selectors will only differ in their normal state.
This is the smartest way I know to do that.
Another way would be to have only 1 selector, and change it's normal state in Java.
But this would really be other-than-smart.
Because you have to do some extra work in code (therefore, eventually slowing down the CPU).

Change text color based on theme in a selector with different states

I try to change the color based on my theme. My TextView is using color-selector with different states for enabled and disabled and I want to use my theme based color in this selector.
I have followed this solution: android themes - defining colours in custom themes
My selector used as android:textColor in my view looks like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:color="#ffffff" />
<item android:state_enabled="false" android:color="?attr/ThemeTest"/>
</selector>
with ThemeTest being my custom attribut which has a color assigned in my themes. If I use this selector as my textColor, the color is actually not what I picked but just a simple plain RED! HOWEVER if I use the custom attribut directly in my view
android:textColor="?ThemeTest"
then it works but I obviously want to do this based on the change of state of my view...
Does anybody understand this behaviour and know how to fix it? Thanks in advance!
Using a theme attribute inside a color selector XML file is only supported in the most recent versions of Android. To overcome this limitation you need to create one color selector file for each theme, and fill them with plain colors. Then create a theme attribute which points to the correct color selector depending on the theme.
source: https://plus.google.com/102404231349657584821/posts/XEeehfwanGy
edit: tested and it works flawlessly!

Android: change button background/text/compoundDrawables with selector XML

I have already been using selector drawables to make my button change background according to the state.
However, I also want to change the text color and left compound drawable together with the background. But the default selector XML atrribute does not contain any "android:textColor" or "android:drawableLeft" to be assigned.
I know I can always achieve this with extend my own button class, but is there any clean way out?
I am not very sure about drawables but for changing textcolor depending upon button state, I use selectors as below,
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:color="#color/color_light_green"></item>
<item android:color="#fff"></item>
//state you want
</selector>
and then apply it to textColor attribute in the xml as,
android:textColor="#drawable/selector_btn_text_color"
Eclipse doesn't auto suggest color attribute in selector but we can do it. :)

Android: Use default highlight colors in a custom button

I want to use a Button in my android app but I want to customize how it looks. However, I want the highlight and selected colors of the button to be the same as the default colors (i.e. the dark and light orange gradients, or whatever the theme color supplies).
Is there anyway to get the default highlight/selected drawables and to use that as the fill for my buttons on the selected and highlighted states?
Thanks!
You are asking for two different things, do you want the drawables or the colorcode?
Anyway, you can find the name of the drawables here: http://androiddrawableexplorer.appspot.com/
I don't know if you can use them directly from your app or if you have to save them to your drawables folder first, but you can find them in your sdk. If you want the colorcodes, use gimp to extract them from the pictures.
It seems that you can use a selector as drawable inside a selector!
(You can or should not use #android:drawable/btn_default_selected, because it is private)
This meens that you can write your own selecter and use the whole default android selector for the items you want the default behavior for.
I used this selector
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#android:drawable/btn_default" android:state_pressed="true"/>
</selector>
And added it to as background to a linear layout.
I don't know why, but this messed up the padding/margin as well, thats why i set them to 0.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/background_linear_layout_button"
android:padding="0dp"
android:layout_margin="0dp"
android:orientation="vertical" >
<!-- YOUR LAYOUT THAT ACTS LIKE A BUTTON -->
</LinearLayout>
The Result is that you have the parent background color in the unpressed state and the android background color for the pressed state.
Selectors are what you're looking for. Google around for tutorials. Here's one.
I Suppose you can find the Default Selector in the Android Source Code.

Fixed: "Android: Detecting focus/pressed color"

I'm trying to detect the focus/pressed color for button and other elements.
This is needed because I'm developing new components and it's important that those look as part of platform.
Those colors are ORANGE on android sdk and GREEN on HTC SenseUI.
If I could detect that color my component will look as part of platform on both version.
Does anyone knows how to do this?
It's possible to create "selector" which uses custom image for default state and platform default for focus/selection.
To do this follow the steps:
1) create xml file with selector in "res/drawable" (e.g. "red_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="#android:drawable/btn_default" >
</item>
<item android:state_focused="true"
android:drawable="#android:drawable/btn_default" >
</item>
<item
android:drawable="#drawable/btn_default_red" >
</item>
</selector>
2) from folder ".../android-sdk-mac/platforms/android-1.5/data/res/drawable/" take picture "btn_default_pressed.9.png" and change color as you like (I needed to change it to red and for this GIMP is enough).
3) place altered picture in "res/drawable" (e.g. with name "btn_default_red.9.png")
4) define button:
<Button
android:id="#+id/info_button"
android:layout_width="wrap_content"
android:layout_height="37dip"
android:layout_marginTop="1dip"
android:background="#drawable/red_button"
android:text="[Info]" />
That's all.
This is result:
alt text http://img200.imageshack.us/img200/1349/custombutton.png
I had this problem too. As already stated, the problem is that the backgrounds aren't simple colors, they're Drawables that could take on all kinds of appearances. However, I found a work-around that may help. If your custom component looks something like an existing one, e.g. a Button or ListView entry, you can just steal their background/selector and set that as the background for your custom component. E.g., in your custom component constructor:
setBackgroundDrawable(new Button(context).getBackground());
or for a background more suitable for list-like components:
setBackgroundDrawable(new ListView(context).getSelector());
You may want to optimise that code somewhat, but you get the idea.
Those aren't colors. They are a few nine-patch images out of a StateListDrawable. I am skeptical that there will be a reliable way for you to determine what the color is, one that will work across all devices and all versions of Android.
This is pretty much a duplicate of: Android ListView Selector Color
Also, why do you need to detect the colours? Just do nothing and your widgets will fit in to the platform's existing look & feel.
Or if you're writing a custom theme, just make yours inherit from android:Theme.

Categories

Resources