I am trying to make the statusbar translucent, but dont want, that the content of my activity is hiding behind it. -> I have to set the top margin dynamically...
I have tried it like that: MainActivty.java:
public void onAttachedToWindow() {
super.onAttachedToWindow();
View view = findViewById(android.R.id.content).getRootView();
WindowManager.LayoutParams newLayoutParams = (WindowManager.LayoutParams) view.getLayoutParams();
view.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
@Override
public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
int x = insets.getSystemWindowInsetTop();
Log.d(TAG, "onApplyWindowInsets: " + x);
//newLayoutParams.topMargin = x;
//newLayoutParams.setMargins(0,x,0,0);
view.setLayoutParams(newLayoutParams);
return insets;
}
});
setContentView(view);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
Sadly this does not work. Error message:
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.rxstudios.rxmusic2, PID: 6324 java.lang.ClassCastException: android.view.WindowManager$LayoutParams cannot be cast to androidx.constraintlayout.widget.ConstraintLayout$LayoutParams at com.rxstudios.rxmusic2.MainActivity.onAttachedToWindow(MainActivity.java:77) at androidx.appcompat.view.WindowCallbackWrapper.onAttachedToWindow(WindowCallbackWrapper.java:129) at com.android.internal.policy.DecorView.onAttachedToWindow(DecorView.java:1537) at android.view.View.dispatchAttachedToWindow(View.java:18347) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3397) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1761) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1460) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7183) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949) at android.view.Choreographer.doCallbacks(Choreographer.java:761) at android.view.Choreographer.doFrame(Choreographer.java:696) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935) at android.os.Handler.handleCallback(Handler.java:873) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
How else would I do this if it doesn't let me get my Constraintlayout like that? Since it is a constraintlayout, I thought about getting the first child of it, which is a fragment. Can I set the margin to the fragment? If yes, how?