Change background color on touchhevent in relative layout

I am using the following code.

  <RelativeLayout
            android:layout_width="310dp"
            android:layout_height="70dp"
            android:layout_below="@+id/UIReportView"
            android:layout_marginLeft="4dp"
            android:layout_marginTop="2dp"
            android:background="@drawable/small_corners" >

      

small_corners

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/UINoCorners"
    android:shape="rectangle">
    <corners android:radius="10dp"/>
    <padding android:left="10dp" 
             android:right="10dp" 
             android:top="5dp" 
             android:bottom="5dp"/>
    <solid android:color="#FFFFFF"/>

    </shape>

      

It now shows a white stripe. I want that whenever I click on this relative layout, its color should change as set by me.

 report = (RelativeLayout) findViewById(R.id.UIMainReportViewTimely);


       report .setOnClickListener(new View.OnClickListener() 
       {
        @Override
        public void onClick(View v) 
        {
// My Code
        }
       });

      

What should I do here? report.onTouchEvent?

And how can I change the background color.

Regards

+3


source to share


3 answers


you can use this: -



    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape>
            <stroke android:width="1dp" android:color="#999999" />
            <padding android:left="10dp" android:top="3dp" android:right="10dp"
                android:bottom="3dp" />
            <corners android:radius="7dp" android:bottomRightRadius="7dp"
                android:bottomLeftRadius="7dp" android:topLeftRadius="0dp"
                android:topRightRadius="0dp" />

            <gradient android:startColor="#449def" android:endColor="#2f6699"
                android:angle="270" />


        </shape>
    </item>
<item>
<shape>
    <corners android:radius="10dp"/>
    <padding android:left="10dp" 
             android:right="10dp" 
             android:top="5dp" 
             android:bottom="5dp"/>
    <solid android:color="#FFFFFF"/>
    </shape>
    </item>
</selector>
</shape>

      

+3


source


Create another option with a new background and in your setOnclickListener put

report.setBackgroundDrawable(R.drawable.newbackground_small_corners);



or

report.setBackgroundColor(Color.parseColor("#00000000"));

      

+4


source


I would rather use a method onTouch()

and check if the affected coordinates are within your relative layout. If so, just set the background using setBackgroundColor()

.

0


source







All Articles