2

I try to put a regular button inside an image in android XML. How can it be done? I tried something like this:

<ImageView ....... 
          <Button .... />
</ImageView>

where the dots represent code. Apparently this is not the way because the platform threw an exception.

Can anyone help?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Uriel Frankel
  • 14,304
  • 8
  • 47
  • 69

3 Answers3

6

Use relative layout to insert button on the image.

your image should be given in background tag of RelativeLayout.

   <RelativeLayout 
               android:layout_width="fill_parent"
                android:layout_height="50dp"
                android:background="@drawable/list_nav"

                >

          <Button
            android:layout_width="63dp"
            android:layout_height="36dp"

            android:id="@+id/mapbutton"
            android:layout_marginTop="7dp"
             android:layout_marginLeft="4dp"
            android:layout_alignParentLeft="true"
          />

this Example adds button on left side of the image since i used layout_alignParentLeft="true"

Abhi
  • 8,935
  • 7
  • 37
  • 60
3

You can use FrameLayout:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <ImageView  android:id="@+id/myImage"
                android:layout_height="fill_parent"
                android:layout_width="fill_parent"
                android:src="@drawable/icon" />

    <Button     android:id="@+id/myButton"
                android:layout_width="150dip"
                android:layout_height="150dip"
                android:text="My Button"/>
</FrameLayout>

You don't write why you need image and button. Do you know about ImageButton?

ania
  • 2,352
  • 1
  • 20
  • 20
2

@Uriel Frankel you may try creating a custom ImageView either.Here is a post regarding Creating Custom ImageView.

Hope it helps.

Community
  • 1
  • 1
100rabh
  • 6,156
  • 5
  • 27
  • 41