Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Color tint support for @BindDrawable #976

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package butterknife;

import android.support.annotation.AttrRes;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
Expand All @@ -23,5 +24,5 @@
@DrawableRes int value();

/** Color attribute resource ID that is used to tint the drawable. */
@AttrRes int tint() default 0;
@ColorRes @AttrRes int tint() default 0;
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
package com.example.butterknife.library;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.butterknife.R;

import java.util.List;

import butterknife.BindDrawable;
import butterknife.BindString;
import butterknife.BindView;
import butterknife.BindViews;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnItemClick;
import butterknife.OnLongClick;
import com.example.butterknife.R;

import java.util.List;

import static android.widget.Toast.LENGTH_SHORT;

Expand All @@ -38,10 +43,12 @@ public class SimpleActivity extends Activity {
@BindView(R.id.hello) Button hello;
@BindView(R.id.list_of_things) ListView listOfThings;
@BindView(R.id.footer) TextView footer;
@BindView(R.id.image) ImageView image;
@BindString(R.string.app_name) String butterKnife;
@BindString(R.string.field_method) String fieldMethod;
@BindString(R.string.by_jake_wharton) String byJakeWharton;
@BindString(R.string.say_hello) String sayHello;
@BindDrawable(value = R.drawable.ic_adb, tint = android.R.color.darker_gray) Drawable drawable;

@BindViews({ R.id.title, R.id.subtitle, R.id.hello }) List<View> headerViews;

Expand All @@ -67,6 +74,7 @@ public class SimpleActivity extends Activity {
ButterKnife.bind(this);

// Contrived code to use the bound fields.
image.setImageDrawable(drawable);
title.setText(butterKnife);
subtitle.setText(fieldMethod);
footer.setText(byJakeWharton);
Expand Down
11 changes: 11 additions & 0 deletions butterknife-integration-test/src/main/res/drawable/ic_adb.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>

<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M5,16c0,3.87 3.13,7 7,7s7,-3.13 7,-7v-4L5,12v4zM16.12,4.37l2.1,-2.1 -0.82,-0.83 -2.3,2.31C14.16,3.28 13.12,3 12,3s-2.16,0.28 -3.09,0.75L6.6,1.44l-0.82,0.83 2.1,2.1C6.14,5.64 5,7.68 5,10v1h14v-1c0,-2.32 -1.14,-4.36 -2.88,-5.63zM9,9c-0.55,0 -1,-0.45 -1,-1s0.45,-1 1,-1 1,0.45 1,1 -0.45,1 -1,1zM15,9c-0.55,0 -1,-0.45 -1,-1s0.45,-1 1,-1 1,0.45 1,1 -0.45,1 -1,1z"/>
</vector>
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
android:orientation="vertical"
android:padding="8dp"
tools:ignore="SelectableText">

<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:contentDescription="@string/tinted_drawable" />
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
<string name="field_method">Field and method binding for Android views.</string>
<string name="by_jake_wharton">by Jake Wharton</string>
<string name="say_hello">Say Hello</string>
<string name="tinted_drawable">A tinted drawable</string>
</resources>
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.example.butterknife.library;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import com.example.butterknife.R;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;

import butterknife.ButterKnife;
import butterknife.Unbinder;

import static com.google.common.truth.Truth.assertThat;

@RunWith(RobolectricTestRunner.class)
Expand All @@ -22,6 +24,7 @@ public class SimpleActivityTest {
}

protected static void verifySimpleActivityBound(SimpleActivity activity) {
assertThat(activity.image.getId()).isEqualTo(R.id.image);
assertThat(activity.title.getId()).isEqualTo(R.id.title);
assertThat(activity.subtitle.getId()).isEqualTo(R.id.subtitle);
assertThat(activity.hello.getId()).isEqualTo(R.id.hello);
Expand All @@ -30,6 +33,7 @@ protected static void verifySimpleActivityBound(SimpleActivity activity) {
}

protected static void verifySimpleActivityUnbound(SimpleActivity activity) {
assertThat(activity.image).isNull();
assertThat(activity.title).isNull();
assertThat(activity.subtitle).isNull();
assertThat(activity.hello).isNull();
Expand Down
15 changes: 10 additions & 5 deletions butterknife/src/main/java/butterknife/internal/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.support.annotation.AttrRes;
import android.support.annotation.ColorRes;
import android.support.annotation.DimenRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.IdRes;
Expand All @@ -21,13 +22,17 @@ public final class Utils {

@UiThread // Implicit synchronization for use of shared resource VALUE.
public static Drawable getTintedDrawable(Context context,
@DrawableRes int id, @AttrRes int tintAttrId) {
boolean attributeFound = context.getTheme().resolveAttribute(tintAttrId, VALUE, true);
@DrawableRes int id, @ColorRes @AttrRes int tintId) {
boolean attributeFound = context.getTheme().resolveAttribute(tintId, VALUE, true);
if (!attributeFound) {
VALUE.resourceId = tintId;
}

if (VALUE.resourceId == 0) {
throw new Resources.NotFoundException("Required tint color attribute with name "
+ context.getResources().getResourceEntryName(tintAttrId)
+ " and attribute ID "
+ tintAttrId
+ context.getResources().getResourceEntryName(tintId)
+ " and attribute- or color resource-ID "
+ tintId
+ " was not found.");
}

Expand Down