DialogFragment fullscreen dialog overlaps with StatusBar

I created a full-screen dialogue with the official guide

The problem is that my toolbar is overlapping with the status bar, and I cannot figure out how to solve this.

DialogFragment

public class CreateAccountDialogFragment extends BaseDialogFragment { @Inject CreateAccountViewModel viewModel; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //InjectDependencies.... View rootView = createDataBinding(inflater, container); createToolbar(rootView); return rootView; } private View createDataBinding(LayoutInflater inflater, ViewGroup container) { CreateAccountDialogFragmentBinding binding = DataBindingUtil.inflate(inflater, R.layout.create_account_dialog_fragment, container, false); binding.setViewModel(viewModel); return binding.getRoot(); } private void createToolbar(View rootView) { Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar); // Set an OnMenuItemClickListener to handle menu item clicks toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { if (item.getItemId() == R.id.action_save) { viewModel.createAccount(); } return true; } }); // Inflate a menu to be displayed in the toolbar toolbar.inflateMenu(R.menu.create_account_menu); toolbar.setTitle(R.string.create_account); toolbar.setNavigationIcon(R.drawable.ic_cancel); toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialogFragment.dismiss(); } }); } } 

Markup

 <?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android"> <data> <variable name="viewModel" type="org.altoware.weity.viewmodels.CreateAccountViewModel"/> </data> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/background_light"> <include layout="@layout/toolbar"/> <LinearLayout android:layout_width="250dp" android:layout_height="wrap_content" android:layout_centerInParent="true" android:orientation="vertical"> <org.altoware.weity.views.BindableEditText android:id="@+id/editTextUsername" android:layout_width="match_parent" android:layout_height="wrap_content" android:addTextChangedListener="@{viewModel.onUsernameChanged}" android:hint="Username" android:singleLine="true"/> <org.altoware.weity.views.BindableEditText android:id="@+id/editTextPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:addTextChangedListener="@{viewModel.onPasswordChanged}" android:hint="Password" android:inputType="textPassword" android:singleLine="true"/> </LinearLayout> </RelativeLayout> 

Toolbar

 <?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay" tools:showIn="@layout/activity_login"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay"/> </android.support.design.widget.AppBarLayout> 

Action Creation Dialog

 @Subscribe public void showNewAccountDialog(OttoMessages.Login.ShowNewAccountDialog evt) { FragmentManager fragmentManager = getSupportFragmentManager(); CreateAccountDialogFragment newFragment = new CreateAccountDialogFragment(); boolean isLargeLayout = false; if (isLargeLayout) { newFragment.show(fragmentManager, "dialog"); } else { FragmentTransaction transaction = fragmentManager.beginTransaction(); transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); transaction.add(android.R.id.content, newFragment) .addToBackStack(null).commit(); } } 

Screenshot

EDIT

After removing StatusBarTransparency from v21 styles, it looks like this: enter image description here

+9
java android android-fragments dialogfragment dialog
source share
3 answers

After removing the transparency color of the StatusBar , I found that now you can add padding if the status bar does not turn white.

RelativeLayout my DialogFragment now looks like this.

  <RelativeLayout android:paddingTop="24dp" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/background_light"> 

app / src / main / res / values-v21 / styles.xml

 <resources>> <style name="AppTheme.NoActionBar"> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> <item name="android:windowDrawsSystemBarBackgrounds">true</item> <!-- REMOVE THIS LINE <item name="android:statusBarColor">@android:color/transparent</item> --> </style> </resources> 

Attention
I have not tested this on devices other than the Nexus 5.

+2
source share

I had exactly this problem, and I managed to solve it. The problem is in the transaction part .add (containerId, fragment).

It has a value: transaction.add(android.R.id.content, fragment) , and it is its value that is equal to android.R.id.content, which causes overlapping.

Instead, set it to the identifier of the parent content frame in the calling action.

For example, in my code, the main parent activity layout was DrawerLayout with an identifier in the pocket_layout field, so my fix was

  MyDialogFragment fragment = new MyDialogFragment (); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); transaction.add(R.id.drawer_layout, frag) .addToBackStack(null).commit(); 
+7
source share

If you need DialogFragment in full screen mode, as well as a status bar with your own color, you can add this to the onCreate () method of your DialogFragment.

  setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_DeviceDefault_Light_NoActionBar_Fullscreen); 

You can also add the code below in the onCreateView method for the color of the status bar.

  getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); getDialog().getWindow().setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark)); 
-one
source share

All Articles