I am creating simple application in Android and i want to change the focus color.That my application consist of two text box. When moving focus to the textbox,i can get the focus of textbox with the orange color. is it possible to change the color ? If it is possible please tell me how to do it ?
Yes, you can change the focus color of a button (this an open source OS). I poked around the SDK and could not find a direct way to change the focus color of a button. I did find this tantalizing xml file...
widget_button.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:color="#ff000000"/>
<item android:color="#ff000000"/> <!-- unfocused -->
</selector>
...but I'm having a hard time figuring out where to adjust things. Button extends TextView, and based on the TextView source that's where the focus color is defined.
If you really want to this you need to make your own class from source.
PS: Since this is so hard I suspect it's discouraged by the Andoird team. It makes for a less consistent user interface.
Related
I am currently developing an android TV app and have configured a basic button on the main fragment. At the current time there is no way of indicating wether you are focusing on, or knowing you can click on this button. I created a view with a drawable background, to use as the indicator; then wrote some code (Kotlin). At first I wrote: indicator.isVisible=button.isFocused to no avail, I also tried earlier in the code to set (as a test) button.isFocused = true this also did not work. After, some research I realised that you could set the button to focused by default. After inserting this into the code, there was of course an issue with the API usage (26 or above); mine being API 22. So, I'd like to know for an API below 26 (in my case 22) how can I make the indicator visible when the button is focused? Once again, so the user knows what they're currently hovering over (especially in rows with many selectable views)
Update:
Well, looking at the View documentation, I found out there is a setOnHoverListener method and a OnHoverListener interface. I haven't tried it but I think this one is what you are looking for.
Old answer:
You can create a new xml drawable with different background colors for each state (hover, pressed, default) and set that drawable as the button's background.
The new drawable would look like this, i.e :
<?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/blue" />
<item android:state_focused="true" android:drawable="#color/gold" />
<item android:drawable="#color/grey" />
</selector>
I am trying to set up a selector for TextView textColor using the following code:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:color="?android:attr/textColorTertiary" />
<item android:color="?android:attr/textColorPrimary"/>
</selector>
However, the color always appears to be red instead of those theme colors. If I put hardcoded color, everything seems to work fine. (ex. <item android:state_enabled="false" android:color="#666666" /> ).
What is the problem and how to solve it? P.S. if anyone knows how to set theme's default disabled color for disabled item in the list, please share, that is what I am trying to achieve. Thanks.
As far as i can see you may have to use 3 states in a selector.
state enabled
state focused
state pressed
in exactly this order. This might help
You used selector for what reason?
If you want to make your text of text view always red then no need of selector. Just define color in color.XML or in string.XML using add color.
And if you want to chanhe it on selection or focus than use the states.
state enabled
state pressed
state focused
Than it will work as you need.
I've seen there are many different "themes" for the Android, depending on the device. For example on my HTC WildFire the highlight color is a "lime green", and that of the emulator is orange.
Is there anyway to know what are the main interface colors of the device in which my app is running?
So i can set (for example) TextViews background colors to match the device theme.
EDIT: You told me this is not possible so...
Is there any way to draw a simple rectangle with the highlight color? Maybe a void button?
Thanks!!
It's not actually a specified color, the drawables themselves are actually replaced on these modified Android skins that HTC, Samsung, etc. put out. So programmatically, there's no direct way to know what the color scheme will be. Your best bet would be to simply define your own drawables for your widgets with your own color scheme, or even reuse the defaults from stock Android, but copy them to your app's drawable folder, and set them into a StateListDrawable, and apply these to your widgets. This will ensure that you get the same color style on all platforms, with the disadvantage being that your app will not match the scheme of the rest of the skin. Depending on your app's layout, that will likely not be a problem, though.
I don't think that there is a way for you to get what the default highlight color is but you can certainly set your own by using the "textColorHighlight=#aarrggbb" attribute on your TextView within the layout.
As far as I know, there isn't a way (I don't see how there could be really). I guess you could have a few different "themes" in your own app and let the user pick one, to easily atleast somewhat match the rest of their setup.
You can use the #android:drawable/list_selector_background (or code form: android.R.drawable.list_selector_background) on your views. It has each of the default selector states. You can also create a selector yourself in the drawable folder like this (named something like default_highlight.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/list_selector_background" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#android:drawable/list_selector_background" /> <!-- focused -->
<item android:drawable="#android:drawable/list_selector_background" /> <!-- default -->
</selector>
And change any of those that you might want to customize. Then you use it like any drawable by setting the background of a View to that (default_highlight or whatever you call it).
I'm new to Android and just starting the very basics. I implement my custom button skin using .9.png images for norma/focus/pressed states. It works fine, but I noticed that after a pressed the focussed button it visually "lost" focus and draws the normal state frame. I planned to use different state images to highloght what button is selected right now, but it seems that it would not work. I noticed also that the same happens with the default LAF button. Is it OK, or it's just emulator issue? What the good workaroud can be used?
Thanks
I think the following may help. I wanted to have one of the buttons in a list of button to be coloured differently, to highlight the fact you were already in that section.
My buttons android:background field was set to the following drawable (drawable/my_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:state_focused="true" android:drawable="#drawable/btn_focused" />
<item android:state_selected="true" android:drawable="#drawable/btn_selected" />
<item android:drawable="#color/transparent" />
</selector>
You'll noticed i've got an item with the android:state_selected="true" attribute set.
Then in code you can have
Button mybtn = (Button)findViewById(R.id.my_btn_1);
mybtn.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Button btn = (Button)findViewById(R.id.nav_secondary_1);
btn.setSelected(true);
}
});
I'm not sure if you can set the selected stat of a Button through a property in the xml. Not sure you would want to.
The order of the item's are also important as it can change the visibility of the other states. The current order will allow you to see the pressed and focused states. however, if you moved the selected item to the top you would find that your pressed and focused states would not be displayed.
I am not sure if you can combine the pressed, focused and selected states to allow for more customised graphics. I haven't tried it but the following would allow for more complicated state based graphical layouts.
<item android:state_selected="true" android:state_focused="true" android:drawable="#drawable/btn_selected_focused" />
Read up on Selectors here http://developer.android.com/guide/topics/resources/drawable-resource.html
This is the default behavior in touch mode, and you should not seek to tamper with it. This is how your users will expect for your app to behave. If you set the focus without touching the screen, such as when using the trackball that's available on most devices, it will indeed remain in focus, but in touch mode there's no visual representation for the state of having focus.
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.