Reputation: 6976
How can I get a button on a WIDGET on Android to display the Material ripple when clicked?
<LinearLayout
android:id="@+id/button_power"
style="@style/MyStyle">
<ImageView
android:id="@+id/image_view_power"
android:layout_width="@dimen/widget_button_image_size"
android:layout_height="@dimen/widget_button_image_size"
android:layout_gravity="center_horizontal"
android:contentDescription="power"
android:src="@drawable/baseline_power_settings_new_24"
android:tint="@color/primary_text" />
<TextView
android:id="@+id/text_view_power"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="On"
android:textColor="@color/primary_text"
android:textSize="@dimen/widget_button_text_size" />
</LinearLayout>
style.xml
<style name="MyStyle" parent="Base.Widget.AppCompat.Button.Borderless">
<item name="android:layout_width">@dimen/button_height</item>
<item name="android:layout_height">@dimen/button_height</item>
<item name="android:minHeight">0dp</item>
<item name="android:padding">8dp</item>
<item name="android:minWidth">0dp</item>
<item name="android:textAllCaps">false</item>
<item name="android:drawablePadding">0dp</item>
<item name="android:background">@color/white_pressed</item>
<item name="android:clickable">true</item>
<item name="android:focusable">true</item>
<item name="android:orientation">vertical</item>
</style>
Above shows how I'm using a linear layout to represent a clickable button for the widget. It also includes the style details.
I tried adding the line
android:background="?attr/selectableItemBackground"
to the LinearLayout but got an error on the widget which stated "Problem loading widget". The problem also occurred if I moved the attribute to the style itself.
It looks like what I want to achieve is possible since the Google Drive widget on Android has this effect. How is this done?
Thank you!
Upvotes: 3
Views: 895
Reputation: 883
I was using
android:background="?selectableItemBackground"
issue was solved after using,
android:background="?android:attr/selectableItemBackground"
Upvotes: 2
Reputation: 656
You can make a ripple drawable:
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/colorPrimary">
<item android:id="@android:id/mask">
<shape android:shape="rectangle">
<solid android:color="@android:color/white" />
</shape>
</item>
</ripple>
Then set this drawable as a background of your layout
Upvotes: 0
Reputation: 964
Add these attribute to your LinearLayout
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
Note: This will not work below lollipop.
Upvotes: 2