From 55d1d26879a510703c69fdd1b4b3141a06e2f378 Mon Sep 17 00:00:00 2001 From: Vavassor Date: Wed, 3 May 2017 18:33:15 -0400 Subject: [PATCH] Fixes crash on android version Lollipop and earlier due to an unsupported way vector drawable icons were assigned to radio buttons. --- .../tusky/ComposeOptionsFragment.java | 39 ++++++++++++++++++- .../com/keylesspalace/tusky/MainActivity.java | 5 ++- .../res/layout/fragment_compose_options.xml | 8 ---- app/src/main/res/values/attrs.xml | 1 + app/src/main/res/values/styles.xml | 2 + 5 files changed, 44 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/ComposeOptionsFragment.java b/app/src/main/java/com/keylesspalace/tusky/ComposeOptionsFragment.java index 3f3e1c0b..1b24c27e 100644 --- a/app/src/main/java/com/keylesspalace/tusky/ComposeOptionsFragment.java +++ b/app/src/main/java/com/keylesspalace/tusky/ComposeOptionsFragment.java @@ -16,9 +16,15 @@ package com.keylesspalace.tusky; import android.content.Context; +import android.content.res.ColorStateList; +import android.graphics.drawable.Drawable; +import android.os.Build; import android.os.Bundle; +import android.support.annotation.DrawableRes; import android.support.annotation.Nullable; import android.support.design.widget.BottomSheetDialogFragment; +import android.support.graphics.drawable.VectorDrawableCompat; +import android.support.v4.graphics.drawable.DrawableCompat; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -85,8 +91,16 @@ public class ComposeOptionsFragment extends BottomSheetDialogFragment { } radio.check(radioCheckedId); + RadioButton publicButton = (RadioButton) rootView.findViewById(R.id.radio_public); + RadioButton unlistedButton = (RadioButton) rootView.findViewById(R.id.radio_unlisted); + RadioButton privateButton = (RadioButton) rootView.findViewById(R.id.radio_private); + RadioButton directButton = (RadioButton) rootView.findViewById(R.id.radio_direct); + setRadioButtonDrawable(getContext(), publicButton, R.drawable.ic_public_24dp); + setRadioButtonDrawable(getContext(), unlistedButton, R.drawable.ic_lock_open_24dp); + setRadioButtonDrawable(getContext(), privateButton, R.drawable.ic_lock_outline_24dp); + setRadioButtonDrawable(getContext(), directButton, R.drawable.ic_email_24dp); + if (isReply) { - RadioButton publicButton = (RadioButton) rootView.findViewById(R.id.radio_public); publicButton.setEnabled(false); } @@ -132,4 +146,27 @@ public class ComposeOptionsFragment extends BottomSheetDialogFragment { } }); } + + private static void setRadioButtonDrawable(Context context, RadioButton button, + @DrawableRes int id) { + ColorStateList list = new ColorStateList(new int[][] { + new int[] { -android.R.attr.state_checked }, + new int[] { android.R.attr.state_checked } + }, new int[] { + ThemeUtils.getColor(context, R.attr.compose_image_button_tint), + ThemeUtils.getColor(context, R.attr.colorAccent) + }); + Drawable drawable = VectorDrawableCompat.create(context.getResources(), id, + context.getTheme()); + if (drawable == null) { + return; + } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + button.setButtonTintList(list); + } else { + drawable = DrawableCompat.wrap(drawable); + DrawableCompat.setTintList(drawable, list); + } + button.setButtonDrawable(drawable); + } } diff --git a/app/src/main/java/com/keylesspalace/tusky/MainActivity.java b/app/src/main/java/com/keylesspalace/tusky/MainActivity.java index caaed452..764d1bdd 100644 --- a/app/src/main/java/com/keylesspalace/tusky/MainActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/MainActivity.java @@ -27,8 +27,8 @@ import android.os.PersistableBundle; import android.support.annotation.NonNull; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.TabLayout; +import android.support.graphics.drawable.VectorDrawableCompat; import android.support.v4.app.Fragment; -import android.support.v4.content.ContextCompat; import android.support.v4.view.ViewPager; import android.text.SpannableStringBuilder; import android.text.Spanned; @@ -274,7 +274,8 @@ public class MainActivity extends BaseActivity implements SFragment.OnUserRemove } }); - Drawable muteDrawable = ContextCompat.getDrawable(this, R.drawable.ic_mute_24dp); + VectorDrawableCompat muteDrawable = VectorDrawableCompat.create(getResources(), + R.drawable.ic_mute_24dp, getTheme()); ThemeUtils.setDrawableTint(this, muteDrawable, R.attr.toolbar_icon_tint); drawer = new DrawerBuilder() diff --git a/app/src/main/res/layout/fragment_compose_options.xml b/app/src/main/res/layout/fragment_compose_options.xml index 818cf656..a414a6b9 100644 --- a/app/src/main/res/layout/fragment_compose_options.xml +++ b/app/src/main/res/layout/fragment_compose_options.xml @@ -15,48 +15,40 @@ diff --git a/app/src/main/res/values/attrs.xml b/app/src/main/res/values/attrs.xml index 0b1e3b2d..b151ffc9 100644 --- a/app/src/main/res/values/attrs.xml +++ b/app/src/main/res/values/attrs.xml @@ -37,6 +37,7 @@ + diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index a6454085..0256ee02 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -62,6 +62,7 @@ @drawable/border_background_dark @color/image_button_dark @color/color_accent_dark + @color/image_button_dark @color/color_background_dark @drawable/status_divider_dark @@ -148,6 +149,7 @@ @drawable/border_background_light @color/image_button_light @color/color_accent_light + @color/image_button_light @color/report_status_background_light @drawable/report_status_divider_light