0

I have a problem in my activity: it has FragmentContainerView that initially show the home fragment and, when the activity's method onCreate start, it show a BottomSheetDialog. When I show the BottomSheetDialog the Home Fragment disappear. From what I know the problem is the replace method, so I think that I should insert the Dialog in a child fragment, but I don't know how.

This is my project:

MainActivity onCreate

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    toolbar.setSubtitle("Workout");

    toolbar.setNavigationOnClickListener(v -> {
        getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainerView, new HomeFragment()).commit();

        toolbar.setSubtitle("Workout");
    });

    bottomSheetDialog = new BottomSheetDialog(this, R.style.BottomSheetTheme);
    bottomSheetDialog.setContentView(LayoutInflater.from(getApplicationContext()).
            inflate(R.layout.bottom_sheet_layout, findViewById(R.id.bottom_sheet)));
    bottomSheetDialog.show();
}

activitymain.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
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="match_parent"
tools:context=".MainActivity">

<androidx.fragment.app.FragmentContainerView
    android:id="@+id/fragmentContainerView"
    android:name="com.example.myproject.Fragments.HomeFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="?actionBarSize"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:layout="@layout/fragment_home" />

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?actionBarSize"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:menu="@menu/items"
    app:navigationIcon="@drawable/app_logo"
    app:titleTextColor="@color/white"
    app:subtitleTextColor="@color/white" />

</androidx.constraintlayout.widget.ConstraintLayout>

bottom_sheet_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bottom_sheet_background">

<TextView
    android:id="@+id/titleView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:gravity="center"
    android:text="title"
    android:textColor="@color/red_700"
    android:textSize="20sp"
    android:textStyle="bold"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:text="text"
    android:textColor="#E53935"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/titleView" />
</androidx.constraintlayout.widget.ConstraintLayout>

bottom_sheet_backgound.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle">
  <solid android:color="@color/white"/>
  <corners android:topLeftRadius="20dp" android:topRightRadius="20dp"/>
</shape>

themes.xml

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <!-- Primary brand color. -->
    <item name="colorPrimary">@color/red_700</item>
    <item name="colorPrimaryVariant">@color/red_900</item>
    <item name="android:background">@color/black</item>
    <item name="android:textColor">@color/white</item>
    <!-- Status bar color. -->
    <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
    <!-- Customize your theme here. -->
    <item name="android:listDivider">@drawable/divider</item>
</style>

<style name="BottomSheetTheme" parent="Theme.Design.BottomSheetDialog">
    <item name="bottomSheetStyle">@style/BottomSheetStyle</item>
</style>

<style name="BottomSheetStyle" parent="Widget.Design.BottomSheet.Modal">
    <item name="background">@android:color/transparent</item>
</style>
</resources>

If you need to see other files, tell me

1 Answers1

0

Ok problem resolved. For first I was wrong in style and I changed from:

<item name="background">@android:color/transparent</item>

to:

<item name="android:background">@android:color/transparent</item>

The real problem was that BottomSheetDialog's theme has setted the app's theme background like default background, so the background was black. Resolved by adding:

<item name="android:background">@android:color/transparent</item>

in BottomSheetTheme