I can't figure out why my buttons doesn't have the press effect.
i guess my styles is the problem.
The button is displayed inside a fragment hosted by AppCombatActivity
v21 style.xml is
<?xml version="1.0" encoding="UTF-8" ?>
<resources>
<style name="MyTheme" parent="MyTheme.Base">
<item name="android:windowContentTransitions">true</item>
<item name="android:windowAllowEnterTransitionOverlap">true</item>
<item name="android:windowAllowReturnTransitionOverlap">true</item>
<item name="android:windowSharedElementEnterTransition">#android:transition/move</item>
<item name="android:windowSharedElementExitTransition">#android:transition/move</item>
</style>
</resources>
style.xml is
<style name="MyTheme" parent="MyTheme.Base">
</style>
<style name="MyTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Your customizations go here -->
<item name="buttonStyle">#style/LoginButton</item>
<!-- Override the color of UI controls -->
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:textColorSecondary">#color/white</item>
<item name="android:textColorPrimary">#color/white</item>
<item name="colorPrimary">#color/my_blue</item>
<item name="colorPrimaryDark">#color/my_blue</item>
<item name="colorAccent">#color/my_purple</item>
</style>
<style name="LoginButton" parent="Widget.AppCompat.Button.Colored">
<item name="android:textAllCaps">false</item>
<item name="colorControlHighlight">#color/my_purple</item>
<item name="android:textColor">#color/white</item>
</style>
button code:
<Button
android:theme="#style/LoginButton"
android:background="#drawable/roundedbutton_do_this"
android:backgroundTint="#color/dark_blue"
android:id="#+id/SelectDateButton"
android:layout_width="match_parent"
android:layout_height="52dp"
android:gravity="center"
android:text="#string/ChooseDateTime_SelectDate"
android:layout_margin="12dp"
android:singleLine="false"
android:layout_gravity="center_vertical" />
background code:
<?xml version="1.0" encoding="utf-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/green"/>
<corners android:radius="10dp" />
</shape>
minSdkVersion:16
targetSdkVersion:27
Now i don't see any effect on the button when i press it. test it on android 6 and 7
EDIT: i have try this but the buttons dont have shape (rounded corners)
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="#color/green" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/gradient" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/gradient" />
<item android:drawable="#color/green" />
<item>
<shape android:shape="rectangle">
<solid android:color="#color/green"/>
<corners android:radius="10dp" />
</shape>
</item>
</selector>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
app:cardCornerRadius="#dimen/_3sdp"
app:cardBackgroundColor="#android:color/white">
You can use selector as background drawable, refer to this answer.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="#color/green" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#color/ANY_OTHER" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#color/ANY_OTHER" />
<item android:drawable="#color/green" />
</selector>
I have manage to create a press effect with rounded corners that fades on click this way:
selector.xml
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="#android:integer/config_shortAnimTime">
<item android:state_focused="true" android:state_pressed="false" android:drawable="#drawable/colorbutton" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/gradient" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/gradient" />
<item android:drawable="#drawable/colorbutton" />
</selector>
gradient.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="10dp" />
<gradient android:angle="90" android:startColor="#880f0f10" android:centerColor="#880d0d0f" android:endColor="#885d5d5e"/>
</shape>
</item>
</layer-list>
colorbutton.xml
<?xml version="1.0" encoding="utf-8" ?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/green"/>
<corners android:radius="10dp" />
</shape>
and on button i set:
<Button
android:background="#drawable/selector" />
thanks #Omkar for the help.
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 can find many rounded-rectangle button but it is not working to me.
What do I miss?
It is layout.xml:
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:background="#ebf1ff"
android:orientation="vertical">
<LinearLayout
style="#style/wrapperWidthWeightSumOne"
android:paddingTop="50dp">
<Button
android:id="#+id/btnTypeAll"
style="#style/ConsentTypeBtn"
android:layout_width="0dp"
android:layout_weight="0.85"
android:text="All"/>
</LinearLayout>
(...)
</LinearLayout>
This is button's style:
<style name="ConsentTypeBtn">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:onClick">onClick</item>
<item name="android:background">#drawable/selector_btn_consent_all</item>
<item name="android:layout_marginBottom">2dp</item>
<item name="android:singleLine">true</item>
<item name="android:layout_gravity">center</item>
<item name="android:textColor">#fff</item>
</style>
...and this is selector_btn_consent_all.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="#drawable/consent_type_on">
<shape android:shape="rectangle">
<corners android:radius="4dp" />
<stroke android:width="2dp" android:color="#1F3259"/>
</shape>
</item>
<item android:state_pressed="true" android:drawable="#drawable/consent_type_on"></item>
<item android:drawable="#drawable/consent_type_off"></item>
your selector_btn_consent_all.xml has status like this: when android:state_selected="true", use android:drawable="#drawable/consent_type_on".
So, find consent_type_on.xml at your drawable folder, and it would be rectangle, add <corners android:radius="4dp" />
EDIT
then, make consent_type_on.xml at your drawable folder. example here
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="5dp" />
<solid android:color="#android:color/holo_blue_bright" />
</shape>
I'm using a custom view for SlidingTabLayout. I've set a drawable(selector) for the text's color , but for some reason it only changes on tab press and not on select.
<TextView
android:id="#+id/tabText"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:textColor="#drawable/tab_text_color_selector"
android:textSize="16sp"
android:paddingLeft="4dp"
android:paddingRight="4dp"/>
the drawable:
<?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/white" />
<item android:state_selected="true" android:color="#color/white" />
<item android:state_focused="true" android:color="#color/white" />
<item android:color="#color/blue" />
</selector>
Try this.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:color="#color/white" />
<item android:state_active="true" android:color="#color/white" />
<item android:state_pressed="true" android:color="#color/white" />
<item android:state_selected="true" android:color="#color/white" />
<item android:state_focused="true" android:color="#color/white" />
<item android:color="#color/blue" />
</selector>
Hope. It will help you
In my project I have a ViewPager that is not linked to the action bar and I'm having problems with setting the font size of the text for the tabs. I'm using ViewPagerIndicator (v2.4.1) in combination with ActionBarSherlock (v4.3.1).
The layout I'm using is:
<com.viewpagerindicator.TabPageIndicator
android:id="#+id/artistIndicator"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<android.support.v4.view.ViewPager
android:id="#+id/artistPager"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:minHeight="50dp" />
This is within a LinearLayout inside a ScrollView. The layout works as expected and I see no problems with the interaction between the ScrollView and my ViewPager.
Styling of the ViewPager and tabs is done with the following XML:
<style name="ArtistIndicatorTheme" parent="AppTheme">
<item name="vpiTabPageIndicatorStyle">#style/ArtistInfoTabPageIndicator</item>
</style>
<style name="ArtistInfoTabPageIndicator" parent="Widget.TabPageIndicator">
<item name="android:background">#drawable/tab_background</item>
<item name="background">#android:color/white</item>
<item name="android:textAppearance">#style/ArtistInfoTabPageIndicator.Text</item>
</style>
<style name="ArtistInfoTabPageIndicator.Text" parent="#style/TextAppearance.TabPageIndicator">
<item name="android:textColor">#drawable/tab_text_color</item>
<item name="android:textSize">#dimen/activity_position_artistinfo_font_size</item>
<item name="android:textStyle">bold</item>
</style>
The background of the tabs is handled by the following XMLs to create a actual tab-style look:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="#drawable/tab_background_unselected" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="#drawable/tab_background_selected" />
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="#drawable/tab_background_unselected" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="#drawable/tab_background_selected" />
</selector>
And the two shape XMLs for selected:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Bottom Line -->
<item>
<shape android:shape="rectangle" >
<solid android:color="#color/text_inverted" />
</shape>
</item>
<!-- Color of your action bar -->
<item android:bottom="2dip">
<shape android:shape="rectangle" >
<solid android:color="#color/text_inverted" />
</shape>
</item>
</layer-list>
and unselected:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Bottom Line -->
<item>
<shape android:shape="rectangle" >
<solid android:color="#color/text_inverted" />
</shape>
</item>
<!-- Color of your action bar -->
<item android:bottom="2dip" android:right="2dip">
<shape android:shape="rectangle" >
<solid android:color="#color/text_important" />
</shape>
</item>
</layer-list>
The current font size is set by the following dimension:
<dimen name="activity_position_artistinfo_font_size">15dp</dimen>
Any indication on what I'm doing wrong?
Is there a way to combine the effects of a StateListDrawable and a LevelListDrawable?
I wrote this resource file but when used nothing appear:
<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<selector
android:constantSize="true"
android:variablePadding="false"
android:maxLevel="0"
>
<item android:state_pressed="true">
<layer-list>
<item android:drawable="#drawable/btu_color_standard" />
<item android:drawable="#drawable/btu_shade_pressed" />
</layer-list>
</item>
<item android:state_focused="true">
<layer-list>
<item android:drawable="#drawable/btu_color_standard" />
<item android:drawable="#drawable/btu_shade_selected" />
</layer-list>
</item>
<item>
<layer-list>
<item android:drawable="#drawable/btu_color_standard" />
<item android:drawable="#drawable/btu_shade_normal" />
</layer-list>
</item>
</selector>
<selector
android:constantSize="true"
android:variablePadding="false"
android:maxLevel="1"
>
<item android:state_pressed="true">
<layer-list>
<item android:drawable="#drawable/btu_color_standard" />
<item android:drawable="#drawable/btu_lighted" />
<item android:drawable="#drawable/btu_shade_pressed" />
</layer-list>
</item>
<item android:state_focused="true">
<layer-list>
<item android:drawable="#drawable/btu_color_standard" />
<item android:drawable="#drawable/btu_lighted" />
<item android:drawable="#drawable/btu_shade_selected" />
</layer-list>
</item>
<item>
<layer-list>
<item android:drawable="#drawable/btu_color_standard" />
<item android:drawable="#drawable/btu_lighted" />
<item android:drawable="#drawable/btu_shade_normal" />
</layer-list>
</item>
</selector>
</level-list>
Then I call
_btn.getBackground().setLevel(level); //level is either 0 or 1
to change the level of the drawable (_btn is a button btw).
Why it doesn't work?
Thanks in advance.
Found myself the solution: <selector> elements must be enclosed in <item> elements:
<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:maxLevel="0"
>
<selector
android:constantSize="true"
android:variablePadding="false"
>
<item android:state_pressed="true">
<layer-list>
<item android:drawable="#drawable/btu_color_standard" />
<item android:drawable="#drawable/btu_shade_pressed" />
</layer-list>
</item>
<item android:state_focused="true">
<layer-list>
<item android:drawable="#drawable/btu_color_standard" />
<item android:drawable="#drawable/btu_shade_selected" />
</layer-list>
</item>
<item>
<layer-list>
<item android:drawable="#drawable/btu_color_standard" />
<item android:drawable="#drawable/btu_shade_normal" />
</layer-list>
</item>
</selector>
</item>
<item
android:maxLevel="1"
>
<selector
android:constantSize="true"
android:variablePadding="false"
>
<item android:state_pressed="true">
<layer-list>
<item android:drawable="#drawable/btu_color_standard" />
<item android:drawable="#drawable/btu_lighted" />
<item android:drawable="#drawable/btu_shade_pressed" />
</layer-list>
</item>
<item android:state_focused="true">
<layer-list>
<item android:drawable="#drawable/btu_color_standard" />
<item android:drawable="#drawable/btu_lighted" />
<item android:drawable="#drawable/btu_shade_selected" />
</layer-list>
</item>
<item>
<layer-list>
<item android:drawable="#drawable/btu_color_standard" />
<item android:drawable="#drawable/btu_lighted" />
<item android:drawable="#drawable/btu_shade_normal" />
</layer-list>
</item>
</selector>
</item>
</level-list>
In my case, the problem was I did not set the button to use the background in the level-list. For example, if you have the level-list defined in res/drawable/button_levels.xml, then you need to set the button background as "#drawable/button_levels".