I have two image buttons, button1 and button2. For button1, I am setting a bigger image and for button2 I am setting a small image. I have to place button2 exactly at the center of button1. How do I achieve this?
I am using relative layout and I have to do this changes in an XML file. I need to receive click events also separately for both the buttons.
I have tried the below code, but I am not receiving separate click events.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="@+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/mainscreen"
android:scaleType="fitXY" />
<com.google.ads.AdView
android:id="@+id/adView"
android:layout_width="320dp"
android:layout_height="50dp"
ads:adUnitId="xxxx"
ads:adSize="BANNER"
ads:loadAdOnCreate="true"/>
<ImageButton android:id="@+id/star"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:layout_toRightOf="@+id/adView"
android:src="@drawable/star"
android:background="@layout/selector"
android:layout_marginLeft="40dp"
android:focusable="false" />
<ImageButton android:id="@+id/image1"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:layout_below="@+id/adView"
android:src="@drawable/drum"
android:background="@layout/selector"
android:layout_marginTop="30dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="10dp"
android:focusable="false" />
<ImageButton android:id="@+id/image2"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:src="@drawable/drum1"
android:layout_alignLeft="@+id/image1"
android:layout_alignTop="@+id/image1"
android:layout_alignRight="@+id/image1"
android:layout_alignBottom="@+id/image1"
android:focusable="false" />
<ImageButton android:id="@+id/image3"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:src="@drawable/drum2"
android:background="@layout/selector"
android:layout_alignLeft="@+id/image1"
android:layout_alignTop="@+id/image1"
android:layout_alignRight="@+id/image1"
android:layout_alignBottom="@+id/image1"
android:focusable="false" />
<ImageButton android:id="@+id/image4"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:layout_below="@+id/adView"
android:src="@drawable/drum"
android:background="@layout/selector"
android:layout_marginTop="30dp"
android:layout_toRightOf="@+id/image1"
android:focusable="false" />
<ImageButton android:id="@+id/image5"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:src="@drawable/drum1"
android:background="@layout/selector"
android:layout_alignLeft="@+id/image4"
android:layout_alignTop="@+id/image4"
android:layout_alignRight="@+id/image4"
android:layout_alignBottom="@+id/image4"
android:focusable="false" />
<ImageButton android:id="@+id/image6"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:src="@drawable/drum2"
android:background="@layout/selector"
android:layout_alignLeft="@+id/image4"
android:layout_alignTop="@+id/image4"
android:layout_alignRight="@+id/image4"
android:layout_alignBottom="@+id/image4"
android:focusable="false" />
</RelativeLayout>
With your current layout, you will need to nest the items. but since you’re only interested in the image part of the imagebutton, you can just use a FrameLayout for what you’re currently calling “button1” like in my example (adapt to your circumstances):