ImageButton effect on Android 3.0+ - android

I want to have an onTouch effect like the Home Button on Android 3.0+.
Is there a property to do so easily?
Or I have to do it all myself? And how?
Thanks!
UPDATE:
Here is the screenshot: http://i.imgur.com/1eyVE.jpg
I would like to have the same blue, circle, fade-out effect.

What you're talking about is called the Navigation Bar. The animation and circle effect used when you touch a button are from a custom class that's extending ImageView.
You can view the full source for that class here.
As far as the Holo blue color goes, you can visit the Android Design Guidelines - Color section to look at all of the colors used in Ice Cream Sandwich. Move your mouse over a specific color in the palette to learn the corresponding hex value.
I'll leave this example here in case you decide not to use the KeyButtonView class.
Holo selector example:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="#android:integer/config_mediumAnimTime">
<item android:drawable="#color/holo_blue_dark" android:state_pressed="true"/>
<item android:drawable="#color/holo_blue_dark" android:state_enabled="true" android:state_focused="true"/>
<item android:drawable="#color/transparent"/>
</selector>

Related

Animate a ChoiceChips' background color on selector state change

I'm trying to achieve a choice chip group like shown in the material documentation:
Link to Animation Video
Currently, I'm just using a selector as a background drawable:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="?attr/colorAccent" android:state_enabled="true" android:state_selected="true" />
<item android:color="#color/md_white_1000" />
</selector>
Switching those colors given above shall be animated.
If possible, the animation/transition should be compatible to api 17.
Currently, this is only possible with a custom view.
There's an open issue on MDC to implement animatable backgrounds:
https://issuetracker.google.com/issues/130410732

Why does a Button loses its original behaviour after changing its color?

I am writing an Android app and now I am styling it. I am using a custom theme which is a child of Theme.Holo.Light. I really like Theme.Holo.Light because its buttons have a special effect when you click and hold it. Like the lower button in the picture below:
Not click:
click:
The upper button has been changed color. Now when I click on that button, it doesn't have that effect. I really don't understand why. Can anyone tell me why this happens and how can I get the same effect with a colored button?
And also, the colored button seems fatter.
This is because the button uses a selector to display different colors/effects/drawables based on the state of the click. You can check out the link on Color State List Resource.
To create your own you have to create a slecetor cml file and put it in your drawables folder.
For example.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/shape_btn_default_normal_gray" android:state_enabled="true" android:state_pressed="false"/>
<item android:drawable="#drawable/shape_btn_default_pressed_gray" android:state_pressed="true"/>
<item android:drawable="#drawable/shape_btn_default_disabled_gray"/>
</selector>
or with colors
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/dark_green" android:state_enabled="true" android:state_pressed="false"/>
<item android:drawable="#color/light_green" android:state_pressed="true"/>
<item android:drawable="#color/gray"/>
</selector>
To apply this you have to set the background drawable in your layout xml like this.
<Button
android:id="#+id/my_btn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Some text"
android:background="#drawable/selector_btn_default_gray"/>
That is the "ripple effect" of material design.You have define you own style for that effect.Link below may help you or you may search for many other answers on StackOverflow. Material effect on button with background color
It does not loses its behavior you can see after click (in your second image) the button show same scale as the above have...so by default the background is set as to show that it is button (like with padding or so) and can changes to show oncklick effect...
So when you set your desire background to button...It takes complete change on what is on presently on it and then you have to manually set onclick effect..

Android selector with fade in / fade out duration initially invisible

I'm trying to achieve that an icon in ActionBar will not change states discretely, but by fading animation. When I add android:enterFadeDuration and android:exitFadeDuration to the selector tag, my drawable is initially invisible - when I tap it, it changes state to state_pressed (properly with enter fade duration) and when I release it, it turns back to its normal visible unselected state.
I must be missing something obvious, or is this a bug of some kind?
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:enterFadeDuration="150" android:exitFadeDuration="150">
<item android:drawable="#drawable/filters_toggle_icon_selected" android:state_focused="true"/>
<item android:drawable="#drawable/filters_toggle_icon_selected" android:state_pressed="true"/>
<item android:drawable="#drawable/filters_toggle_icon" android:state_focused="false" android:state_pressed="false"/>
</selector>
I had a similar problem, with my code looking like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:enterFadeDuration="#android:integer/config_mediumAnimTime"
android:exitFadeDuration="#android:integer/config_mediumAnimTime" >
<item android:state_pressed="true" android:drawable="#color/pressed" />
<item android:drawable="#color/default" />
</selector>
At first, I found a hint to get rid of enterFadeDuration and only use exitFadeDuration. That solved the problem with initial invisibility, but the view still faded into invisibility during the first interraction.
Then, I modified my structure as follows:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/default" />
<item>
<selector android:enterFadeDuration="#android:integer/config_mediumAnimTime"
android:exitFadeDuration="#android:integer/config_mediumAnimTime" >
<item android:state_pressed="true" android:drawable="#color/pressed" />
</selector>
</item>
</layer-list>
Basically, I just pushed the default drawable out of the selector. It's a workaround and it also works for selectors with multiple states, but has some notable limitations:
The default drawable is always visible as a bottom layer. It works for opaque colors, but transparency may cause undesirable results.
If the view starts in one of the states tested by selector, in still displays as default, because the selector still starts as invisible.
It might not be applicable to the original problem, but it's something to consider for overcoming this behaviour of selectors.
Use android:enterFadeDuration="#android:integer/config_mediumAnimTime" and android:exitFadeDuration="#android:integer/config_mediumAnimTime".
My problem was similar as well, the issue was that after setting a background drawable on my view, it was in the wrong state (sometimes it even mixed the stroke and solid of two states...). This was only before the first interaction, like receiving focus or changing enabled state.
I've found that if you call jumpToCurrentState() on the drawable (which has the fade duration properties) after setting it on the view, it will be set on the correct state, and you can keep on using the enter / exit fade duration properties.
Here's how I did it:
val stateList = (darkBackground as? RippleDrawable)?.findDrawableByLayerId(android.R.id.background) as? StateListDrawable
background = darkBackground
stateList?.jumpToCurrentState()
In this example I had a ripple drawable which contained the selector which had the fade properties (I had to add an ID to the selector so I could look it up using the ID).
This seems to be a bug that happens on specific Android versions. You can turn off the android:enterFadeDuration programmatically in Java code, by accessing the Selector with a StateListDrawable:
// Disable android:enterFadeDuration/exitFadeDuration on Android 4.2 only
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR1) {
StateListDrawable stateListDrawable =
(StateListDrawable) animatedButton.getBackground();
stateListDrawable.setEnterFadeDuration(0);
stateListDrawable.setExitFadeDuration(0);
}

How to make a non-holo button with DialogFragment

I am building an application to show a dynamic picture and a dialog with a button. The thumbnail of that picture is then used as the background of the button. However, if the picture is with black pattern, it turns out showing as "holo" and transparent to the background. This makes the button ugly.
I am trying this on android 4.x.
I have tried using different themes for the dialog, Theme.Dialog, Theme.Holo and Theme.Light but no luck.
My problems are
(1) how to make a non-holo button with DiaglogFragment(even on a holo theme view/activity).
(2) is this related to android versions? or machines?
Thank you.
you need to create a selector with all the different states of a button and for each state you will need an image
for example
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/blue_curve" android:state_enabled="true" android:state_pressed="false"/>
<item android:drawable="#drawable/blue_curve_pressed" android:state_pressed="true"/>
</selector>
then set the background to the selector in your xml

How can I change the android button to dark grey instead of light grey

I would like to change the color of the android button in my application to be dark grey instead of light grey. I intent to keep the color of the focus/pressed the same. I just want to change the color of the button in 'normal' state to dark grey.
I have found this thread:
Standard Android Button with a different color
I think the easiest way is to do this. Is that correct? But how can I make the LightingColor Filter to make it darkgrey?
button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xFFAA0000));
I have tried
button.getBackground().setColorFilter(new LightingColorFilter(0xffffffff, 0xFF3A3A3A));
But all I get is a white color.
That's similar to what I am doing in my TabHost tabs in XML. If you have custom buttons and graphics it can be done in Java, but it is much easier if you're able to do it in your XML.
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:drawable="#drawable/settings"
android:state_selected="true" />
<!-- When not selected, use white-->
<item android:drawable="#drawable/mountain" />
</selector>
You could use the default light theme
Example

Categories

Resources