Reputation: 45
This is an layout file that used in RecyclerView
.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/wordLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:background="@drawable/textview_pressed_background"
android:gravity="left|center_vertical"
android:padding="10dp"
android:text="Android Studio"
android:textColor="@color/textview_pressed_textcolor"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:background="#FFFFFF"
android:gravity="right|center_vertical"
android:padding="10dp"
android:text="@string/hide_kor_string"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
And this is textview_pressed_background.xml
file.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<solid android:color="#C89EC4" />
</shape>
</item>
<item android:state_pressed="false">
<shape>
<solid android:color="#FFFFFF" />
</shape>
</item>
</selector>
And this is textview_pressed_textcolor.xml
file.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#FFFFFF" android:state_pressed="true" />
<item android:color="#000000" android:state_pressed="false" />
</selector>
I want to change all view's background color same color and change all TextView's text color same color when i press the textView1
.
So when I thought to myself, the way using MotionEvent
in View.OnTouchListener came to mind.
TextView.setOnTouchListener(object : View.OnTouchListener {
override fun onTouch(v: View?, event: MotionEvent?): Boolean {
val action = event?.action
when (action) {
action -> MotionEvent.ACTION_DOWN {
}
action -> MotionEvent.ACTION_CANCEL {
}
action -> MotionEvent.ACTION_UP {
}
}
}
})
But I asked for help because I thought there would be a better way that I didn't know.
Is there a better way?
Upvotes: 1
Views: 718
Reputation: 61
for changing button background color when its clicked use this solotion
Button XML Code: step1:
<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=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click here to change color!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="HardcodedText" />
</android.support.constraint.ConstraintLayout>
step 2: intialize button in MainActivity:
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
button.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
}
});
}
}
final Step: change manifest
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Upvotes: 1