I'm trying to achieve buttons similar to the icons in the Action Bar, i.e. transparent images that change background colour on click.
These are the conditions I'd like to satisfy:
Background colour changes on click
Contains a small rectangle shape in the center
Rectangle can change colour programmatically
I tried using a Drawable to represent the rectangle and then to set it as the background of the button, but it expanded to the edges of the button and so there was no background colour to change when clicked (I was able to use drawable.setColorFilter() and button.setBackground(drawable) to alter its colour however). Shrinking the button also shrank the touch-target.
I also tried using a StateListDrawable containing two rectangle shapes, a background and an inner rectangle, so on state_pressed the background rectangle would change colour. However the front rectangle stretched again and completely covered he background rectangle.
Which method can achieve my conditions? Thanks.
Have 2 images for the state of the buttons and use this:
boolean clicked = false
Button btn = (Buttton)findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
if (clicked)
{
clicked = false;
btn.setBackgroundResource(R.drawable.button_image_on);
}
else
{
clicked = true;
btn.setBackgroundResource(R.drawable.button_image_off);
}
}
});
This talk from Google IO 2013 on Android UI design by Nick Butcher and Roman Nurik helped to solve the problem.
The XML attribute style="?android:borderlessButtonStyle" can be set on an ImageButton (or even just a Button) to give it a transparent background which lights up in a standard Holo blue colour on touch. According to Roman Nurik, this attribute provides the standard styling "all for free".
I created an ImageButton containing that borderless button attribute in my layout.xml file, then I created my coloured Drawable in Java and put it inside the button using myImageButton.setImageDrawable(myColouredDrawable); to fit all three of my conditions.
Edit July 2013: To bring the ActionBar pattern to older versions of Android using Jake Wharton's ActionBarSherlock, use style="#style/Widget.Sherlock.ActionButton", or if using the ActionBarCompat Support Library, use style="#style/Widget.AppCompat.ActionButton"
Update May 2017: For ImageViews (I believe it is actually an ImageViewCompat) I am using android:background="?android:attr/selectableItemBackground" on Jellybean onwards.
Related
What do the Material Design Guidelines say about ImageButtons (not toggle buttons)?
I cannot find anything about them here: https://material.google.com/components/buttons.html.
How should they look?
When should they be used and when?
How should they behave?
Example: I have navigation buttons left/right to switch through days and I wonder if they are conform with Material Design.
Found this from source of Decompiled ImageButton class.
Displays a button with an image (instead of text) that can be pressed or clicked by the user. By default, an ImageButton looks like a regular {#link android.widget.Button}, with the standard button
background that changes color during different button states. The image on the surface of the button is defined either by the {#code android:src} attribute in the {#code } XML element or by the {#link #setImageResource(int)} method.
To remove the standard button background image, define your own background image or set the background color to be transparent.
To indicate the different button states (focused, selected, etc.), you can define a different image for each state. E.g., a blue image by default, an orange one for when focused, and a yellow one for when pressed. An easy way to do this is with an XML drawable "selector."
Is it possible to set a custom background to a floating action button? For example, I'd like the fab to have background color with a gradient, and the "plus" icon on top of that. I've only seen simple background color so far. I'd like to keep using the design support library from Google.
You set background color to Floating Action Button through this property:
android:backgroundTint= "#color/colorPrimary"
and this does not accept #drawable. Which means you can not apply any drawable so my answer is NO, you cannot apply gradient to fab.
According to Google Buttons: Floating Action Button documentation, Google suggests to avoid such designs.:
Have a custom xml for the background and have a drawable for the src. With this you might be able to solve your problem. Hope this helps.
I have start and end date time fields that look like in picture below. Blue color shows active selection - if we select right then blue color goes to right and white color appears on the left and vice versa.
Hardest part is to create that arrow style in the middle. What should i use because buttons are rectangles and I don't know how it could be done.
It's easy. Just use ninepatch.
You can start there:
http://radleymarx.com/blog/simple-guide-to-9-patch/
you need a custom image according to what you ask, maybe extend ToggleButton for that but basically what you want is the same component when pressing on two different places on the same component will dispatch different events.
I would for example extend Linear layout, make it vertical layout, put two clickable textView\buttons with 9 patch images as background ,like suggested before, one for the white part and the other for the blue part. and replace the background image for any event u like.
you can also create a StateListDrawable with actions for selected and deselected and set the state of each drawable upon the right click
I have an ImageButton that I switch the images when pushed. The problem is that the animation tints the button blue when I push the button between my two image switches. How to I prevent that blueish tint from animating? Also, how do you do it programatically and not through the XML.
Well for most use cases, you should be using XML... but lecture aside, the blue background is part of a StateListDrawable that is set by default as your button's background. Defining and setting your own background (via setBackgroundResource(int resId) or similar) should be sufficient to "get rid" of the focused color state.
In my Android app, I'm trying to get buttons to be tinted a certain color (either blue, red or gold). The default button tint is grey. Does anyone know how to change the color of the button so that the transparency remains with it?
I tried using:android:background
but that ditches the transparency completely and makes the button one solid color.
I also tried using:android:#color/transparent
but that ditches the color completely in favor of a fully transparent button. I've looked online for a while and found mostly stuff relating to image buttons. The kind that I'm trying to tint is just the standard Button buttons. I'm also guessing that this can be done mostly just through XML. Is that correct?
You could try and create your own button drawable like so...
Take the button drawable from the Android SDK ([ANDROID_SDK_HOME]/platforms/data/res) and import the drawables into your project. Then create a layer list with the button drawable on the bottom and a semi-transparent color on top.