ldjesper 37a8acf454 Spinner Picker Window Shape
PiperOrigin-RevId: 230618573
2019-01-25 13:59:37 -05:00

142 lines
5.1 KiB
Java

/*
* Copyright 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.material.picker;
import com.google.android.material.R;
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import androidx.annotation.AttrRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StyleRes;
import com.google.android.material.dialog.InsetDialogOnTouchListener;
import com.google.android.material.dialog.MaterialDialogs;
import com.google.android.material.resources.MaterialAttributes;
import com.google.android.material.shape.MaterialShapeDrawable;
import android.util.TypedValue;
import java.util.Calendar;
/** A Material version of {@link android.app.DatePickerDialog} * */
public class MaterialDatePickerDialog extends DatePickerDialog {
@AttrRes private static final int DEF_STYLE_ATTR = android.R.attr.datePickerStyle;
@StyleRes
private static final int DEF_STYLE_RES =
R.style.MaterialAlertDialog_MaterialComponents_Picker_Date_Spinner;
private final Drawable background;
private final Rect backgroundInsets;
/**
* Creates a new date picker dialog for the current date.
*
* @param context the parent context
*/
public MaterialDatePickerDialog(@NonNull Context context) {
this(context, 0);
}
/**
* Creates a new date picker dialog for the current date.
*
* @param context the parent context
* @param themeResId the resource ID of the theme against which to inflate this dialog, or {@code
* 0} to use the parent {@code context}'s default alert dialog theme
*/
public MaterialDatePickerDialog(@NonNull Context context, int themeResId) {
this(context, themeResId, null, -1, -1, -1);
}
/**
* Creates a new date picker dialog for the specified date.
*
* @param context the parent context
* @param listener the listener to call when the user sets the date
* @param year the initially selected year
* @param month the initially selected month (0-11 for compatibility with {@link Calendar#MONTH})
* @param dayOfMonth the initially selected day of month (1-31, depending
*/
public MaterialDatePickerDialog(
@NonNull Context context,
@Nullable OnDateSetListener listener,
int year,
int month,
int dayOfMonth) {
this(context, 0, listener, year, month, dayOfMonth);
}
/**
* Creates a new date picker dialog for the specified date.
*
* @param context the parent context
* @param themeResId the resource ID of the theme against which to inflate this dialog, or {@code
* 0} to use the parent {@code context}'s default alert dialog theme
* @param listener the listener to call when the user sets the date
* @param year the initially selected year
* @param monthOfYear the initially selected month of the year (0-11 for compatibility with {@link
* Calendar#MONTH})
* @param dayOfMonth the initially selected day of month (1-31, depending
*/
public MaterialDatePickerDialog(
@NonNull Context context,
int themeResId,
@Nullable OnDateSetListener listener,
int year,
int monthOfYear,
int dayOfMonth) {
super(context, themeResId, listener, year, monthOfYear, dayOfMonth);
context = getContext();
TypedValue colorSurfaceValue =
MaterialAttributes.resolveAttributeOrThrow(
getContext(), R.attr.colorSurface, getClass().getCanonicalName());
int surfaceColor = colorSurfaceValue.data;
MaterialShapeDrawable materialShapeDrawable =
new MaterialShapeDrawable(context, null, DEF_STYLE_ATTR, DEF_STYLE_RES);
// Pre-L, windowBackground is a second background behind the Picker Dialog.
if (Build.VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
materialShapeDrawable.setFillColor(ColorStateList.valueOf(surfaceColor));
} else {
materialShapeDrawable.setFillColor(ColorStateList.valueOf(Color.TRANSPARENT));
}
backgroundInsets =
MaterialDialogs.getDialogBackgroundInsets(context, DEF_STYLE_ATTR, DEF_STYLE_RES);
background = MaterialDialogs.insetDrawable(materialShapeDrawable, backgroundInsets);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setBackgroundDrawable(background);
getWindow()
.getDecorView()
.setOnTouchListener(new InsetDialogOnTouchListener(this, backgroundInsets));
}
}