Button selector on API 10 not working - android

I have the following Button style:
<style name="ButtonmyTimeRed" parent="android:Widget.Button">
<item name="android:minHeight">48dip</item>
<item name="android:minWidth">64dip</item>
<item name="android:textColor">#ffffff</item>
<item name="android:background">#drawable/button_selector_red</item>
</style>
<style name="ImageButtonmyTimeRed" parent="android:Widget.ImageButton">
<item name="android:background">#drawable/button_selector_red</item>
</style>
with the following selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--<item android:state_window_focused="false" android:state_enabled="true" android:drawable="#drawable/button_shape_red" />-->
<!--<item android:state_window_focused="false" android:state_enabled="false" android:drawable="#drawable/button_shape_disabled" />-->
<!--<item android:state_focused="true" android:state_enabled="true" android:drawable="#drawable/button_shape_red" />-->
<item android:drawable="#drawable/button_shape_red_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/button_shape_red" android:state_focused="true"/>
<item android:drawable="#drawable/button_shape_disabled" android:state_enabled="false" />
<!--<item android:drawable="#drawable/button_shape_red" android:state_enabled="true" />-->
<item android:drawable="#drawable/button_shape_red"/>
</selector>
My shape looks like:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#color/red" />
</shape>
Everything works fine on Api level 14 and above. I don't have custom values-xx folders, so I'm wondering why my button has no background on the API 10 device.
My base theme is:
Theme.AppCompat.Light
and I'm using:
compile 'com.android.support:appcompat-v7:22.0.0'
The strange thing is that my ImageButton has the correct background.
UPDATE:
Seems "solid" does not work on onlder devices. When I put:
<stroke
android:width="3dp"
android:color="#color/red" />
<solid android:color="#color/red" />
I get a red border but no background

Related

Change the color of a disabled button in android

Is there a way to change the color of a disabled button in android through styles or some other form ?
I currently have the following,
drawable/button_default.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_default_shape"/>
</selector>
drawable/button_default_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/button_default_background"/>
<corners android:radius="3dp"/>
</shape>
values/styles.xml
<style name="AppTheme.Button">
<item name="android:background">#drawable/button_default</item>
<item name="android:textColor">#android:color/white</item>
<item name="android:textAllCaps">false</item>
<item name="android:paddingTop">10dp</item>
<item name="android:paddingBottom">10dp</item>
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
<item name="android:gravity">center</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">17sp</item>
<item name="android:textAppearance">#style/CustomFontAppearance</item>
</style>
You'll have to use a selector for different drawables in those states.
You can create a selector like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/your_enabled_drawable" android:state_enabled="true" />
<item android:drawable="#drawable/your_disabled_drawable" android:state_enabled="false" />
<!-- default state-->
<item android:drawable="#drawable/your_enabled_drawable" />
</selector>
Specify the color in selector for android:state_enabled="false" as drawable
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false">
<shape>
<solid android:color="#32ff09" />
</shape>
</item>
</selector>
And apply this drawable resource to background of button
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/drawble_name"
android:enabled="false"
android:text="Selector Applied Button" />
Try this -
drawable/bg_button.xml :-
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/bg_button_focused" android:state_selected="true"/>
<item android:drawable="#drawable/bg_button_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/bg_button_disabled" android:state_enabled="false" />
<item android:drawable="#drawable/bg_button_normal"/>
</selector>
After this just set background on your button like this -
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg_button"
android:text="Button"/>
UPDATE: For Android MaterialButton
For Android MaterialButton there are two solutions, one for SDK >= v21 and another for SDK < v21.
Here I am also giving how to change the text color of the button depending whether it is enabled or disabled.
First create two color selector .xml file in your color folder. One for button color and another for button text color. For example button_state_colors.xml for button color and button_text_state_colors.xml for text color.
Here are those two color files:
button_state_colors.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:color="#303F9F" />
<item android:state_enabled="false" android:color="#BBBBBB" />
</selector>
button_text_state_colors.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:color="#FFFFFF" />
<item android:state_enabled="false" android:color="#555555" />
</selector>
Now for all versions of Android SDK you can change the color of button and it's text using style like:
in style.xml:
<style name="Widget.AppTheme.MaterialButton" parent="Widget.MaterialComponents.Button">
<item name="backgroundTint">#color/button_state_colors</item>
<item name="android:textColor">#color/button_text_state_colors</item>
</style>
in layout.xml:
<com.google.android.material.button.MaterialButton
android:id="#+id/button"
style="#style/Widget.AppTheme.MaterialButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Only for SDK >= 21 you can direct change button color without using style like:
<com.google.android.material.button.MaterialButton
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/button_state_colors"/>
Another way to achieve this is to create a color selector.
Create a file
res/color/your_color.xml
which looks like
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/colorDisabledBackground" android:state_enabled="false" />
<item android:color="#color/colorEnabledBackground" />
</selector>
Then you may use it as a regular color
in a style:
<style name="YourStyle">
<item name="android:background">#color/your_color</item>
<item name="android:textColor">#android:color/black</item>
</style>
As well is in layouts, shapes or in code as etc.
Here is my code which works properly with button enable and disable state.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:drawable="#drawable/ic_button_gradient"/>
<item android:state_enabled="false" android:drawable="#color/gray"/>
</selector>

What's causing styles to don't work with API level 16?

So I applied a custom style to my buttons, it works ok with API level 22, but when I test it with API level 16 all my custom styles are gone. What's happening here?. I am an android beginner so probably it's a common behavior but I couldn't find any answer.
Styles are split into 4 xml's representing button states: Pressed, focused, disabled and enabled. I grouped them in 1 xml called button.xml and applied it in styles.xml
button_pressed.xml (focused, disabled and enabled are similar)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="90"
android:endColor="#004B8D"
android:startColor="#0865B7" />
<size
android:height="65dp"
android:width="65dp"/>
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
<stroke
android:width="1dp"
android:color="#FFFFFF" />
<corners android:radius="3dp" />
</shape>
button.xml
<?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>
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name ="android:buttonStyle">#style/button</item>
</style>
<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">#000000</item>
<item name="android:shadowDx">0</item>
<item name="android:shadowDy">-1</item>
<item name="android:shadowRadius">0.4</item>
<item name="android:textSize">20sp</item>
<item name="android:textStyle">bold</item>
<item name="android:background">#drawable/button</item>
<item name="android:focusable">false</item>
<item name="android:clickable">true</item>
<item name="android:fontFamily">sans-serif-condensed</item>
</style>
If you are using appcompat-v7 22.2.1 than it will use material design due to this reason it will be not supported in API 16.
for further information see design support library description
may this answer help to solve your issue.
Not all styles are supported out of the box on older android versions.
Thats why you could probably notice folders such as values -14 values-16 values-21
Without you providing actual styles code, we cannot help you further.

Button not showing background 9 patch image

I am using a background 9 patch image name button_normal.9.png. The image is not showing as a background image of button. Got stuck for 5 hours still couldn't find it. Please help. Here are the respective files.
This is button:
<Button
android:id="#+id/s_id"
android:text="#string/s_id_string"
android:layout_marginTop="20dp"
style="#style/MainScreen"/>
Style MainScreen:
<style name="MainScreen">
<item name="android:layout_width">200dp</item>
<item name="android:layout_marginTop">10dp</item>
<item name="android:textSize">15dp</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textStyle">bold</item>
<item name="android:background">#drawable/button_shape</item>
<item name="android:textColor">#drawable/button_text_color</item>
</style>
button_shape file:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#drawable/button_shape_normal"
android:background="#drawable/button_normal"
android:state_pressed="false"/>
<item
android:drawable="#drawable/button_shape_pressed"
android:state_pressed="true"
android:background="#ed2020"
/>
button_text_color file:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:color="#ed2020"/>
<item android:state_pressed="true" android:color="#ffffff"/>
button_shape_pressed & button_shape_normal file is same:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="30dp" />
try this button_shape file:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#drawable/button_shape_pressed"
android:state_pressed="true"
android:background="#ed2020" />
<item
android:drawable="#drawable/button_shape_normal"
android:background="#drawable/button_normal" />
</selector>

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>

Styled my buttons. Now what? I need to add icon

I was able to style button to my liking with pure XML (no image). Here is how it looks:
shape_button_normal_blue.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="7dip"/>
<gradient android:startColor="#color/blue_start" android:endColor="#color/blue_end" android:angle="270" />
<stroke android:width="1dip" android:color="#80036990" />
<padding android:left="5dip" android:right="5dip" android:top="7dip" android:bottom="7dip"/>
</shape>
Similar to that I described other shapes for pressed/disabled/etc
Here is my selector_button_blue:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="#drawable/shape_button_normal_blue" />
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="#drawable/shape_button_disabled" />
<item android:state_pressed="true"
android:drawable="#drawable/shape_button_selected" />
<item android:state_focused="true" android:state_enabled="true"
android:drawable="#drawable/shape_button_focused_blue" />
<item android:state_enabled="true"
android:drawable="#drawable/shape_button_normal_blue" />
<item android:state_focused="true"
android:drawable="#drawable/shape_button_normal_blue" />
<item android:drawable="#drawable/shape_button_disabled" />
</selector>
And finally my style:
<style name="MyBlueButton" parent="#android:style/Widget.Button">
<item name="android:background">#drawable/selector_button_blue</item>
<item name="android:layout_marginLeft">1dp</item>
<item name="android:layout_marginRight">1dp</item>
<item name="android:layout_marginTop">5dp</item>
<item name="android:layout_marginBottom">5dp</item>
<item name="android:textColor">#color/white</item>
<item name="android:textSize">18dp</item>
<item name="android:textStyle">bold</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">fill_parent</item>
</style>
I'm using those buttons just fine with text. Happy and proud :)
Now I need to put icon on them. I see there is drawingTop/Bottom/Left/Right but it doesn't do what I need. What do I miss here? How do I make it nice and centered?
<Button style="#style/MyBlueButton" android:layout_marginTop="0dp" android:text="" android:layout_width="0dp" android:layout_weight="0.15"
android:drawableTop="#drawable/ic_accept"/>
use imagebutton, something like Edited
>
<ImageButton
android:id="#+id/call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_accept"
android:background="#drawable/selector_button_blue"
/>

Categories

Resources