android - Change background and textcolor in cardview on touch -


i having difficulty in changing text color background color on touch (hover effect) in cardview. have changed background color not text color.

i have tried use selector didn't worked out.

    <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android">     <item android:state_activated="true" android:drawable="@android:color/white" />     <item android:drawable="@android:color/black"/> </selector> 

it changes color of text , background

enter image description here

my layout

enter image description here

on pressing want change color text background

enter image description here

here layoutcode

    <?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:card_view="http://schemas.android.com/apk/res-auto"     android:id="@+id/main"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:background="#f1f1f3"     android:orientation="vertical">      <android.support.v7.widget.cardview         android:id="@+id/card_view"         android:layout_width="fill_parent"         android:layout_height="120dp"         android:layout_gravity="center"         android:layout_marginleft="15dp"         android:layout_marginright="15dp"         android:layout_margintop="15dp"         card_view:cardcornerradius="0dp"         card_view:contentpadding="0dp">          <relativelayout             android:layout_width="match_parent"             android:layout_height="match_parent"             android:background="@drawable/listhover">              <imageview                 android:id="@+id/thumbnail"                 android:layout_width="wrap_content"                 android:layout_height="match_parent"                 android:layout_alignparentleft="true"                 android:scaletype="fitxy"                 android:src="@drawable/cardviewimage_cio" />              <textview                 android:id="@+id/textview"                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:layout_alignparenttop="true"                 android:layout_marginleft="12dp"                 android:layout_margintop="10dp"                 android:layout_toendof="@+id/thumbnail"                 android:textstyle="bold" />              <textview                 android:id="@+id/textview2"                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:layout_alignstart="@+id/textview"                 android:layout_below="@+id/textview"                 android:layout_margintop="5dp"                 android:textcolor="#000" />         </relativelayout>      </android.support.v7.widget.cardview>  </linearlayout> 

update: use on touch listener. example:

cardview.setontouchlistener(new view.ontouchlistener() {         @override         public boolean ontouch(view v, motionevent event) {             if (event.getaction() == motionevent.action_down) {                 textview.settextcolor(color.red);                 return true;             } else {                 textview.settextcolor(color.blue);             }              return false;         }     }); 

use selector background cardview.

bg_selector.xml

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android">     <item android:state_activated="true" android:drawable="@android:color/white" />     <item android:drawable="@android:color/black"/> </selector> 

add this:

    android:background="@drawable/bg_selector" 

Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -