Question

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

Was it helpful?

Solution

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

OTHER TIPS

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top