0

I have an standalone ImageButton defined in xml with the following:

<ImageButton xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/btnTourStartGame"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="32dp"
    android:background="@drawable/tour_btn_start"/>

This ImageButton is then dynamically inflated in a fragment with the following code. (Note: ltTourScreenLast is a Relative Layout.)

ImageButton startButton = (ImageButton) inflater.inflate(R.layout.button_tour_start, ltTourScreenLast, false);
startButton.setOnClickListener(this);
ltTourScreenLast.addView(startButton);

Currently, the android:layout_marginBottom attribute seems to be ignored when this button is inflated and the button sits directly at the bottom of the screen.

My intention is to have the button sit 32dp from the bottom of the screen. How do I achieve this?

I've referred to the following post but to no avail: Layout problem with button margin (Sidenote: I attempted testing out layout_marginLeft on my ImageButton and it seems to work. layout_marginBottom doesn't though.)

Community
  • 1
  • 1
Brenda Nicole Tan
  • 5,164
  • 2
  • 17
  • 17
  • is there any view below your Image-button whose visibility is gone? – Amrut Mar 11 '14 at 10:06
  • @Amrut Thanks for your reply! This ImageButton is a standalone and will be the last element in ltTourScreenLast, and there are no invisible elements around it. – Brenda Nicole Tan Mar 11 '14 at 10:09

2 Answers2

1

Try putting the ImageView inside a LinearLayout...then inflate that layout.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ImageButton
        android:id="@+id/btnTourStartGame"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="32dp"
        android:background="@drawable/tour_btn_start"/>

</LinearLayout>

Inflate as follows...

View view =  inflater.inflate(R.layout.button_tour_start, ltTourScreenLast, false);
ImageButton startButton = (ImageButton)view.findViewById(R.id.btnTourStartGame);
startButton.setOnClickListener(this);
ltTourScreenLast.addView(view);
Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41
  • This works. Thanks! Although the last line of the inflate code should be ltTourScreenLast.addView(view); instead (: Please edit your answer so that I can mark it as the accepted one. I'm still puzzled why android:layout_marginBottom="32dp" does not work in this case though. Anyone has ideas? – Brenda Nicole Tan Mar 11 '14 at 11:01
  • I have updated my answer...Glad to hear that it works...I have no idea...but anyhow it ignores the margin. – Hamid Shatu Mar 11 '14 at 11:05
-1

Try to give margin bottom in your java code itself using Layout Params

RelativeLayout.LayoutParams params=showbutton.getLayoutParams();
params.setMargins(0,0,0,32);
showbutton.setLayoutParams(params);
Priya
  • 489
  • 1
  • 10
  • 20