0

I get the root view (Which in my case happens to be RelativeLayout) using the below code:

final ViewGroup viewGroup = (ViewGroup) ((ViewGroup) this
        .findViewById(android.R.id.content)).getChildAt(0);

Source: Get root view from current activity

Now I want to set the size and the layout gravity of the root view. But I don't know the layout type of its parent

The below code works when the view I want to set it's layout params is inside a LinearLayout.

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
params.weight = 1.0f;
params.gravity = Gravity.TOP;

button.setLayoutParams(params);

Source: How to set layout_gravity programmatically?

Jack
  • 693
  • 1
  • 7
  • 25

1 Answers1

0

I believe you need DecorView.LayoutParams.

The root view is added to a DecorView, which is attached to the Activity's Window.

PhoneWindow.java


Since the DecorView in question isn't accessible to apps in general, you can use FrameLayout.LayoutParams.

TheWanderer
  • 16,775
  • 6
  • 49
  • 63
  • Thank you, it turns out that the parent of the root view is a FrameLayout, as my app crashed and AS says can't cast RelativeLayout (root) to FrameLayout when I use rootview.setLayoutParams(params); – Jack Nov 12 '18 at 20:59
  • Oh right, the DecorView you're using is probably from the ViewPager class, since the internal one is hidden. I guess ViewPager's extends RelativeLayout and not FrameLayout. – TheWanderer Nov 12 '18 at 21:01