Create week button background programmatically

pull/618/head
Alex Baker 7 years ago
parent 580b9ce721
commit 79b156c333

@ -6,6 +6,8 @@ import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.LayerDrawable;
@ -14,6 +16,7 @@ import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -253,17 +256,28 @@ public class CustomRecurrenceDialog extends InjectingDialogFragment {
});
int inset = (int) context.getResources().getDimension(R.dimen.week_button_inset);
int accentColor = ResourceResolver.getData(context, R.attr.colorAccent);
int animationDuration = context.getResources().getInteger(android.R.integer.config_shortAnimTime);
for(int i = 0; i < 7; i++) {
ToggleButton weekButton = weekButtons[i];
GradientDrawable ovalDrawable = (GradientDrawable) context.getResources().getDrawable(R.drawable.week_day_button_oval).mutate();
ovalDrawable.setColor(accentColor);
LayerDrawable layerDrawable = new LayerDrawable(new Drawable[]{ovalDrawable});
layerDrawable.setLayerInset(0, inset, inset, inset, inset);
StateListDrawable stateListDrawable = new StateListDrawable();
stateListDrawable.setEnterFadeDuration(animationDuration);
stateListDrawable.setExitFadeDuration(animationDuration);
stateListDrawable.addState(new int[] {-android.R.attr.state_checked}, new ColorDrawable(Color.TRANSPARENT));
stateListDrawable.addState(new int[] {android.R.attr.state_checked}, layerDrawable);
int paddingBottom = weekButton.getPaddingBottom();
int paddingTop = weekButton.getPaddingTop();
int paddingLeft = weekButton.getPaddingLeft();
int paddingRight = weekButton.getPaddingRight();
ViewCompat.setBackground(weekButton, stateListDrawable);
weekButton.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
int dayOfWeek = dayOfWeekCalendar.get(Calendar.DAY_OF_WEEK);
ToggleButton weekButton = weekButtons[i];
((StateListDrawable) weekButton.getBackground())
.addState(new int[] {android.R.attr.state_checked}, layerDrawable);
String text = shortWeekdays[dayOfWeek];
weekButton.setTextColor(colorStateList);
weekButton.setTextOn(text);

@ -1,7 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:enterFadeDuration="@android:integer/config_shortAnimTime"
android:exitFadeDuration="@android:integer/config_shortAnimTime">
<item android:state_checked="false" android:drawable="@drawable/transparent_drawable" />
</selector>

@ -6,5 +6,5 @@
android:textAllCaps="true"
android:textSize="12sp"
android:singleLine="true"
android:background="@drawable/week_button_bg"
android:background="@null"
android:gravity="center" />

Loading…
Cancel
Save