Reputation: 6350
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" />
Upvotes: 0
Views: 9964
Reputation: 98
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;
}
});
Upvotes: 0
Reputation: 5061
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);
}
});
}
Upvotes: 2
Reputation: 3010
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.
Upvotes: -1
Reputation: 28484
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" />
Upvotes: 1
Reputation: 4547
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
}
}
Upvotes: 0