カブトムシの壺

消しゴム付き鉛筆

Viewをタップした時にRipple Effectをさせる。

結論から言うと、 ViewのレイアウトXMLの属性値に以下の1行を加えればokです。APIレベル21以上のOSの端末であれば、タップされた際に波紋のように反応するようになります。

android:background="?android:attr/selectableItemBackground"

 

 例えば、EmptyActivityの「Hello World!」のTextViewに適用させるときはこんな感じになります。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.h_ujikawa.myapplication.MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:background="?android:attr/selectableItemBackground"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

 

簡単ですね。以上です。

 

(Qiitaにも投稿しています)

qiita.com