How can I make a style file that adds borders to all buttons? All buttons already have a background image.
Create your style in file res/values/styles.xml
<style name="KeyBoardButton">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">match_parent</item>
<item name="android:layout_weight">1</item>
<item name="android:gravity">center</item>
<item name="android:layout_margin">4dp</item>
<item name="android:paddingTop">8dp</item>
<item name="android:paddingBottom">8dp</item>
<item name="android:textAppearance">?android:attr/textAppearanceMedium</item>
<item name="android:textColor">#555</item>
<item name="android:background">#drawable/button_border</item>
</style>
Define the border that will be used in file res/drawable/button_border.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="4dp"/>
<solid
android:color="#d5cbc6" />
<stroke
android:width="2dp"
android:color="#dedede" />
</shape>
</item>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="4dp"/>
<solid
android:color="#f9f9f9" />
<stroke
android:width="2dp"
android:color="#dedede" />
</shape>
</item>
Now you can use it in your layout files, as follows:
<Button android:id="#+id/my_button"
style="#style/KeyBoardButton"
android:text="1" />
Related
Since I followed a Video from "Coding in Flow", I did everything (Link: https://www.youtube.com/watch?v=Z1w3y0saKjY)
And of course, I like to add Custom Buttons into my Game. But the Problem is, that the View shows still the standard button or doesn't even like me (that won't even show in preview). And my laptop doesn't have that much space available, then I can't run on AVD Device. And I'll do the first tests on my 25% development completion.
Still, nothing. This is the code (all 5 codes including styles.xml):
<?xml version="1.0" encoding="utf-8"?>
<!--button_default.xml-->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="0"
android:endColor="#2D0D0D"
android:startColor="#7E0707" />
<padding
android:bottom="8dp"
android:left="8dp"
android:right="8dp"
android:top="8dp" />
<stroke
android:width="2dp"
android:color="#4E2828" />
<corners android:radius="15dp" />
</shape>
<!--button_pressed.xml-->
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="0"
android:endColor="#543838"
android:startColor="#CA7676" />
<padding
android:bottom="8dp"
android:left="8dp"
android:right="8dp"
android:top="8dp" />
<stroke
android:width="2dp"
android:color="#4E2828" />
<corners android:radius="15dp" />
</shape>
<!--button_disabled.xml-->
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#989898" />
<padding
android:bottom="8dp"
android:left="8dp"
android:right="8dp"
android:top="8dp" />
<stroke
android:width="2dp"
android:color="#191919" />
<corners android:radius="15dp" />
</shape>
<!--custom_button.xml-->
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_pressed" android:state_pressed="true" />
<item android:state_enabled="false" android:drawable="#drawable/button_disabled" />
<item android:drawable="#drawable/button_default" />
</selector>
<!--styles.xml-->
<resources>
<style name="AppTheme" parent="#android:style/Theme.Material.Light.DarkActionBar">
<item name="android:colorPrimary">#color/colorPrimary</item>
<item name="android:colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="android:colorAccent">#color/colorAccent</item>
<item name="android:colorControlHighlight">#color/colorControlHighlight</item>
<item name="android:colorControlNormal">#color/colorControlNormal</item>
<item name="buttonStyle">#style/CustomButton</item>
</style>
<style name="FullScreen" parent="#android:style/Theme.Material.Light.NoActionBar.Fullscreen">
<item name="android:colorPrimary">#color/colorPrimary</item>
<item name="android:colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="android:colorAccent">#color/colorAccent</item>
<item name="android:colorControlHighlight">#color/colorControlHighlight</item>
<item name="android:colorControlNormal">#color/colorControlNormal</item>
<item name="buttonStyle">#style/CustomButton</item>
</style>
<style name="NoActionBar" parent="#android:style/Theme.Material.Light.NoActionBar">
<item name="android:colorPrimary">#color/colorPrimary</item>
<item name="android:colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="android:colorAccent">#color/colorAccent</item>
<item name="android:colorControlHighlight">#color/colorControlHighlight</item>
<item name="android:colorControlNormal">#color/colorControlNormal</item>
<item name="buttonStyle">#style/CustomButton</item>
</style>
<style name="NoStatusBar" parent="AppTheme">
<item name="android:windowFullscreen">true</item>
<item name="buttonStyle">#style/CustomButton</item>
</style>
<style name="CustomButton" parent="Widget.AppCompat.Button">
<item name="android:background">#drawable/custom_button</item>
<item name="android:textColor">#FFF</item>
</style>
</resources>
I hope for any answer. It could be the case of "AndroidX" thing.
I am using radioButton in my app but by default my radioButton color is black and when I select it that turns to color accent color that I have mentioned in my color.xml but I want to change radioButton initial color and selected color to white color. How I am able to do so. I tried background color but it changes background color property. Please help.
First Put this code in style.xml
<!-- custom style -->
<style name="radionbutton"
parent="Base.Widget.AppCompat.CompoundButton.RadioButton">
<item name="android:button">#drawable/rb_drawable</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">false</item>
<item name="android:backgroundDimEnabled">true</item>
</style>
Now make rb_drawable.xml in drawable folder
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/radio_unchecked" android:state_checked="false" android:state_focused="true"/>
<item android:drawable="#drawable/radio_unchecked" android:state_checked="false" android:state_focused="false"/>
<item android:drawable="#drawable/radio_checked" android:state_checked="true" android:state_focused="true"/>
<item android:drawable="#drawable/radio_checked" android:state_checked="true" android:state_focused="false"/>
</selector>
Now make 2 more files for radio button check/uncheck in drawable folder
radio_unchecked.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke android:width="1dp" android:color="#color/colorAccent"/>
<size android:width="30dp" android:height="30dp"/>
</shape>
radio_checked.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<stroke android:width="1dp" android:color="#color/colorAccent"/>
<size android:width="30dp" android:height="30dp"/>
</shape>
</item>
<item android:top="5dp" android:bottom="5dp" android:left="5dp" android:right="5dp">
<shape android:shape="oval">
<solid android:width="1dp" android:color="#color/colorAccent"/>
<size android:width="10dp" android:height="10dp"/>
</shape>
</item>
</layer-list>
Finally change style of radio button in layout xml
<RadioButton
android:layout_width="wrap_content"
style="#style/radionbutton"
android:checked="false"
android:layout_height="wrap_content"
/>
I want to change the style of the Button. And I add the style in style.xml then set the #style/BaseButton to every Button.
Is there any way can do it better? Can I just set the theme to change all Button or other views?
I know that I can add the theme to the style.xml but unfortunately, if I set the attributes like #android:background to the Button, other views' backgrounds are set too.
So here are what I need:
Set the style in only one(or some) place(s). No need to set style in
every layout and every <Button />.
Only the Button is influenced while other views stay where they
are.
Can be changed easily.
Follow below steps
Create an XML file that represents the button states
<?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/button_disabled" />
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="#drawable/button_pressed" />
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="#drawable/button_focused" />
<item
android:state_enabled="true"
android:drawable="#drawable/button_enabled" />
</selector>
Create an XML file that represents each button state
First button shape is for the enabled button state.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient
android:startColor="#00CCFF"
android:centerColor="#0000CC"
android:endColor="#00CCFF"
android:angle="90"/>
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp" />
<stroke
android:width="2dip"
android:color="#FFFFFF" />
<corners android:radius= "8dp" />
</shape>
Second button shape is for the focused button state.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient
android:startColor="#F7D358"
android:centerColor="#DF7401"
android:endColor="#F7D358"
android:angle="90"/>
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp" />
<stroke
android:width="2dip"
android:color="#FFFFFF" />
<corners android:radius= "8dp" />
</shape>
Third button shape is for the pressed button state.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient
android:startColor="#0000CC"
android:centerColor="#00CCFF"
android:endColor="#0000CC"
android:angle="90"/>
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp" />
<stroke
android:width="2dip"
android:color="#FFFFFF" />
<corners android:radius= "8dp" />
</shape>
And finally, Fourth button shape is for the disabled button state.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient
android:startColor="#F2F2F2"
android:centerColor="#A4A4A4"
android:endColor="#F2F2F2"
android:angle="90"/>
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp" />
<stroke
android:width="2dip"
android:color="#FFFFFF" />
<corners android:radius= "8dp" />
</shape>
Create an XML file that represents the button style
<resources>
<style name="button" parent="#android:style/Widget.Button">
<item name="android:gravity">center_vertical|center_horizontal</item>
<item name="android:textColor">#FFFFFFFF</item>
<item name="android:shadowColor">#FF000000</item>
<item name="android:shadowDx">0</item>
<item name="android:shadowDy">-1</item>
<item name="android:shadowRadius">0.2</item>
<item name="android:textSize">16dip</item>
<item name="android:textStyle">bold</item>
<item name="android:background">#drawable/button</item>
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
</style>
Create an XML with your own custom application theme .themes.xml below
<resources>
<style name="YourApplicationTheme" parent="android:style/Theme.NoTitleBar">
<item name="android:buttonStyle">#style/button</item>
</style>
</resources>
Now, you can create buttons on your application with the new style
Use this
<style name="ApplicationStyle" parent="android:Theme">
<item name="android:buttonStyle">#style/CKButton</item>
</style>
Linked from:
How do I apply a style to all buttons of an Android application
I have this style on my styles.xml:
<style name="btnStyleGenoa" parent="#android:style/Widget.Button">
<item name="android:textSize">15sp</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">#FFFFFF</item>
<item name="android:gravity">center</item>
<item name="android:shadowColor">#000000</item>
<item name="android:shadowDx">1</item>
<item name="android:shadowDy">1</item>
<item name="android:shadowRadius">0.6</item>
<item name="android:background">#drawable/custom_btn_genoa</item>
<item name="android:padding">10dip</item>
</style>
I also have this custom_btn_genoa.xml on drawable folder:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" >
<shape android:shape="rectangle" >
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#20534e" />
<gradient android:angle="-90" android:startColor="#062d30" android:endColor="#4c898e" />
</shape>
</item>
<item android:state_focused="true">
<shape android:shape="rectangle" >
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#20534e" />
<solid android:color="#125156"/>
</shape>
</item>
<item >
<shape android:shape="rectangle" >
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#20534e" />
<gradient android:angle="-90" android:startColor="#4c898e" android:endColor="#125156" />
</shape>
</item>
</selector>
Finally, i have this button:
<Button
android:text="#string/change_but"
android:textSize="15dp"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginRight="30dp"
android:id="#+id/change_but"
android:onClick="onChangeClicked"
style=”#style/btnStyleGenoa”
/>
The button does not use the desired style. Last line in button (style) says "attribute value expected. Any thoughts?
(Note, i found this code on a website, it's not mine)
You must replace style=”#style/btnStyleGenoa” to style="#style/btnStyleGenoa"
Working like a charm.
I am creating a custom style for a seek bar using xml:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/background">
<shape>
<solid android:color="#00FF00" />
<corners android:radius="0dp" />
</shape>
</item>
<item android:id="#android:id/progress">
<clip>
<shape>
<solid android:color="#FF0000" />
<corners android:radius="0dp" />
</shape>
</clip>
</item>
</layer-list>
<style name="Widget.MySeekBar" parent="android:Widget.SeekBar">
<item name="android:indeterminateOnly">false</item>
<item name="android:progressDrawable">#drawable/seek_bar_progress</item>
<item name="android:minHeight">3dp</item>
<item name="android:maxHeight">3dp</item>
<item name="android:thumb">#drawable/image_seek_bar_thumb</item>
<item name="android:thumbOffset">8dp</item>
<item name="android:focusable">true</item>
</style>
I want the background to be thinner than the progress similar to the standard jelly bean style seek bars. Is there any way to do this?
Thanks
I have used this in xml:
<SeekBar
android:id="#+id/seekID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/linearLayoutComponentTopRow"
android:layout_marginTop="1px"
android:background="#drawable/slide_bar"
android:progress="0"
android:secondaryProgress="0"
android:thumb="#drawable/bt_slider" />
It sets the background and the thumbnail too.
I hope it helps!