package com.example.materialdesigndemo; import android.annotation.SuppressLint; import android.os.Bundle; import android.widget.Button; import android.view.View; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import com.google.android.material.datepicker.MaterialDatePicker; import com.google.android.material.datepicker.MaterialPickerOnPositiveButtonClickListener; import com.google.android.material.snackbar.Snackbar; public class MainActivity extends AppCompatActivity { // Button to show the snackbar Button bShowSnackbar; private Button mPickDateButton; private TextView mShowSelectedDateText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // register the show snackbar button with the appropriate ID bShowSnackbar = findViewById(R.id.show_snackbar_button); // button click listener to show the snackbar bShowSnackbar.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Snackbar snackbar = Snackbar.make(v, "You have deleted an item", Snackbar.LENGTH_LONG); snackbar.setAction("UNDO", new View.OnClickListener() { @Override public void onClick(View v) { // perform any action when the button on the snackbar is clicked here in this case // it shows a simple toast Toast.makeText(MainActivity.this, "The item has been restored", Toast.LENGTH_SHORT).show(); } }); snackbar.show(); } }); mPickDateButton = findViewById(R.id.pick_date_button); mShowSelectedDateText = findViewById(R.id.show_selected_date); // create instance of the material date picker // builder make sure to add the "datePicker" which // is normal material date picker which is the first // type of the date picker in material design date // picker MaterialDatePicker.Builder materialDateBuilder = MaterialDatePicker.Builder.datePicker(); // define the properties of the // materialDateBuilder that is title text as SELECT A DATE materialDateBuilder.setTitleText("SELECT A DATE"); // create the instance of the material date picker final MaterialDatePicker materialDatePicker = materialDateBuilder.build(); // handle select date button which opens the // material design date picker mPickDateButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { // getSupportFragmentManager() to // interact with the fragments // associated with the material design // date picker tag is to get any error // in logcat materialDatePicker.show(getSupportFragmentManager(), "MATERIAL_DATE_PICKER"); } }); // handle the positive button click from the material design date picker materialDatePicker.addOnPositiveButtonClickListener( new MaterialPickerOnPositiveButtonClickListener() { @SuppressLint("SetTextI18n") @Override public void onPositiveButtonClick(Object selection) { // if the user clicks on the positive button // that is ok button update the selected date mShowSelectedDateText.setText("Selected Date is: " + materialDatePicker.getHeaderText()); // in the above statement, getHeaderText // is the selected date preview from the dialog } }); } }