Question

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" />
Was it helpful?

Solution

 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);
               }
     });



}

OTHER TIPS

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" />
    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
                }
    }

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;
            }
        });

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top