CDrosos
CDrosos

Reputation: 2518

Button with background and shape doesn't have a press effect

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>

Upvotes: 0

Views: 490

Answers (3)

Mohamed Fiyaz
Mohamed Fiyaz

Reputation: 46

 <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">

Upvotes: 1

CDrosos
CDrosos

Reputation: 2518

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.

Upvotes: 0

Omkar
Omkar

Reputation: 1543

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>

Upvotes: 1

Related Questions