Android – Is possible to set an listener just for a part of a custom view


I want to create a custom view (extends View) which contains an image and a text.

Is it possible to set an listener just for image if i use canvas.drawBitmap(imageBitmap, ...) method?

Thank you!

Best Solution

If you want your view just to show an image and a text, why not create a specific layout in your XML file and use setContentView to display the view?

If thats what you were looking for, I think that is the easiest approach. XML example coming soon.

Here is the contents of my personal view to display my main application, just to demonstrate how I can switch view's, your view would be different including a TextView and an ImageView.

dblist.xml inside of res/layout folder.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
<ListView android:id="@+id/list"

This contents display my database results onto a Spinner

I can call that view by using:


And if I want to return to my main layout, I can switch the view by calling



You can manipulate the view you are working with programmatically by using findViewById method. Let's use my dblist.xml file as an example, notice that my ListView has an id of list. So I would call:

ListView myList = (ListView) findViewById(;

With corresponds to that layout, so whenever I want to change anything to my XML file, I would reference myList use methods and callbacks to meet my needs.

I hope this helps.