LoginSignup
7
5

More than 5 years have passed since last update.

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

Last updated at Posted at 2017-07-23

結論から言うと、 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>

(ブログにも書いています。)
カブトムシのつぼ

7
5
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
5