Skip to content

Commit

Permalink
Use MaterialAlertDialogBuilder and M3 themes
Browse files Browse the repository at this point in the history
Closes #85
  • Loading branch information
eltos committed May 21, 2023
1 parent 85f16ec commit 2c82864
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.appcompat.app.AlertDialog;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import android.text.Html;
import android.util.TypedValue;
import android.view.View;
Expand Down Expand Up @@ -504,11 +505,11 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
}
}
if (theme != null) {
dialog = new AlertDialog.Builder(getContext(), theme).create();
dialog = new MaterialAlertDialogBuilder(getContext(), theme).create();
setStyle(STYLE_NORMAL, theme);
} else {
// default theme or 'alertDialogTheme'
dialog = new AlertDialog.Builder(getContext()).create();
dialog = new MaterialAlertDialogBuilder(getContext()).create();
}

context = dialog.getContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class SimplePinDialog extends CustomViewDialog<SimplePinDialog> {

public static SimplePinDialog build(){
return new SimplePinDialog()
.theme(eltos.simpledialogfragment.R.style.SimpleDialogTheme);
.theme(eltos.simpledialogfragment.R.style.SimpleDialogTheme_AppCompat);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
app:counterOverflowTextAppearance="@style/OverflowText">

<eltos.simpledialogfragment.input.TextInputAutoCompleteTextView
style="?attr/editTextStyle"
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Expand Down
11 changes: 11 additions & 0 deletions simpledialogfragments/src/main/res/values-night/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>


<!-- app compat -->
<style name="SimpleDialogTheme.AppCompat" parent="ThemeOverlay.AppCompat.Dialog.Alert">
<item name="colorSurface">#212121</item>
</style>


</resources>
20 changes: 12 additions & 8 deletions simpledialogfragments/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,33 @@
<!-- base styles for all simple dialogs
-->

<!-- default -->
<style name="SimpleDialogTheme" parent="ThemeOverlay.MaterialComponents.Dialog.Alert">
<!-- app compat -->
<style name="SimpleDialogTheme.AppCompat" parent="ThemeOverlay.AppCompat.Dialog.Alert">
<item name="textInputStyle">@style/Widget.Design.TextInputLayout</item>
<item name="colorSurface">#fff</item>
</style>

<!-- material design -->
<style name="SimpleDialogTheme.Material">
<!-- material components -->
<style name="SimpleDialogTheme.MaterialComponents" parent="ThemeOverlay.MaterialComponents.Dialog.Alert">
<item name="textInputStyle">@style/SimpleDialogMaterialTextInputStyle</item>
<item name="editTextStyle">@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox</item>
</style>
<style name="SimpleDialogMaterialTextInputStyle" parent="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense.ExposedDropdownMenu">
<item name="endIconMode">none</item>
</style>

<!-- material 3 design -->
<!-- material 3 -->
<style name="SimpleDialogTheme.Material3" parent="ThemeOverlay.Material3.Dialog">
<item name="textInputStyle">@style/SimpleDialogMaterial3TextInputStyle</item>
<item name="editTextStyle">@style/Widget.Material3.TextInputLayout.OutlinedBox</item>
</style>
<style name="SimpleDialogMaterial3TextInputStyle" parent="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense.ExposedDropdownMenu">
<style name="SimpleDialogMaterial3TextInputStyle" parent="@style/Widget.Material3.TextInputLayout.OutlinedBox.Dense.ExposedDropdownMenu">
<item name="endIconMode">none</item>
</style>


<!-- deprecated aliases -->
<style name="SimpleDialogTheme" parent="SimpleDialogTheme.MaterialComponents"/>
<style name="SimpleDialogTheme.Material" parent="SimpleDialogTheme.MaterialComponents"/>



Expand All @@ -41,10 +44,11 @@
-->

<!-- image dialog -->
<style name="ImageDialogTheme" parent="SimpleDialogTheme">
<style name="ImageDialogTheme" parent="ThemeOverlay.AppCompat.Dialog.Alert">
<!-- these are required so that the dialog wraps the images dimensions -->
<item name="android:windowMinWidthMinor">@null</item>
<item name="android:windowMinWidthMajor">@null</item>
<item name="colorSurface">#0000</item>
</style>

</resources>
49 changes: 34 additions & 15 deletions testApp/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -1,35 +1,55 @@
<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
<!-- Base application theme.
Example themes to inherit from:
- Theme.AppCompat.DayNight
- Theme.MaterialComponents.DayNight
- Theme.Material3.DayNight
-->
<style name="AppTheme" parent="Theme.Material3.DayNight">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorSecondary">@color/colorAccent</item>

<!-- Optional custom theme for all dialogs
Use simpleDialogTheme for simple dialogs only or alertDialogTheme for all dialogs
You can directly reference the default themes or use them as parent:
- SimpleDialogTheme
- SimpleDialogTheme.Material
<!-- Optional custom theme
- simpleDialogTheme applies to simple dialogs only
- alertDialogTheme applies to all dialogs (including simple dialogs)
- imageDialogTheme applies to image dialogs only
-->
<item name="simpleDialogTheme">@style/MyDialogTheme</item>
<!--<item name="alertDialogTheme">@style/SimpleDialogTheme</item>-->
<!--<item name="alertDialogTheme">@style/SimpleDialogTheme.Material</item>-->
<item name="simpleDialogTheme">@style/MyDialogTheme.Material3</item>
<!--<item name="alertDialogTheme">@style/MyDialogTheme.MaterialComponents</item>-->
<!--<item name="imageDialogTheme">@style/MyImageDialogTheme</item>-->
</style>


<style name="MyDialogTheme" parent="SimpleDialogTheme.Material">
<!-- customizations for all dialogs -->
<!-- Customized theme for dialogs
Example themes to inherit from:
- SimpleDialogTheme.AppCompat
- SimpleDialogTheme.MaterialComponents
- SimpleDialogTheme.Material3
-->
<style name="MyDialogTheme.AppCompat" parent="SimpleDialogTheme.AppCompat">
<item name="colorAccent">@color/colorAccent</item>
<item name="colorPrimary">@color/colorAccent</item>
</style>
<style name="MyDialogTheme.MaterialComponents" parent="SimpleDialogTheme.MaterialComponents">
<item name="colorPrimary">@color/colorAccent</item>
</style>
<style name="MyDialogTheme.Material3" parent="SimpleDialogTheme.Material3">
<item name="colorPrimary">@color/colorAccent</item>
</style>


<style name="MyFancyDialogTheme" parent="SimpleDialogTheme">



<!-- A very fancy theme with custom colors and custom text styles -->
<style name="MyFancyDialogTheme" parent="SimpleDialogTheme.Material3">
<!-- customizations per dialog, passed via the SimpleDialog.theme(...) method -->
<item name="android:colorBackground">#282828</item>
<item name="colorPrimary">#a000ff</item>
<item name="colorSecondary">#d50000</item>
<item name="colorSurface">#abcdef</item>
<item name="android:textColorPrimary">#33691e</item>
<item name="checkboxStyle">@style/MyCheckBoxStyle</item>
<!-- MaterialComponent inputs have two style: for the TextInputLayout and the TextInputEditText -->
Expand All @@ -50,12 +70,11 @@

<!-- When customizing the theme for a SimpleImageDialog you must either
inherit from "ImageDialogTheme" or specify the two attributes as shown below -->
<style name="MyImageDialogThemeA" parent="MyDialogTheme">
<style name="MyImageDialogThemeA">
<item name="android:windowMinWidthMinor">@null</item> <!-- these are required if parent -->
<item name="android:windowMinWidthMajor">@null</item> <!-- is not ImageDialogTheme -->
<!-- customizations -->
</style>

<style name="MyImageDialogThemeB" parent="ImageDialogTheme">
<!-- customizations -->
</style>
Expand Down

0 comments on commit 2c82864

Please sign in to comment.