Skip to content

Commit ffb64c1

Browse files
veganafropaulfthomas
authored andcommitted
[DatePicker] expand the touch area of a month's days
PiperOrigin-RevId: 437775591
1 parent 784f901 commit ffb64c1

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

lib/java/com/google/android/material/datepicker/MonthAdapter.java

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@
1717

1818
import com.google.android.material.R;
1919

20+
import static java.lang.Math.max;
21+
2022
import android.content.Context;
23+
import android.graphics.Rect;
2124
import android.view.LayoutInflater;
25+
import android.view.TouchDelegate;
2226
import android.view.View;
2327
import android.view.ViewGroup;
2428
import android.widget.BaseAdapter;
@@ -48,6 +52,8 @@ class MonthAdapter extends BaseAdapter {
4852
+ UtcDates.getUtcCalendar().getMaximum(Calendar.DAY_OF_WEEK)
4953
- 1;
5054

55+
private static final int MIN_TOUCH_TARGET_SIZE_DP = 48;
56+
5157
final Month month;
5258
/**
5359
* The {@link DateSelector} dictating the draw behavior of {@link #getView(int, View, ViewGroup)}.
@@ -107,13 +113,13 @@ public int getCount() {
107113

108114
@NonNull
109115
@Override
110-
public TextView getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
116+
public TextView getView(
117+
int position, @Nullable View convertView, @NonNull final ViewGroup parent) {
111118
initializeStyles(parent.getContext());
112-
TextView day = (TextView) convertView;
113-
if (convertView == null) {
114-
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
115-
day = (TextView) layoutInflater.inflate(R.layout.mtrl_calendar_day, parent, false);
116-
}
119+
final TextView day = convertView == null
120+
? (TextView) LayoutInflater.from(parent.getContext()).inflate(
121+
R.layout.mtrl_calendar_day, parent, false)
122+
: (TextView) convertView;
117123
int offsetPosition = position - firstPositionInMonth();
118124
if (offsetPosition < 0 || offsetPosition >= month.daysInMonth) {
119125
day.setVisibility(View.GONE);
@@ -139,6 +145,22 @@ public TextView getView(int position, @Nullable View convertView, @NonNull ViewG
139145
return day;
140146
}
141147
updateSelectedState(day, date);
148+
parent.post(new Runnable() {
149+
@Override
150+
public void run() {
151+
final Rect delegateArea = new Rect();
152+
day.getHitRect(delegateArea);
153+
154+
int dayTouchTargetDeltaVertical = max(0, MIN_TOUCH_TARGET_SIZE_DP - day.getHeight());
155+
int dayTouchTargetDeltaHorizontal = max(0, MIN_TOUCH_TARGET_SIZE_DP - day.getWidth());
156+
157+
delegateArea.top -= dayTouchTargetDeltaVertical;
158+
delegateArea.right += dayTouchTargetDeltaHorizontal;
159+
delegateArea.bottom += dayTouchTargetDeltaVertical;
160+
delegateArea.left -= dayTouchTargetDeltaHorizontal;
161+
parent.setTouchDelegate(new TouchDelegate(delegateArea, day));
162+
}
163+
});
142164
return day;
143165
}
144166

0 commit comments

Comments
 (0)