Android: How to hide activity (or make it transparent) while showing a bottom sheet?

596 views Asked by At

I'm a newbie to Android programming.

I'm building an app with sharing feature as the first picture.

When the user shares a file, I want to show the bottom sheet menu as dialog like the 2nd picture.

But now the activity is covering the main screen like 3rd picture.

How can I hide the activity or make it transparent (result like 2nd image)?

Thank you for your help!!!

https://material.io/components/sheets-bottom/android

(I made these images by Photoshop)

How I added my activity to the Android sharing sheet in AndroidManifest.xml

    <activity
        android:name=".ActivityShare"
        android:exported="true"
        android:label="@string/ActivityShareName">
        <intent-filter>
            <category android:name="android.intent.category.DEFAULT" />

            <action android:name="android.intent.action.SEND" />
            <action android:name="android.intent.action.SEND_MULTIPLE" />

            <data android:mimeType="*/*" />
        </intent-filter>
    </activity>

How I created the bottom sheet in ActivityShareSheet.kt

class ModalBottomSheet : BottomSheetDialogFragment() {

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? = inflater.inflate(R.layout.activity_share_sheet, container, false)

companion object {
    const val TAG = "ModalBottomSheet"
    }
}

open class ActivityShareSheet : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val modalBottomSheet = ModalBottomSheet()
        modalBottomSheet.show(supportFragmentManager, ModalBottomSheet.TAG)
    }
}

1

There are 1 answers

0
Anna On BEST ANSWER

I just found a simple solution: Creating a transparent theme for the activity

But note that the theme must be based on the Theme.AppCompat family

 <!--below is the style for transparent activity and here we are using no action bar.-->
    <style name="Theme.AppCompat.Translucent" parent="Theme.AppCompat.NoActionBar">
        <!--on below line we are setting background as transparent color-->
        <item name="android:background">@android:color/transparent</item>
        <!--on below line we are displaying the windowNotitle as true as we are not displaying our status bar-->
        <item name="android:windowNoTitle">true</item>
        <!--on below line we are setting our window background as transparent color-->
        <item name="android:windowBackground">@android:color/transparent</item>
        <!--on below line we are setting color background cache hint as null-->
        <item name="android:colorBackgroundCacheHint">@null</item>
        <!--on below line we are adding a window translucent as true-->
        <item name="android:windowIsTranslucent">true</item>
        <!--on below line we are adding a window animationstyle-->
        <item name="android:windowAnimationStyle">@android:style/Animation</item>
    </style>