0

On click of the ImageView i want to change the background of the layout .How can i do this i have created the selector that is

<?xml version="1.0" encoding="utf-8"?>

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

XML is

 <ImageView
            android:id="@+id/bookButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:onClick="reviewItineary"
            android:src="@drawable/book" />
onkar
  • 4,427
  • 10
  • 52
  • 89
Developer
  • 6,292
  • 19
  • 55
  • 115

5 Answers5

2
 mylayout = (RelativeLayout) findViewById(R.layout.main);//Your layout instance

imageview=(ImageView)findViewById(R.id.bookButton);
  imageview.setOnClickListener(new OnClickListener()
     {

   @Override
     public void onClick(View v) {
     // TODO Auto-generated method stub
                   myImgView.setImageResource(R.drawable. blue_bar);
                     mylayout.setImageResource(R.drawable. blue_bar);
               }
     });



}
Nithinlal
  • 4,845
  • 1
  • 29
  • 40
1

Create selector like this

image_selector.xml

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

    <item android:drawable="@drawable/blue_bar" android:state_focused="true"/>
    <item android:drawable="@drawable/blue_bar" android:state_pressed="true"/>
    <item android:drawable="@drawable/blue_bar" android:state_selected="true"/>
    <item android:drawable="@drawable/gray_bg" />

<ImageView
            android:id="@+id/bookButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:onClick="reviewItineary"
            android:background="@drawable/image_selector.xml"
            android:src="@drawable/book" />
Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77
0
    lLayout = (RelativeLayout) findViewById(R.layout.main);//Your layout instance

    bookButton=(ImageView)findViewById(R.id.bookButton);
bookButton.setOnClickListener(this);


    public void onClick(View v) {

            if(v==bookButton){

    lLayout.setBackgroundColor(Color.parseColor("#000000"));//to change the color
    lLayout.setBackgroundDrawable(drwableItem);//drawable object
    lLayout.setBackgroundResource(R.id.bckResource);//resource
                }
    }
onkar
  • 4,427
  • 10
  • 52
  • 89
0

You could change the background of the imageview without setting any color filters to the imageview itself like so:

    button = (ImageView) findViewById(R.id.back);
    button.setOnTouchListener(new View.OnTouchListener() {
    private Rect rect;

    @Override
    public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
           button.setBackgroundColor(Color.WHITE);
            rect = new Rect(v.getLeft(), v.getTop(), v.getRight(),  v.getBottom());
    }
    if (event.getAction() == MotionEvent.ACTION_UP) {
        button.setBackgroundColor(Color.argb(0, 0, 0, 0));
    }
    if (event.getAction() == MotionEvent.ACTION_MOVE) {
         if (!rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())) {
                        backButton.setBackgroundColor(Color.argb(0, 0, 0, 0));
                    }
                }
                return false;
            }
        });
-1

Programatically you can do it as shown below:

bookButton=(ImageView)findViewById(R.id.bookButton);

    public void setActivityBackgroundColor(int color) {
        View view = this.getWindow().getDecorView();
        view.setBackgroundColor(color);
    }

        bookButton.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub

                setActivityBackgroundColor(Color.GRAY);
                return false;
            }
        });

This code is working perfectly fine for me.

Zax
  • 2,870
  • 7
  • 52
  • 76
  • on click of the imageview i am starting a new activity so will it work se in the xml i have mentioned on click – Developer Aug 26 '13 at 10:34
  • No,in that case this will not work. In your case you can Attach the color id with the intent and start the activity. And onCreate of that activity, fetch the color id from intent and use the above setActivityBackgroundColor() function. Surely it will work I'll edit the answer if you want. – Zax Aug 26 '13 at 10:43