Added fragment shows under elevated views in fragment below?

Multi tool use


Added fragment shows under elevated views in fragment below?
So I was having an issue adding my fragment for dialog to my activity. The fragment is a regular fragment added using
supportFragmentManager.beginTransaction().add(...).commit()
The problem is the fragment is added to the activity, but the activity's toolbar and floating action button will show on top of the fragment. I know this is because those two views are elevated to 4dp and it can be solved by changing the fragment elevation to < 4dp. My question is, is there a better way to do this? IS there a way to add a fragment on top of an activity? Or is this the way it has to be done?
Activity
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:elevation="4dp"
android:gravity="center"
android:text="@string/app_name"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Headline"
app:layout_constraintTop_toTopOf="parent" />
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar"/>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_margin="24dp"/>
Dialog Fragment
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrim"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="24dp"
android:background="#40000000">
<FrameLayout
android:elevation="4dp"
android:layout_gravity="center"
android:layout_width="match_parent"
android:background="@color/white"
android:layout_height="250dp"/>
2 Answers
2
When the activity started, try using visibility to hide the views, its the simplest way to do it, floatingActionButton.setVisibility(View.GONE);
In order to hide the floatingActionButton, add the following in your fragment
onCreateView
:
fragment
onCreateView
//Global Variable
private View view;
@Override
public View onCreateView(@NonNull LayoutInflater layoutInflater, ViewGroup container, Bundle savedInstanceState) {
if (view != null) {
ViewGroup group = (ViewGroup) view.getParent();
if (group != null) {
group.removeView(view);
}
} else {
floatingActionButton = view.findViewById(R.id. floatingActionButton);
floatingActionButton.hide();
...
return view;
}
in your xml:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
//If this is your toolbar, why are you instantiating a textview and not a toolbar???
<TextView
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:elevation="4dp"
android:gravity="center"
android:text="@string/app_name"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Headline"
app:layout_constraintTop_toTopOf="parent" />
// get rid off below toolbar so that the container will cover the whole height and width of the screen
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"/>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_margin="24dp"/>
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Although I'm sure your answer is fine, it doesn't answer my question. My question is trivial in that I want to better understand how things work. I'm guessing the fragment manager just adds the fragment's view to the bottom of the ViewGroup which is why it is on top of any views with the same elevation, but below the views in that ViewGroup with an elevation higher than that of the fragment. Maybe my question is a bad one, but I was wondering if there was already a solution where adding the fragment flattens the ViewGroup in terms of elevation so the fragment is on top or something...
– Darryl Johnson
yesterday