0

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.)

Loïc
  • 144
  • 1
  • 2
  • 10
  • Please check http://stackoverflow.com/questions/22523232/inflate-into-this/22680619?noredirect=1#comment34621831_22680619 – Gaskoin Mar 31 '14 at 07:42
  • You should create a LinearLayout with the desired layout and use it to build your button. – randomizer Mar 31 '14 at 07:43

3 Answers3

2

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
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
0

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.

interlude
  • 843
  • 8
  • 29
0

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

EdmDroid
  • 1,350
  • 1
  • 11
  • 25