'BottomSheetDialog Unable to use AppCompatDelegate.setDefaultNightMode() to change background style
like this
[picture][1]https://imgtu.com/i/Lk0ARs
Beginners searched the Internet and couldn't find the results they wanted, so I came here to ask everyone, I'm sorry to trouble you!
What I want to do is that when I manually change the MODE_NIGHT using AppCompatDelegate.setDefaultNightMode(), the bottomsheetdialog can also follow the manual changes instead of still following the system.
layout
- activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="follow_system" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MODE_NIGHT_NO" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MODE_NIGHT_yes" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="show_dialog" />
</LinearLayout>
- dialog_date_picker.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/bottomSheetContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="@drawable/dialog_date_picker_backdround"
android:orientation="horizontal"
android:padding="10dp">
<LinearLayout
android:id="@+id/number_picker_height"
android:layout_width="match_parent"
android:layout_height="270dp"
android:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal">
</LinearLayout>
</LinearLayout>
java code
- MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(this, R.style.BottomSheetDialogTheme);
View bottomSheetView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.dialog_date_picker, findViewById(R.id.bottomSheetContainer));
bottomSheetDialog.setContentView(bottomSheetView);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
recreate();
}
});
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
recreate();
}
});
findViewById(R.id.button3).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
recreate();
}
});
findViewById(R.id.button4).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
bottomSheetDialog.show();
}
});
}
}
- color.xml
values/color.xml
<color name="Choose_Year_Month_BottomSheetDialog_background">#FFFFFF</color>
values-night/color.xml
<color name="Choose_Year_Month_BottomSheetDialog_background">#000000</color>
- dialog_date_picker_backdround.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/Choose_Year_Month_BottomSheetDialog_background" />
<corners
android:bottomLeftRadius="0dp"
android:bottomRightRadius="0dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp" />
</shape>
<style name="BottomSheetDialogTheme" parent="Theme.MaterialComponents.Light.BottomSheetDialog">
<item name="bottomSheetStyle">@style/BottomSheetStyle</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
<style name="BottomSheetStyle" parent="Widget.Design.BottomSheet.Modal">
<item name="android:background">@android:color/transparent</item>
</style>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
