Fancy Button with animation - android

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"/>

Related

Want to add an indicator when a button is focused

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>

Samsung button click effect

While I was creating Calculator app same as installed in my S6 edge, I stuck when
design buttons. I want to make buttons look exactly same as calc app which comes in all samsung phones.I tried state button pressed but they have design like fading colors in and out for seconds which I ain't sure how to create.
by simple way create new xml file called clickable_button.xml
inside it write this code
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_clicked" android:state_pressed="true"/>
<item android:drawable="#drawable/button"/>
</selector>
don't for get to make two drawable files one seem clicked and another not clicked
finally set your button background with clickable_button ;)

How to add a click effect to a button on click event?

How would I add a click effect similar to the below image for a button click event?
The button in my activity already has it's background set to an image so I'm not sure how I would add a background of a state also as in this tutorial:
Android Button color changing on onClick?
You need two image, one for normal state and another for pressed state. At first create a selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/pressed_image" android:state_pressed="true"/>
<item android:drawable="#drawable/normal_image"/>
</selector>
Add this selector in your button as a background.
Here's something similar. I am not sure though, if this fits your solution.
EDIT: It uses selector, just like #Raghunandan suggested above.

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

custom android button look and feel

The two buttons in the bottom of the screen are Scan and Leads, if we click on any button, the view of the button is visible like pressed as u see in image for lead button, this view will change according to the button click, and here i am unable to create that kind of look and feel for buttons. please provide me the sample code or suggest me to achieve this look and feel.
Thanking you
this is custom button .A custom button is very easy to create, you just have to make different images for the different state of the button and place it into your res/drawable folder.
below link contain the step by step description for creating this:
http://androidemulator.wordpress.com/2011/11/03/creating-custom-buttons-in-android-applications/
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_some_name_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/button_some_name_default"/>
</selector>
Have this in your /drawable/ folder as button_some_name.xml
Then have the button_some_name_pressed.png & button_some_name_pressed.png in your drawable-ldpi drawable-mdpi etc
There are other states to consider as well
ref: http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

Categories

Resources