Anurag Prakash Anurag Prakash - 5 months ago 48
Android Question

Replace the image of imageview onclick when pressed before redirecting

I have several Images Inside linear layout. I want to change there Image source upon click before redirecting to next activity. My linear layout is finally enclosed in fragment. That fragment I have used in main activity. What should I do to change the source?

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="false"
android:layout_alignParentStart="false"
android:layout_alignParentEnd="false">

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:layout_margin="5dp">

<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/background"
android:layout_weight="1"
android:clickable="true"
android:onClick="setContentView"
android:id="@+id/campus"
android:contextClickable="false">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/acad"
android:src="@drawable/nap"
android:layout_marginLeft="24dp"
android:layout_marginTop="22dp"
android:layout_marginBottom="2dp" />


<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ACADEMIA"
android:id="@+id/textView"
android:textColor="#993300"
android:textAlignment="center" />
</LinearLayout>


The image shows the layout I am trying to change, an image with brown b/g is changing to white

enter image description here

Answer

Take the drawable selector.xml

Take nap_click drawable what you want after click and nap in normal

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/nap_click" android:state_pressed= "true"/>
    <item android:drawable="@drawable/nap"/>

</selector>

And set to Your src or background in xml

  android:src="@drawable/selector"

or

  android:background="@drawable/selector"
Comments