简体   繁体   中英

How to change the TextView Color on click of a layout in android?

I want to change the background image and TextView Color on click of a layout .The background color is chnaging properly but my textview color is not changing.Here is my XML code :

<RelativeLayout
        android:id="@+id/flight_relative"
        android:clickable="true"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_below="@+id/imgLogo"
        android:layout_marginTop="5dp"
        android:background="@drawable/button_effect"
        android:gravity="center_vertical" >

        <TextView
            android:id="@+id/flight_content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/flight_list_image"
            android:layout_marginTop="10dp"
            android:layout_toRightOf="@+id/flight_list_image"
            android:text="@string/flight_tittle"
            android:textColor="#152b72"
            android:textSize="15dp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/content_flight"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/flight_content"
            android:layout_toRightOf="@+id/flight_list_image"
            android:text="@string/flight_content"
            android:textColor="#2f2f2f"
            android:textSize="10sp" />

        <ImageView
            android:id="@+id/flight_arrow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/flight_content"
            android:layout_alignParentRight="true"
            android:src="@drawable/arrow" />

        <ImageView
            android:id="@+id/flight_list_image"
            android:layout_width="50dip"
            android:layout_height="50dip"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:padding="3dip"
            android:src="@drawable/flight_icon" />

    </RelativeLayout>

Code to chnage the textview color is :

flightRelative = (RelativeLayout )findViewById(R.id.flight_relative);
        flightRelative.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                flight = (TextView)findViewById(R.id.flight_content);
                flight.setTextColor(Color.WHITE);

            }
        });

What wrong i am doing please suggest me .For the first time it is not working on second time it is working

you should use

        @Override
        public void onClick(View arg0) {
            flight = (TextView)flightRelative.findViewById(R.id.flight_content);
            flight.setTextColor(Color.WHITE);

        }

I believe it should work now.

You should add this definitely working.I am also check it out...

flightRelative.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
       TextView flight = (TextView)findViewById(R.id.flight_content);
       flight.setTextColor(Color.parseColor("#FFFFFF"));

    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM