1
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
                            params.gravity = Gravity.BOTTOM;
                            myFrameLayout.setLayoutParams(params);

I have only a text-view inside my frame-layout and I want to set its gravity to bottom dynamically in some condition. I am trying with this code but its not working.

 <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="top|center_horizontal">

        <TextView
            android:id="@+id/preview_content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </FrameLayout>
Anupriya
  • 1,663
  • 3
  • 17
  • 28

3 Answers3

1
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
                    params.gravity = Gravity.BOTTOM;
                    myTextView.setLayoutParams(params);

Use this code which sets the params to instance of textview.

Mayank Pandya
  • 265
  • 3
  • 14
1

maybe you are trying to do it on OnCreate, and the frame layout is not yet created , you can try using

ViewTreeObserver viewTreeObserver = myFrameLayout.getViewTreeObserver();
            if (viewTreeObserver.isAlive()) {


    viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    myFrameLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);

FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
                    params.gravity = Gravity.BOTTOM;
                    myFrameLayout..setLayoutParams(params);
    }
  }
}

this will set the gravity only after the frame layout created.

batsheva
  • 2,175
  • 1
  • 20
  • 32
0

Try to use Linear Layout instead of Frame Layout or use linear layout inside your frame layout.