Android cannot set default button style - android

I am trying to set a default background for all buttons in my application. The problem is the android:buttonStyle item does not seam to do anything.
If I set a style of #style/Button to any button, then the style is used but I want this to happen to all buttons that have no style explicitly supplied.
My themes.xml file is:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style
name="BBTheme"
parent="#android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:windowBackground">#drawable/app_background</item>
<item name="android:actionBarStyle">#style/ActionBar</item>
<item name="android:buttonStyle">#style/Button</item>
</style>
</resources>
and my styles.xml file is:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- ActionBar styles -->
<style
name="ActionBar"
parent="#android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">#color/action_bar_background</item>
</style>
<!-- Button styles -->
<style
name="Button"
parent="#android:style/Widget.Button">
<item name="android:textColor">#android:color/white</item>
<item name="android:background">#drawable/button_background</item>
</style>
</resources>

Add the buttonstyles.xml inside your drawable folder call From Button, you can modify the style of button.
<Button
android:id="#+id/button6"
style="?android:attr/buttonStyleSmall"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="15dp"
android:background="#drawable/buttonstyles"
android:text="Cancel"
android:textColor="#fff" />
buttonstyles.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#99d9fa"
android:endColor="#99d9fa"
android:angle="45"/>
<padding android:left="3dp"
android:top="3dp"
android:right="3dp"
android:bottom="3dp" />
<corners android:radius="15dp" />
</shape>

Related

Android Studio buttons wont change colour (will only use colours from themes.xml)

I am fairly new to android studio and I am developing a small app.
I have just added my first buttons but I am having trouble selecting the colour of them.
The code i am using in my activity xml is:
<Button
android:id="#+id/signUpButton"
android:text="SIGN UP"
android:textColor="#FFF"
android:textSize="30sp"
android:layout_marginTop="20dp"
android:layout_width="270dp"
android:layout_height="60dp"
android:background="#drawable/buttonshape"
/>
and the code in buttonshape.xml is:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
android:radius="14dp"
/>
<solid
android:color="#FCB1B1"
/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>
<size
android:width="270dp"
android:height="60dp"
/>
<stroke
android:width="3dp"
android:color="#FCB1B1"
/>
</shape>
For some reason the button will not take on the colour specified in the buttonshape.xml and instead will take on the style from the themes.xml file
themes.xml:
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.Health_Link" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">#color/salmon</item>
<item name="colorPrimaryVariant">#FFF</item>
<item name="colorOnPrimary">#color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">#color/teal_200</item>
<item name="colorSecondaryVariant">#color/teal_700</item>
<item name="colorOnSecondary">#color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowContentTransitions">true</item>
<item name="android:windowDisablePreview">true</item>
</style>
<style name="Theme.Health_Link.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="Theme.Health_Link.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="Theme.Health_Link.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>

Android Theme style not applying for Button

On my application style not applying, It shows like this,
Here is the code I am using,
-----AndroidManifest.xml-----
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
----themes.xml-----
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">#color/white</item>
<item name="colorPrimaryVariant">#color/white</item>
<item name="colorOnPrimary">#color/text_gray</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">#color/white</item>
<item name="colorSecondaryVariant">#color/white</item>
<item name="colorOnSecondary">#color/text_gray</item>
<item name="android:textViewStyle">#style/Regular_TextView</item>
<item name="buttonStyle">#style/Rounded_Gray_Button</item>
</style>
<style name="Rounded_Gray_Button" parent="android:Widget.Button">
<item name="android:gravity">center</item>
<item name="android:minWidth">100dp</item>
<item name="minHeight">40dp</item>
<item name="font">#font/sf_ui_display_bold</item>
<item name="android:textSize">16dp</item>
<item name="background">#drawable/rounded_dark_gray_bg</item>
<item name="android:textColor">#color/white</item>
</style>
<style name="Rounded_Gray_Stroke_Button" parent="android:Widget.Button">
<item name="android:gravity">center</item>
<item name="android:minWidth">100dp</item>
<item name="minHeight">40dp</item>
<item name="font">#font/sf_ui_display_bold</item>
<item name="android:textSize">16dp</item>
<item name="background">#drawable/rounded_dark_gray_stroke</item>
<item name="android:textColor">#color/dark_gray</item>
</style>
</resources>
Button Backgrounds
----rounded_dark_gray_bg.xml----
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<stroke
android:width="1dp"
android:color="#color/dark_gray" />
<solid android:color="#color/dark_gray" />
<corners android:radius="6dp" />
</shape>
</item>
</layer-list>
---rounded_dark_gray_stroke.xml----
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<stroke
android:width="1dp"
android:color="#color/dark_gray" />
<solid android:color="#color/white" />
<corners android:radius="6dp" />
</shape>
</item>
</layer-list>
I am applying the above button style on the below xml file,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:gravity="center"
android:padding="10dp">
<Button
android:id="#+id/btnSignin"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:theme="#style/Rounded_Gray_Button"
android:text="#string/Sign_in"/>
<Button
android:id="#+id/btnRegister"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:theme="#style/Rounded_Gray_Stroke_Button"
android:text="#string/Register"/>
</LinearLayout>
</RelativeLayout>
To apply background we need to replace <item name="background">#drawable/rounded_dark_gray_bg</item> with <item name="android:background">#drawable/rounded_dark_gray_bg</item> on themes.xml
Also to apply we need to use like,
style="#style/Rounded_Gray_Button"
style="#style/Rounded_Gray_Stroke_Button"
You shouldn't set the theme on individual components. You should set the themed attribute you defined: buttonStyle, as the style of the component:
style="?buttonStyle"
... or you can reference the style directly, but then it can't change with the theme (unless the attributes within the style reference themed attributes)
style="#style/Rounded_Gray_Button"

Adding a BackgroundTint on a ToggleButton messes the Background itself

I have a layout with multiple ToggleButtons with a custom layout. Each of them must have a different color.
To achieve that I've created a selector with 2 items in it for the checked and unchecked state. The cheked item has a drawable inside. So I have the xml's like this:
<ToggleButton
android:id="#+id/color_picker_btn_1"
android:layout_width="38dp"
android:layout_height="38dp"
android:textOff=""
android:textOn=""
android:layout_marginEnd="12dp"
android:checked="true"
android:background="#drawable/selector_button_color"/>
The selector_button_color:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/shape_circle_checked" android:state_checked="true"/>
<item android:drawable="#drawable/shape_circle"/>
</selector>
The shape_circle_checked:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"
android:visible="true">
<solid android:color="#color/colorPrimary" />
</shape>
</item>
<item android:drawable="#drawable/ic_check">
</item>
The problem is that when I apply a background tint on the ToggleButton I can't see the drawable on the Checked state.
I would like to know if there is a way to select the background color of each ToggleButton without messing up the background itself with the drawable. Otherwise I will have to create a custom background for every single ToggleButton.
Thank you very much!
To solve it I created an SVG with the circle and the check. That does the trick!
You can use something like:
<com.google.android.material.button.MaterialButton
android:id="#+id/toogleButtton1"
style="#style/circleButton.Green"
android:layout_width="48dp"
android:layout_height="48dp"
/>
<com.google.android.material.button.MaterialButton
android:id="#+id/toogleButtton2"
style="#style/circleButton.Red"
android:layout_width="48dp"
android:layout_height="48dp"
/>
<com.google.android.material.button.MaterialButton
android:id="#+id/toogleButtton3"
style="#style/circleButton"
android:layout_width="48dp"
android:layout_height="48dp"
/>
with:
<style name="circleButton" parent="Widget.MaterialComponents.Button">
<item name="shapeAppearanceOverlay">#style/circle</item>
<item name="icon">#drawable/ic_add_24px</item>
<item name="iconTint">#color/custom_button_icontint_selector</item>
<item name="android:insetTop">0dp</item>
<item name="android:insetBottom">0dp</item>
<item name="android:padding">12dp</item>
<item name="android:checkable">true</item>
</style>
<style name="circleButton.Green">
<item name="materialThemeOverlay">#style/ThemeOverlay.Green</item>
</style>
<style name="ThemeOverlay.Green" parent="">
<item name="colorPrimary">#color/green500</item>
</style>
<style name="circleButton.Red">
<item name="materialThemeOverlay">#style/ThemeOverlay.Red</item>
</style>
<style name="ThemeOverlay.Red" parent="">
<item name="colorPrimary">#color/red600</item>
</style>
<!-- Circular ShapeAppearance -->
<style name="circle">
<item name="cornerSize">50%</item>
</style>
<!-- custom selector for icon tint to hide the icon when unchecked -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="?attr/colorOnPrimary" android:state_checked="true"/>
<item android:color="?attr/colorPrimary" android:state_checked="false"/>
</selector>

How do I make the rounded corners of the menu in the toolbar?

This is my toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="#dimen/_40sdp"
android:background="#android:color/holo_red_dark"
android:gravity="center_vertical"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:popupTheme="#style/PopUpTheme"/>
This is how I customized the popup menu in style.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="PopUpTheme" parent="ThemeOverlay.AppCompat.Light">
<item name="android:popupBackground">#drawable/background_popup_menu</item>
</style>
</resources>
background_popup_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#android:color/white" />
<corners android:radius="5dp" />
</shape>
That's what I get in the end.
But I need to do like this
You can change the background of the overflow menu defining in your app theme the actionOverflowMenuStyle attribute.
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
<item name="actionOverflowMenuStyle">#style/popupOverflowMenu</item>
</style>
With:
<style name="popupOverflowMenu" parent="#style/Widget.MaterialComponents.PopupMenu.Overflow">
<item name="android:popupBackground">#drawable/my_mtrl_popupmenu_background</item>
</style>
Then define the drawable/my_mtrl_popupmenu_background.xml with something like:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="?attr/colorSurface"/>
<corners
android:bottomLeftRadius="8dp"
android:bottomRightRadius="8dp"
android:topLeftRadius="8dp"
android:topRightRadius="8dp"/>
</shape>
But you have rounded corners :) Just try to increase a value of radius like <corners android:radius="25dp" />.

Changing button style in the whole application

I'm trying to change all the TextColor of my buttons in my app to white and also trying to make it bold. But that isn't happening, I'm overwriting the android:Widget.Button
I'm developing for Jelly Bean 4.1.2
What am I doing wrong?
Theme definition in manifest
android:theme="#style/spui" >
The theme where I like to
<style name="spui" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:buttonStyle">#style/Buttonspui</item>
</style>
The style for the button itself
<style name="Buttonspui" parent="android:Widget.Button">
<item name="android:background">#drawable/btn_default_holo_light</item>
<item name="android:minHeight">48dip</item>
<item name="android:minWidth">64dip</item>
<item name="android:textColor">#ffffff</item>
<item name="android:textStyle">bold</item>
</style>
The button
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="#string/edit"
android:id="#+id/btnEdit"
style="#style/Buttonspui"/>
For styling your Button you can use this:
Go to the drawable folder and make an XML (e.g. button.xml) file called "style" which contains the following:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient android:startColor="#449def" android:endColor="#2f6699" android:angle="270" />
<stroke android:width="1px" android:color="#000000" />
<corners android:bottomLeftRadius="0dp"
android:bottomRightRadius="0dp"
android:topLeftRadius="8dp"
android:topRightRadius="8dp"/>
<padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" />
</shape>
</item>
</selector>
This is my code, you can make the necessary changes you want
Now call it in your layout XML (mainactivity.xml) like this
android:background="#drawable/button.xml"
Now to change the font color and style you can use the following which comes as part of the styles.xml in the values folder
<style name="buttonStyle" parent="#android:style/Widget.Button.Small">
<item name="android:textColor">#FFFFFF</item>
<item name="android:textSize">12sp</item>
<item name="android:textStyle">bold</item>
</style>
Now call this in the layout XML (mainactivity.xml) like this
style="#style/buttonStyle"
The final code is:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="#string/edit"
android:id="#+id/btnEdit"
android:background="#drawable/button.xml"
style="#style/buttonStyle"
/>
Hope this helps :)
This will change the default button style for the entire app, include alert dialog button. Adjust the style based on yours.
res/values/styles.xml
<resources>
<!-- Your base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Other styles -->
<item name="buttonStyle">#style/MyCustomButton</item>
</style>
<style name="MyCustomButton" parent="Widget.AppCompat.Button">
<item name="android:background">#drawable/selector_button</item>
<item name="android:textColor">#color/buttonText</item>
<item name="android:textStyle">bold</item>
<item name="android:paddingLeft">16dp</item>
<item name="android:paddingRight">16dp</item>
</style>
</resources>
res/drawable/selector_button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_selected" android:state_pressed="true"/>
<item android:drawable="#drawable/button_selected" android:state_focused="true"/>
<item android:drawable="#drawable/button_unselected"/>
</selector>
res/drawable/button_selected.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="1000dp"/>
<solid android:color="#color/buttonSelected"/>
</shape>
res/drawable/button_unselected.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="1000dp"/>
<solid android:color="#color/buttonUnselected"/>
</shape>

Categories

Resources