tags:

views:

70

answers:

1

Hi!

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!

+1  A: 

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="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:background="#FFFFFF">
<ListView android:id="@+id/list"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:textColor="#000000"/>
</LinearLayout>

This contents display my database results onto a Spinner

I can call that view by using:

setContentView(R.layout.dblist);

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

setContentView(R.layout.main);

EDIT:

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(R.id.list);

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.

Anthony Forloney
I want as my view to behave like a unique part of my application: I want to create a custom view (put inside of it a image and an text) and to use it with an list adapter. I can create this view(by example MyCustomView) by extending the RelativeLayout class and put on it an ImageView + TextView but every time when I call MyNewCustomView.invalidate() I think the myImageView.invalidate() and myTextView.invalidate() are called, right? If I have 1000 MyCustomView objects inside of my ListView I can't get the best speed! What do you think about this approach ? It is wrong?
Ungureanu Liviu
Not 100% sure, all the layout's I have done have in the XML file itself, check here for some examples that might be helpful: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/index.html
Anthony Forloney
Thank you for your answers! I will take a look at the examples.. All my application layout is made from code because I need to update it(adding/removing views). Please, can you give me an tip about how to update an layout what was created from an XML file? Thank you!
Ungureanu Liviu
I updated my answer, let me know if you need anything else or further explanation.
Anthony Forloney
Thank you for your help.. Now I understand..
Ungureanu Liviu
No problem, come back if you need anything else
Anthony Forloney