Want to add an indicator when a button is focused - android

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>

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).

adding style to a android borderless button (android 4.0+)

I want to add a border to a borderlessbutton. However, if I create my own style with the parent borderlessbutton and I overwrite the background tag I loose the state change animation etc, as this is also defined by the background. I also cannot implement them myself as the native android drawables are not available as public and therefore not accessible. I do not want to have to copy the drawables.
Is it only possible to overwrite ondraw progammatically or is there an xml based solution i am missing?
(btw this is for a periodic table so this should not involve having an xml file for each button as there are about 100 of them)
thanks
stephan
The bulk of this comes from this article.
There is a 4 step process to doing something like this:
Create an XML file that contains the states.
Create an XML file (Or background) for each state
Create the style of the button
Add the button to your layout, and see what it looks like.
The single most difficult thing is the first step, so I'll show that one here. For the other ones, you can visit the site or do your own thing. Essentially, it will look something like this. Basically needs to capture all of the 4 states. This should be saved in the drawable folder, and the name of this is what your application will use for the name of the drawable.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="false"
android:drawable="#drawable/loc_for_button_disabled" />
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="#drawable/loc_for_button_pressed" />
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="#drawable/loc_for_button_focused" />
<item
android:state_enabled="true"
android:drawable="#drawable/loc_for_button_enabled" />
</selector>
An easy way would be to place your button within a separate layout, e.g. a LinearLayout and give this layout a background color and a padding of e.g. 1dp. This would render a "border" around the button. Note, that this is quite costly in regard of layouting, so do not use this method when you have lots of buttons.
The correct solution would be to create your one drawables for all states and build a statelist drawable with your drawables and assign this statelist drawable as your button's background. Actually it's not that much work, just have a look at http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

Fancy Button with animation

I want to create a 2-state-switch button (on/off) like in doubletwist alarm clock
-> http://www.design-by-izo.com/wp-content/uploads/2012/03/Screenshot_2012-03-30-20-11-30.png
When pressing the button it switches its caption with a fancy slide-in effect.
I already know about android animations, but anyhow I dont get this work within a single view.
EDIT: is there any way to do this, prior to ICS? (without taking ICS' code...)
Any Idea?
You're looking for an android switch - It's already been done for you. You just need to supply your own custom images for the switch's style
There is no slide effect on Android like on iPhone. However doing a two-state btn is easy. Just create an xml file like this and put the drawables in the folder:
<?xml version="1.0" encoding="utf-8"?>
<item android:drawable="#drawable/my_btn_pressed_bkgnd" android:state_pressed="true"/>
<item android:drawable="#drawable/my_btn_disabled_bkgnd" android:state_enabled="false"/>
<item android:drawable="#drawable/my_btn_unpressed_bkgnd" android:state_enabled="true"/>

Get ToggleButton drawable from selector

I have a ToggleButton that uses a selector to choose between 2 images (checked and unchecked).
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="#drawable/img_piece1" /> <!-- pressed -->
<item android:drawable="#drawable/img_piece2" /> <!-- default/unchecked -->
</selector>
Is there a way to retrieve the current drawable resource (checked/unchecked) of the toggle button in code? I tried using the ToggleButton.getBackground(), however this only seems to return the default Drawable always.
I figured out how to do this and so am answering my own question. :)
Apparently the current drawable can be retrieved with ToggleButton.getBackground().getCurrent()
Hope it helps.
i was in same situation few days ago..
but made my way out by giving its TEXT ON ="." and TextOFF as "" ie blank...
that dot wont be seem on that image (probably) coz mine did not.
works well
then check the text on toggle button.
Though I haven't ever played with it, you may want to take a look at getDrawableState(). It may have what you're looking for.

Button draws normal state after pressing

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.

Categories

Resources