FloatingActionButton: Make attribute grabbing not be terrible

Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
Harsh Shandilya 2018-06-01 17:11:22 +05:30
parent 32d669a661
commit 4edfdd8f3b
3 changed files with 10 additions and 11 deletions

View File

@ -7,6 +7,7 @@
package com.wireguard.android.widget.fab; package com.wireguard.android.widget.fab;
import android.content.Context; import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray; import android.content.res.TypedArray;
import android.graphics.*; import android.graphics.*;
import android.graphics.Paint.Style; import android.graphics.Paint.Style;
@ -16,9 +17,9 @@ import android.graphics.drawable.ShapeDrawable.ShaderFactory;
import android.graphics.drawable.shapes.OvalShape; import android.graphics.drawable.shapes.OvalShape;
import android.support.annotation.*; import android.support.annotation.*;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatDelegate;
import android.support.v7.widget.AppCompatImageButton; import android.support.v7.widget.AppCompatImageButton;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.TextView; import android.widget.TextView;
import com.wireguard.android.R; import com.wireguard.android.R;
@ -58,14 +59,13 @@ public class FloatingActionButton extends AppCompatImageButton {
init(context, attrs); init(context, attrs);
} }
//TODO(msf): make not terrible
public static int getColorFromTheme(final Context context, final int themeResource, @ColorRes final int fallback) { public static int getColorFromTheme(final Context context, final int themeResource, @ColorRes final int fallback) {
TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{themeResource}); final TypedValue typedValue = new TypedValue();
try { final Resources.Theme theme = context.getTheme();
return a.getColor(0, ContextCompat.getColor(context, fallback)); theme.resolveAttribute(themeResource, typedValue, true);
} finally { @ColorInt final int color = typedValue.data;
a.recycle(); return color == 0 ? fallback : color;
}
} }
void init(final Context context, final AttributeSet attributeSet) { void init(final Context context, final AttributeSet attributeSet) {

View File

@ -3,6 +3,5 @@
<!-- TODO(msf): remove these two hard-coded colors --> <!-- TODO(msf): remove these two hard-coded colors -->
<color name="list_item_activated">#cfd8dc</color> <!-- Blue Grey 200 --> <color name="list_item_activated">#cfd8dc</color> <!-- Blue Grey 200 -->
<color name="list_item_ripple">#808e95</color> <!-- Blue Grey 200 dark --> <color name="list_item_ripple">#808e95</color> <!-- Blue Grey 200 dark -->
<color name="accent">#2196F3</color>
<!-- TODO(zx2c4): set this once we have a color decided - <color name="accent">#2196F3</color> -->
</resources> </resources>

View File

@ -2,7 +2,7 @@
<resources xmlns:android="http://schemas.android.com/apk/res/android"> <resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar"> <style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
<!--TODO(zx2c4): set this once we have a color decided: <item name="colorAccent">@color/accent</item>--> <item name="colorAccent">@color/accent</item>
</style> </style>
<style name="SettingsTheme" parent="AppTheme"> <style name="SettingsTheme" parent="AppTheme">