简体   繁体   中英

How to highlight the selected item in linear layout in android

I am having linear layout , in which it has one imageview and one textview , when i touch the linear layout , it should highlight the image as well as text inside the linear layout with different color. I have tried using set background for the linear layout when it is pressed since i have used ontouch-listener it doesn't shows the color even i tried to change the colors programmatically but it didn't works.

Can anyone tell me how i can change the imageview and textview color when a linear layout is touched.

Xml:



    <LinearLayout
        android:id="@+id/linear_otherexpense"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:background="@drawable/linearlayout_check"
        android:gravity="center"
        android:orientation="vertical"
        android:weightSum="1">

        <ImageView
            android:id="@+id/img_otherexpense"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.7"
            android:scaleType="centerInside"
            android:src="@mipmap/expense"
            android:tint="#6666FF" />

        <TextView
            android:id="@+id/tv_otherexpense"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.3"
            android:gravity="center"
            android:text="@string/dayexpense"
            android:tint="@color/colorPrimary" />
    </LinearLayout>

Selector:

 <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
    android:drawable="@android:drawable/list_selector_background" />

Code:

    li_otherexpense.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            startActivity(new Intent(NewDaybook_Activity.this,     AddingNewExpense.class));
            NewDaybook_Activity.this.finish();
            return false;
        }
    });

You can use this code. this is working fine for me .May be it will helpfull

findViewById(R.id.linear_otherexpense).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:

                        // When you Touch Down 
                        // U can change Text and Image As per your Functionality  

                        break;
                    case MotionEvent.ACTION_UP:

                        //When you Release the touch 
                        // U can change Text and Image As per your Functionality

                        break;
                }


                return true;
            }
        });

Try this,

  linearLayout = (LinearLayout) findViewById(R.id.linear_otherexpense);
         imageView=(ImageView)findViewById(R.id.img_otherexpense);
        linearLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                linearLayout.setBackgroundColor(getResources().getColor(R.color.colorAccent));
                imageView.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
                return true;
            }
        });

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