Basically what the title says:
I have a View that I have set the background image for (via the android:background attribute in XML), and I'd like to overlay another image when the user presses on the view
I know that you can use XML selectors to CHANGE the background image when the view is pressed, but is it possible to overlay an image instead?
Or will I just have to have 2 images - the plain one and then the one with the overlay added?
Thanks in advance :)
Android Overlay Image On Top Of Background Image Using XML Selector
9.7k Views Asked by Ben Ezard At
3
There are 3 best solutions below
0

// xml
<ImageView
android:id="@+id/img"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:background="@drawable/ijoomer_btn_hover"
android:padding="5dp"
android:scaleType="fitXY" />
// Activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView("xml");
img = (ImageView) findViewById(R.id.img);
img.setImageDrawable(null);
}
img.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(img.getDrawable()!=null){
img.setImageDrawable(null);
}else{
img.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher));
}
}
});
Thanks to Chaosit, I came up with this solution:
In res/xml you create a selector file, e.g. selector.xml
In res/drawable, you then create a layer-list, e.g. image_with_overlay.xml
Then in the
View
that you want to have this property, you simply useandroid:background="@xml/selector"
Then you're all done :-)