android – Put a button over an ImageView-ThrowExceptions

Exception or error:

I’m newbie in Android.

I would like to know if it’s possible to put a button or another component over an ImageView. I’ve tried setting the image as a background image of a LinearLayout, but when I change between landscape and portrait mode, the image porportions are changed.

Thanks a lot.

How to solve:

Don’t put the image as a background, you don’t have any control on how the image is scaled. Instead, create a RelativeLayout, and put an ImageView as one of the child, and you can place anything (buttons etc) as other RelativeLayout children.

<RelativeLayout ...>
    <ImageView (your image) ...>
    <Button (the button you want) ... />


Try this code… it’s Help you….

<?xml version="1.0" encoding="utf-8"?>  
 <RelativeLayout xmlns:android=""  

 <ImageView xmlns:android="" 
    android:src="Path "
<Button android:id="@+id/but2" 
         android:layout_height="wrap_content" />

Try this Code …..

In that give paramater in button to set your Button Position….

android:layout_margin or android:layout_alignParent

And also give Path of Image….


There is another way to do it,

Thank you.


The simpliest way to do it is to use a FrameLayout.


This is easy using the ConstraintLayout.

  1. Set the constraints of the Button to the ImageView.
  2. Drag the Button anywhere you want over the ImageView to position it.
  3. XML code will be autogenerated.

Button over an ImageView

Leave a Reply

Your email address will not be published. Required fields are marked *