thecr0w
thecr0w

Reputation: 2197

Add FrameLayout At The bottom Of Any Activity Programmatically

How can I find the root view of an activity, then add a framelayout at the bottom? On the contrary, code below add to the top, but can't to its bottom.

    FrameLayout aLayout = new FrameLayout(getActivity());
    aLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

    ImageView image = new ImageView(getActivity());
    image.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    image.setImageResource(R.drawable.alayer_0);
    adLayout.addView(image);

    ViewGroup content = (ViewGroup) getActivity().findViewById(android.R.id.content);
    content.addView(aLayout);

Upvotes: 1

Views: 4218

Answers (5)

Vivek Khandelwal
Vivek Khandelwal

Reputation: 7849

android.R.id.content is a FrameLayout. So you cannot align child view to bottom.

What you can do is add a RelativeLayout to android.R.id.content with FILL_PARENT for height and width.

You can now add your FrameLayout to the RelativeLayout with ALIGN_PARENT_BOTTOM.

Upvotes: 1

Michel
Michel

Reputation: 1523

Alternatively you can also add a margin around your FrameLayout. No need for an additional RelativeLayout.

DisplayMetrics metrics = MyActivity.this.getResources().getDisplayMetrics();
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(0, metrics.heightPixels - 20, 0, 0);
myFrameLayout.setLayoutParams(params);

This will position the FrameLayout 20 pixels from the bottom of the screen.

Upvotes: 0

Assign some id to your Root layout(Main layout of your XML)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linear_root"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

Activity class, Map the Root layout

LinearLayout mRootLayout;

mRootLayout=(LinearLayout) findViewById(R.id.linear_root);

mRootLayout.add(yourFrameLayout);// this will add your FrameLayout at the last

Update: To get the Root Layout of any Activity then you can use getRootView

or getWindow().getDecorView().findViewById(android.R.id.content)

Upvotes: 1

Anirudh
Anirudh

Reputation: 3358

looks like you want to add a footer. Start your xml with framelayout and then include your footer in a linear or relative layout and give android:layout_gravity="bottom". This will solve your problem.

Upvotes: 1

Ravi
Ravi

Reputation: 2367

Might be one solution is that you should use by xml ,in xml first take one relative layout and than inside that relative layout take your frame layout and set property of frame layout as-

android:layout_alignParentBottom="true"

so your frame layout will be shown on the bottom of screen.Hope it will work perfectly for you.

Upvotes: 3

Related Questions