How to set a gradient background to a Material Button?

To use a custom drawable background with the MaterialButton you can use the android:background attribute: <MaterialButton app:backgroundTint=”@null” android:background=”@drawable/bkg_button_gradient” … /> NOTE: It requires at least the version 1.2.0-alpha06 Currently it is very important to add app:backgroundTint=”@null” to avoid that the custom background doesn’t get tinted by default with the backgroundTint color. With lower versions of … Read more

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);

Is any difference between a MaterialButton and a simple Button?

If you are using a MaterialComponents Theme there is no difference between <Button /> and <com.google.android.material.button.MaterialButton />. There is an auto-inflation enabled which will replace <Button with <com.google.android.material.button.MaterialButton at runtime. The MaterialComponentsViewInflater replaces some framework widgets with Material Components ones at inflation time, provided if a MaterialComponents theme is in use. Something similar happens also … Read more

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