Change dialog button color

You can use the MaterialAlertDialogBuilder provided by the Material Components library which allows the creation of a Material AlertDialog. Just use: new MaterialAlertDialogBuilder(MainActivity.this, R.style.MyThemeOverlay_MaterialComponents_MaterialAlertDialog) .setTitle(“Dialog”) .setMessage(“Lorem ipsum dolor ….”) .setPositiveButton(“Ok”, /* listener = */ null) .setNegativeButton(“Cancel”, /* listener = */ null) .show(); Then define your custom style, using the buttonBarPositiveButtonStyle and buttonBarNegativeButtonStyle attributes: <!– Alert … Read more

Badge on BottomNavigationView

You can use the BottomNavigationView provided by the Material Components Library. Just add the BottomNavigationView to your layout: <com.google.android.material.bottomnavigation.BottomNavigationView android:layout_gravity=”bottom” app:menu=”@menu/navigation_main” ../> Then use in your code: int menuItemId = bottomNavigationView.getMenu().getItem(0).getItemId(); BadgeDrawable badge = bottomNavigationView.getOrCreateBadge(menuItemId); badge.setNumber(2); To change the badge gravity use the setBadgeGravity method. badge.setBadgeGravity(BadgeDrawable.BOTTOM_END);

Style SnackBar in theme app

With the Material Components Library you can globally change the snackbar style in your app theme: <style name=”AppTheme” parent=”Theme.MaterialComponents.*”> <!– Style to use for Snackbars in this theme. –> <item name=”snackbarStyle”>@style/Widget.MaterialComponents.Snackbar</item> <!– Style to use for action button within a Snackbar in this theme. –> <item name=”snackbarButtonStyle”>@style/Widget.MaterialComponents.Button.TextButton.Snackbar</item> <!– Style to use for message text within … Read more

bottomSheetDialogFragment full screen

In your custom BottomSheetDialogFragment you can use something like: @NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) { Dialog dialog = super.onCreateDialog(savedInstanceState); dialog.setOnShowListener(new DialogInterface.OnShowListener() { @Override public void onShow(DialogInterface dialogInterface) { BottomSheetDialog bottomSheetDialog = (BottomSheetDialog) dialogInterface; setupFullHeight(bottomSheetDialog); } }); return dialog; } private void setupFullHeight(BottomSheetDialog bottomSheetDialog) { FrameLayout bottomSheet = (FrameLayout) bottomSheetDialog.findViewById(R.id.design_bottom_sheet); BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet); ViewGroup.LayoutParams … Read more

MaterialDatePicker get selected dates

Just use the addOnPositiveButtonClickListener listener called when the user confirms a valid selection: For a single date picker: picker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener<Long>() { @Override public void onPositiveButtonClick(Long selection) { // Do something… //Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone(“UTC”)); //calendar.setTimeInMillis(selection); } }); For a range date picker: MaterialDatePicker<Pair<Long, Long>> pickerRange = builderRange.build(); pickerRange.show(….); pickerRange.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener<Pair<Long, Long>>() { @Override public void … Read more

Change Chip Widget style programmatically not working – Android

You can’t use the constructor val chip = Chip(context, null, R.style.CustomChipChoice) because the 3rd parameter isn’t the style but the attribute in the theme as R.attr.chipStyle. The Chip hasn’t a constructor with 4 parameters as other components because it extends AppCompatCheckbox which does not support a 4 parameter constructor. However you can use something different. … Read more

Android material chip component crashing app. Unable to inflate xml

Update your app theme to inherit from one of these themes: Theme.MaterialComponents Theme.MaterialComponents.NoActionBar Theme.MaterialComponents.Light Theme.MaterialComponents.Light.NoActionBar Theme.MaterialComponents.Light.DarkActionBar For example: <style name=”AppTheme” parent=”Theme.MaterialComponents.Light.NoActionBar”> Note: Using a Material Components theme enables a custom view inflater Source: https://www.material.io/develop/android/docs/getting-started/

Round corner for BottomSheetDialogFragment

Create a custom drawable rounded_dialog.xml: <?xml version=”1.0″ encoding=”utf-8″?> <shape xmlns:android=”http://schemas.android.com/apk/res/android” android:shape=”rectangle”> <solid android:color=”@android:color/white”/> <corners android:topLeftRadius=”16dp” android:topRightRadius=”16dp”/> </shape> Then override bottomSheetDialogTheme on styles.xml using the drawable as background: <style name=”AppTheme” parent=”Theme.AppCompat.Light.NoActionBar”> <item name=”bottomSheetDialogTheme”>@style/AppBottomSheetDialogTheme</item> </style> <style name=”AppBottomSheetDialogTheme” parent=”Theme.Design.Light.BottomSheetDialog”> <item name=”bottomSheetStyle”>@style/AppModalStyle</item> </style> <style name=”AppModalStyle” parent=”Widget.Design.BottomSheet.Modal”> <item name=”android:background”>@drawable/rounded_dialog</item> </style> This will change all the BottomSheetDialogs of your app.