Question

I would like create a custom widget.

It looks like this:
enter image description here

So, should I extend Button because I would like to click on it ?

How can I put an image on it? I already read about method onDraw(), but I don't know how to put an image.

(Thank Andrew T.)

Was it helpful?

Solution

You can just make it a compund object: it's a TextView with a drawable on a side.

TextViews are clickable, so you can fire a click handler.

Something like this:

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="4dp"
    android:drawableLeft="@drawable/ic_launcher"
    android:drawablePadding="8dp"
    android:text="Title\nA little description for this object"
    android:textSize="16sp"
    android:onClick="click_handler"
/>

Then just add to your code:

public void click_handler(View v)
{
    // Do something in your click event
}

OTHER TIPS

Put everything on a LinearLayout or a RelativeLayout (An ImageView, two TextViews). Add click listener to that layout. Later you can use the layout in ListViews for example.

You can use this

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/imageView"
            ......
            android:layout_alignParentLeft="true" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ......
            android:layout_toRightOf="@+id/imageView"
            android:layout_alignTop="@+id/imageView" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ......
            android:layout_below="@+id/textView1"
            android:layout_alignLeft="@+id/textView1" />

    </RelativeLayout>

And then write a class to handle it

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