ToggleButton dissapears when make background transparent - android

I try to make some XML layout in Android Studio. I need my app to be compatible with API 10 and higher. My layout have some switch buttons but Switch is for API 14 or higher. It is why I decide to use Toggle Button instead.
I have made custom selector drawable with two states. And problem starts now.
When I make XML like this:
<ToggleButton
android:id="#+id/reminderRepeatToggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/toggleButton"
android:button="#drawable/ic_toggle_bg"
android:background="#android:color/transparent"/>
It works perfectly on android higher than API 10 but on 10 ToggleButton simply disappear.
I also tried something like this:
<ToggleButton
android:id="#+id/reminderRepeatToggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/toggleButton"
android:background="#drawable/ic_toggle_bg"/>
and it works on API 10 but it looses it proportions. Any idea how to overcome this? Or maybe you advice different approach to the problem? (not making Toggle but something different).
Thanks for help!
style:
<style name="toggleButton">
<item name="android:buttonStyleToggle">#style/Widget.Button.Toggle</item>
<item name="android:textOn">#string/blank</item>
<item name="android:textOff">#string/blank</item>
</style>
<style name="Widget.Button.Toggle" parent="android:Widget">
<item name="android:background">#drawable/ic_toggle_bg</item>
<item name="android:disabledAlpha">?android:attr/disabledAlpha</item>
</style>

Related

After migration to AndroidX android:button is not respected for API below Lollipop

I have very simple checkbox:
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/clipboardBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/clipboard_checkbox" />
For checked/unchecked I have two different images.
After migration to AndroidX I see default image for Android on devices below API 21.
What I've tried already:
change CheckBox to AppCompatCheckbox (from AndroidX) - nothing changed
set background to checkbox & set android:button="#null" - background is OK, but I still see default image on background (see image below)
Seems that Android completly disrespect button attribute.
I am out of ideas. For Lollipop+ everythink works as it should. Have anyone faced issue like this?
Only change I did was migration to AndroidX :/
In the appcompat theme, the checkBoxStyle below API 21 is defined as
<style name="Base.Widget.AppCompat.CompoundButton.CheckBox" parent="android:Widget.CompoundButton.CheckBox">
<item name="android:button">?android:attr/listChoiceIndicatorMultiple</item>
<item name="buttonCompat">?attr/listChoiceIndicatorMultipleAnimated</item>
<item name="android:background">?attr/controlBackground</item>
</style>
the attr buttonCompat has a default value to show the click animation. The attr buttonCompat take effect and ignore the button attr.
To fix it, the attr buttonCompat must be undefined. In your theme add
<item name="checkboxStyle">#style/MyCheckBox</item>
and add a style
<style name="MyCheckBox" parent="android:Widget.CompoundButton.CheckBox">
<item name="android:button">?android:attr/listChoiceIndicatorMultiple</item>
<item name="buttonCompat">#null</item>
<item name="android:background">?attr/selectableItemBackgroundBorderless</item>
</style>
Also in your values-v21 dir, add this to your theme
<item name="checkboxStyle">?android:attr/checkboxStyle</item>
Change CheckBox to AppCompatCheckBox(AndroidX) and replace android:button to app:buttonCompat
<androidx.appcompat.widget.AppCompatCheckBox
android:id="#+id/clipboardBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/email_sign_in_button"
app:buttonCompat="#drawable/clipboard_checkbox" />
You need to set the button and buttonCompat null for androidx libraries. It will look like below-
<androidx.appcompat.widget.AppCompatCheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#null"
app:buttonCompat="#null"
android:background="#drawable/cb_pause_resume_selector"
/>

Why isn't my ImageButton style being applied to my ImageButton?

I have some styles set up to work on different API's (one for v21 and one for anything below that). I want to have a style for my ImageButton but it doesn't seem to be working out the way I expect.
The style for the v-21 is
<style name="BorderlessImageButton" parent="AppTheme.BorderlessImageButton">
<item name="android:background">?android:attr/selectableItemBackgroundBorderless</item>
<item name="android:tint">#color/secondary_text</item>
</style>
The style that will be used for all other API's below v-21 is
<style name="BorderlessImageButton" parent="AppTheme.BorderlessImageButton"/>
<style name="AppTheme.BorderlessImageButton" parent="#android:style/Widget.ImageButton">
<item name="android:tint">#color/secondary_text</item>
<item name="android:background">#color/borderless_button_background</item>
</style>
Here is my xml resource
<ImageButton
android:id="#+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toStartOf="#id/date_picker"
android:background="#null"
android:padding="10dp"
android:src="#drawable/ic_today_white_24dp"
android:theme="#style/BorderlessImageButton" />
If I run this on a device that has v-21 or v-22, the button doesn't visually react to my touch as I would expect using the ?android:attr/selectableItemBackgroundBorderless. Also, on any device below v-21, the button still doesn't react to the selector resource I have set up for it.
res/color/borderless_button_background.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#color/accent" />
<item android:color="#color/transparent"/>
</selector>
Please help me get my button to react properly to touch based on which API the user has on their device.
Thanks
You need to apply your style with
style="#style/BorderlessImageButton"
The attribute android:theme is used for activities, see this.
UPDATE
Since Android 5.0, or using AppCompat 22.1.0+ (api 11+), you can also use a ThemeOverlay and use android:theme on single views. You can read about this technique here. Thanks to Ian for this.

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 :)

Setting button background via theme makes the button unclickable

Like the title indicates, when setting background of a button via theme, the button stops responding. I believe it is an issue on a few android versions, since my Nexus 4 running KitKat has no issue, however my HTC Desire S running 2.3.5 has it happenning. By the way, I am using the HoloEverywhere 2.0 atm. What is the problem?
The way I set the theme is the following:
<style name="AppTheme" parent="#style/Holo.Theme.Light.DarkActionBar">
<item name="android:buttonStyle">#style/MyButtonStyle</item>
</style>
The button style is the following:
<style name="MyButtonStyle" parent="android:style/Widget.Holo.Light.Button">
<item name="android:background">#drawable/button_yellow</item>
</style>
This works on the N4, but will NOT work on the HTC Desire S. However, if I set the same background directly on the button, it is working well on the HTC too, like this:
<Button
android:id="#+id/example_button"
android:text="Example"
android:gravity="center"
android:onClick="onClickEvent"
android:background="#drawable/button_yellow"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Any idea what is going wrong?
You should use Holo.Button.Light as parent in your custom button style.

Cannot change android Switch style when using Holo Everywhere

I am having trouble changing the style of a Switch view in my project and can't figure out what I am doing wrong or missing... My project uses holo everywhere.
I can't change the text, the thumb or the track. I've tried applying android:thumb and android:track drawables directly from the xml but nothing changes. Not even the android:textOff and android:textOn have any effect.
I found a great post on how to customise the android Switch view style and looked through all the code but couldn't find anything I was doing wrong:
http://custom-android-dn.blogspot.co.uk/2013/01/how-to-use-and-custom-switch-in-android.html
Really not sure what else to try.
Here is the xml code:
<Switch
android:id="#+id/home_availability_switch"
style="#style/MySwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:enabled="true"
android:focusable="true"
android:textOff="Free"
android:textOn="Busy" />
Here is the style:
<style name="MySwitch" parent="#style/Holo.Switch.Light">
<item name="android:thumb">#drawable/selector_availability_switch_thumb</item>
<item name="android:track">#drawable/selector_availability_switch_background</item>
</style>
Here is the thumb selector
(these are default system drawables. i have tried with different drawables):
<item android:drawable="#drawable/switch_thumb_pressed_holo_light" android:state_pressed="true"/>
<item android:drawable="#drawable/switch_thumb_activated_holo_light" android:state_checked="true"/>
<item android:drawable="#drawable/switch_thumb_disabled_holo_light" android:state_enabled="false"/>
<item android:drawable="#drawable/switch_thumb_holo_dark"/>
Here is the track selector
(these are default system drawables. i have tried with different drawables):
<item android:drawable="#drawable/switch_bg_disabled_holo_dark" android:state_enabled="false"/>
<item android:drawable="#drawable/switch_bg_focused_holo_dark" android:state_focused="true"/>
<item android:drawable="#drawable/switch_bg_holo_dark"/>
I also found this post How can I style an Android Switch? and follow the instructions but had no luck.
Any one else had a similar experience when using Holo Everywhere?
You need to define the switch like this. Need the whole package name in there for it to work.
<org.holoeverywhere.widget.Switch
android:id="#+id/lockSwitch"
style="?switchStyleOld"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TestSwitch" />
Hope this helps.

Categories

Resources