Borderless Button on Pre-Lollipop with Support Library - android

I am making borderless flat button using support library (23.0.1). It works normal on Lollipop. However on pre-Lollipop when I press button Its color changes to colorButtonNormal color like it's a normal button.
I don't think so it's a normal behaviour and focused color should be grey like on Lollipop.
Here's the screenshot from Lollipop and Pre-lollipop.
First normal behaviour on Lollipop:
Borderless button in normal state and focused state on Lollipop
Not normal behaviour on Pre-Lollipop (Desire color is gray like above but it's not):
Borderless button in normal state and focused state on Pre-lollipop
Theme
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
//other stuff
<item name="colorButtonNormal">#color/orangeColor</item>
<item name="buttonBarButtonStyle">#style/BorderlessButtonStyle</item>
</style>
<style name="BorderlessButtonStyle" parent="Widget.AppCompat.Button.Borderless">
<item name="android:textColor">#color/blueTextColor</item>
</style>
And now button in layout:
<Button
android:id="#+id/btnForgotPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/forgot_your_password"
style="?attr/buttonBarButtonStyle"
/>
Any way to get it write using AppCompat Theme and styles without of making separate Drawables.

Borderless Button works on both Post and Pre Lollipop version with Support Library but there's a small difference between their onPressed color.
Pre-Lollipop: By default onPressed color is same as default Button color set using colorButtonNormal.
Lollipop: By default onPressed color is light grey, which is ideal.
You can make a Borderless Button like this:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Name"
style="#style/Widget.AppCompat.Button.Borderless"/>
Now If you want to have the same onPressed color on all versions then you can set colorControlHighlight in a new theme and set that theme on Button.
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Name"
android:theme="#style/BorderlessButton"
style="#style/Widget.AppCompat.Button.Borderless"/>
And theme in your style:
<style name="BorderlessButton" parent="Theme.AppCompat.Light">
<item name="colorControlHighlight">YOUR COLOR</item>
</style>
Updated: You can use android:theme attribute for a View since Android 5.0 Lollipop and AppCompat v22.1.0 (and higher).

Adding style="?borderlessButtonStyle" to the Button worked fine for me.

why do you worry with some things just go with this and be free
<Button
android:id="#+id/btnForgotPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/forgot_your_password"
android:background="#drawable/abc_btn_borderless_material"
/>
and now careless about api stuff

You are using the android implemented style of "buttonBarButtonStyle" since you are calling it via ?atr - use style="#style/BorderlessButtonStyle instead.
Edit: Leave your xml as it is, but you can change it to your wanted behaviour like this:
AppCompatButton button = (AppCompatButton) findViewById(R.id.btnForgotPassword);
ColorStateList colorStateList = new ColorStateList(new int[][] {{0}}, new int[] {0xFF969696});
//969696 is your wanted grey color, just change it
button.setSupportBackgroundTintList(colorStateList);

Related

How to tint Button using the Latest AppCompat

I need to know what is the best (and recommented) way to tint a Material Button (AppCompatButton) using the latest AppCompat (23.2.1 for the time being). I could have never imagined that it would be so frustrating! I tried most of the answers from here but either they wouldn't work or worked with unexpected results.
I need to keep backwards compatibility to api >= 9 And just need the ripple effect to be applied to >=21 nothing fancy. So what is the best way so far?
I'd appreciate if you could provide both xml and java code.
There are many ways to do that.
My favorite is the following:
<Button
android:id="#+id/activity_main_some_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/Widget.AppCompat.Button.Colored"
android:text="This is a button" />
This automatically tints the button with the accent color you (hopefully) set in your theme, while maintaining pressed states at API < Lollipop and Ripple >= Lollipop.
If nothing else works you could just tint the button yourself:
AppCompatButton myExampleButton = new AppCompatButton(getContext());
myExampleButton.setSupportBackgroundTintList(ContextCompat.getColorStateList(getContext(),
R.color.some_color));
Update
You can do the following to use a self defined color:
<style name="MyButtonTheme" parent="Widget.AppCompat.Button.Colored">
<item name="colorButtonNormal">#color/someColor</item>
</style>
Define a new style with the desired color.
<Button
android:id="#+id/activity_main_some_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="#style/MyButtonTheme"
android:text="This is a button" />
Set it to your button.

Appcompat v7 Material checkbox changing color to default black in Lollipop devices

I am using appcompat v7 material checkbox. My project theme is light blue, so I am assigning light blue for my checkbox checked color in styles.xml as follows
<!--checked color-->
<item name="colorAccent">#color/light_blue</item>
<!--un checked color-->
<item name="android:textColorSecondary">#color/secondary_text</item>
In My layout file
<CheckBox
android:id="#+id/chk_tick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/abc_btn_check_material"
android:layout_gravity="center"/>
Everything works fine with the versions below kitkat's, but the problem arises only with lollipop versions ( By default it is automatically assigning black color ). I really dont know why it is happening. Kindly please help me with your solutions. Thanks in advance
Remove this line:
android:button="#drawable/abc_btn_check_material"
Your CheckBox should be styled automatically, without setting android:button property.
Use AppCombat check box for best result
<android.support.v7.widget.AppCompatCheckBox
android:id="#+id/chkSelected"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
/>
After this go to styles.xml and create new styles as follows
<style name="SampleTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorAccent">#color/colorAccent</item>
<item name="android:textColorSecondary">#color/red</item>
</style>
This is not done yet go to your manifest xml
<activity
android:name=".yourActivity"
android:theme="#style/SampleTheme" />
This Works on pre_lollipop devices also. Happy coding :)

How to apply backgroundTint to AppCompatButton in Dialog?

I have a DialogFragment that contains a Button:
<Button
android:id="#+id/btn_wishlist_rename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:theme="#style/WishlistDialogButton"
android:text="#string/rename" />
The theme is defined like this:
<style name="WishlistDialogButton" parent="Widget.AppCompat.Button">
<item name="android:textColor">#color/white</item>
<item name="colorButtonNormal">#color/default_blue</item>
<item name="backgroundTint">#color/default_blue</item>
</style>
When I just embed the Fragment within the layout of my Activity everything works fine: I get a Material Design raised button with white text and blue background.
However, when the Fragment is used as a dialog the backgroundTint is not applied anymore: I get a Material Design raised button with white text but with grey background.
Is there a way to fix this - preferably without creating all the drawables from scratch or importing some third party library?

Material design : coloring two buttons with two colors in Android

I've looked around for a proper way of handling the background color of Buttons in android 5.0 and the only solution I could find was to define a style for the Button in values-21/styles.xml :
<item name="android:colorButtonNormal">#2196f3</item>
Is this the only way of coloring a button, while preserving both its design and the ripple effect ?
If yes, It would imply that I have to define a custom theme for each Button which has a different color, really ???
N.B : My question doesn't relate to backwards compatibility and AppCompat, which has already been discussed a lot.
You can use the tint option to change coloring that way, instead of making a seperate theme for each button, you would just have each tinted a different color. See here: Material Design Drawables
Yes, you have to define a separate theme with colorButtonNormal for each color. You can set theme to your Button as following:
<style name="ColorButonButton" parent="Theme.AppCompat.Light">
<item name="colorButtonNormal">#color/newColor</item>
</style>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text"
android:theme="#style/ColorButonButton"/>

Android lollipop change checkbox color

I have few checkbox's at my application with different colors.
Now when I applied material design theme checkbox's without style changed their color to "colorControlActivated", checkbox's with style left the same.
Also I wanted to change colors of my other checkbox's without drawables files.
I tried to set style with different "colorControlActivated" color
<style name="CheckBox.Custom" parent="#style/Theme.Material.Blue">
<item name="colorControlActivated">#color/custom_color</item>
</style>
but it's not working, also I tried to create checkbox like:
ContextThemeWrapper ctw= new ContextThemeWrapper(getActivity(), R.style.CheckBox_Info);
Checkbox cb = new CheckBox(ctw);
and color didn't changed.
The same code working with SwitchCompat control.
Do somebody now how to set different colors to checkbox's without drawables?
Thank you!
This did the trick for me. Notice the android namespace.
<style name="CheckBox.Custom" parent="#style/Theme.Material.Blue">
<item name="android:colorControlActivated">#color/custom_color</item>
</style>
You can set color directly in the xml. Use buttonTint for the box: (as of API level 23)
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#color/CHECK_COLOR" />
You can also do this using appCompatCheckbox v7 for older APIs:
<android.support.v7.widget.AppCompatCheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:buttonTint="#color/COLOR_HERE" />
Add this line in your styles.xml file:
<style>
<item name="android:colorAccent">#color/custom_color</item>
</style>

Categories

Resources