From 80f4d2a56fecfe9fd22dd04bf890adb650e39fbd Mon Sep 17 00:00:00 2001 From: Alexander Martinz Date: Wed, 4 May 2016 12:24:20 +0200 Subject: [PATCH 1/2] add some annotations, add color methods which take resources as parameter Change-Id: I3a1f1788d8304644c1a790ab70bf480350756a2a Signed-off-by: Alexander Martinz --- .../miz/introactivity/BaseIntroFragment.java | 14 ++++-- .../com/miz/introactivity/IntroActivity.java | 50 +++++++++++++++++-- .../com/miz/introactivity/IntroFragment.java | 9 ++-- .../IntroScreenPagerAdapter.java | 5 +- .../com/miz/introactivity/NextDoneButton.java | 13 ++++- 5 files changed, 74 insertions(+), 17 deletions(-) diff --git a/library/src/main/java/com/miz/introactivity/BaseIntroFragment.java b/library/src/main/java/com/miz/introactivity/BaseIntroFragment.java index 13e126b..47c3007 100644 --- a/library/src/main/java/com/miz/introactivity/BaseIntroFragment.java +++ b/library/src/main/java/com/miz/introactivity/BaseIntroFragment.java @@ -1,6 +1,9 @@ package com.miz.introactivity; import android.os.Bundle; +import android.support.annotation.ColorInt; +import android.support.annotation.DrawableRes; +import android.support.annotation.LayoutRes; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; @@ -14,7 +17,8 @@ */ public abstract class BaseIntroFragment extends Fragment { - public static final int RESOURCE_TYPE_LAYOUT = 0, RESOURCE_TYPE_DRAWABLE = 1; + public static final int RESOURCE_TYPE_LAYOUT = 0; + public static final int RESOURCE_TYPE_DRAWABLE = 1; /** * Get the title for the intro screen. @@ -26,7 +30,7 @@ public abstract class BaseIntroFragment extends Fragment { * Get the text color of the intro screen title. * @return Text color of the title. */ - protected abstract int getTitleColor(); + @ColorInt protected abstract int getTitleColor(); /** * Get the description for the intro screen. @@ -38,19 +42,19 @@ public abstract class BaseIntroFragment extends Fragment { * Get the text color of the intro screen description. * @return Text color of the description. */ - protected abstract int getDescriptionColor(); + @ColorInt protected abstract int getDescriptionColor(); /** * Get the layout ID of the layout to inflate. * @return Layout ID to inflate. */ - protected abstract int getLayoutId(); + @LayoutRes protected abstract int getLayoutId(); /** * Get the drawable ID of the drawable to show. * @return ID of the drawable to show. */ - protected abstract int getDrawableId(); + @DrawableRes protected abstract int getDrawableId(); /** * Get the type of resource ID. Can be either RESOURCE_TYPE_LAYOUT diff --git a/library/src/main/java/com/miz/introactivity/IntroActivity.java b/library/src/main/java/com/miz/introactivity/IntroActivity.java index 94fa42a..eed83f5 100644 --- a/library/src/main/java/com/miz/introactivity/IntroActivity.java +++ b/library/src/main/java/com/miz/introactivity/IntroActivity.java @@ -3,6 +3,8 @@ import android.animation.ArgbEvaluator; import android.graphics.PorterDuff; import android.os.Bundle; +import android.support.annotation.ColorInt; +import android.support.annotation.ColorRes; import android.support.v4.content.ContextCompat; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; @@ -197,10 +199,19 @@ private void setProgressSelection(int position) { * of the intro screen. * @param color Progress circle color to set. */ - protected void setProgressCircleColor(int color) { + protected void setProgressCircleColor(@ColorInt int color) { mProgressCircleColor = color; } + /** + * Set the color of the progress circles at the bottom + * of the intro screen. + * @param colorResId Progress circle color resource to set. + */ + protected void setProgressCircleColorRes(@ColorRes int colorResId) { + mProgressCircleColor = ContextCompat.getColor(this, colorResId); + } + /** * Use this method to initialize your intro screens * and set up various styling options. @@ -210,8 +221,9 @@ protected void setProgressCircleColor(int color) { /** * Add an intro screen (Fragment) to the ViewPager. * @param introScreen Fragment to add. + * @param backgroundColor The background color to apply */ - protected void addIntroScreen(BaseIntroFragment introScreen, int backgroundColor) { + protected void addIntroScreen(BaseIntroFragment introScreen, @ColorInt int backgroundColor) { mPagerAdapter.addFragment(introScreen, backgroundColor); } @@ -237,7 +249,16 @@ protected void setShowSkipButton(boolean showSkipButton) { * Set the text color of the Skip button. Default color is white (#F0F0F0). * @param color Text color to set. */ - protected void setSkipButtonTextColor(int color) { + protected void setSkipButtonTextColor(@ColorInt int color) { + mSkipButton.setTextColor(color); + } + + /** + * Set the text color of the Skip button. Default color is white (#F0F0F0). + * @param color Text color to set. + */ + protected void setSkipButtonTextColorRes(@ColorRes int colorResId) { + int color = ContextCompat.getColor(this, colorResId); mSkipButton.setTextColor(color); } @@ -246,7 +267,17 @@ protected void setSkipButtonTextColor(int color) { * will be 25 percent opaque. Default color is white (#FFFFFF). * @param color Background color for the Next / Done button. */ - protected void setNextButtonBackgroundColor(int color) { + protected void setNextButtonBackgroundColor(@ColorInt int color) { + mNextButton.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_IN); + } + + /** + * Set the background color of the Next / Done button. The selected color + * will be 25 percent opaque. Default color is white (#FFFFFF). + * @param colorResId Background color resource for the Next / Done button. + */ + protected void setNextButtonBackgroundColorRes(@ColorRes int colorResId) { + int color = ContextCompat.getColor(this, colorResId); mNextButton.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_IN); } @@ -255,10 +286,19 @@ protected void setNextButtonBackgroundColor(int color) { * Default color is white (#F0F0F0). * @param color */ - protected void setNextButtonIconColor(int color) { + protected void setNextButtonIconColor(@ColorInt int color) { mNextButton.setColor(color); } + /** + * Set the color of the arrow / done icon for the Next / Done button. + * Default color is white (#F0F0F0). + * @param colorResId + */ + protected void setNextButtonIconColorRes(@ColorRes int colorResId) { + mNextButton.setColorRes(colorResId); + } + /** * Callback when the "Skip" button is pressed. */ diff --git a/library/src/main/java/com/miz/introactivity/IntroFragment.java b/library/src/main/java/com/miz/introactivity/IntroFragment.java index 319e34e..d4b497c 100644 --- a/library/src/main/java/com/miz/introactivity/IntroFragment.java +++ b/library/src/main/java/com/miz/introactivity/IntroFragment.java @@ -1,6 +1,7 @@ package com.miz.introactivity; import android.os.Bundle; +import android.support.annotation.ColorInt; import android.support.v4.content.ContextCompat; /** @@ -61,8 +62,8 @@ public static IntroFragment newInstance(String title, String description, int re * @param descriptionColor Text color for the description. * @return IntroFragment with title, description and custom colors. */ - public static IntroFragment newInstance(String title, int titleColor, String description, - int descriptionColor) { + public static IntroFragment newInstance(String title, @ColorInt int titleColor, String description, + @ColorInt int descriptionColor) { Bundle args = new Bundle(); args.putString(TITLE, title); args.putInt(TITLE_COLOR, titleColor); @@ -85,8 +86,8 @@ public static IntroFragment newInstance(String title, int titleColor, String des * @param resourceIdType Type for the reference ID * @return */ - public static IntroFragment newInstance(String title, int titleColor, String description, - int descriptionColor, int resourceId, int resourceIdType) { + public static IntroFragment newInstance(String title, @ColorInt int titleColor, String description, + @ColorInt int descriptionColor, int resourceId, int resourceIdType) { Bundle args = new Bundle(); args.putString(TITLE, title); args.putInt(TITLE_COLOR, titleColor); diff --git a/library/src/main/java/com/miz/introactivity/IntroScreenPagerAdapter.java b/library/src/main/java/com/miz/introactivity/IntroScreenPagerAdapter.java index 7c57ddb..1124d3e 100644 --- a/library/src/main/java/com/miz/introactivity/IntroScreenPagerAdapter.java +++ b/library/src/main/java/com/miz/introactivity/IntroScreenPagerAdapter.java @@ -1,5 +1,6 @@ package com.miz.introactivity; +import android.support.annotation.ColorInt; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; @@ -24,7 +25,7 @@ public IntroScreenPagerAdapter(FragmentManager fm) { * @param fragment BaseIntroFragment to add. * @param backgroundColor Background color for the Fragment to add. */ - public void addFragment(BaseIntroFragment fragment, int backgroundColor) { + public void addFragment(BaseIntroFragment fragment, @ColorInt int backgroundColor) { mBackgroundColors.put(mFragments.size(), backgroundColor); mFragments.add(fragment); } @@ -47,4 +48,4 @@ public Fragment getItem(int position) { public int getCount() { return mFragments.size(); } -} \ No newline at end of file +} diff --git a/library/src/main/java/com/miz/introactivity/NextDoneButton.java b/library/src/main/java/com/miz/introactivity/NextDoneButton.java index a96d70e..3644a01 100644 --- a/library/src/main/java/com/miz/introactivity/NextDoneButton.java +++ b/library/src/main/java/com/miz/introactivity/NextDoneButton.java @@ -4,6 +4,8 @@ import android.content.Context; import android.graphics.drawable.Animatable; import android.graphics.drawable.Drawable; +import android.support.annotation.ColorInt; +import android.support.annotation.ColorRes; import android.support.v4.content.ContextCompat; import android.support.v4.graphics.drawable.DrawableCompat; import android.util.AttributeSet; @@ -72,11 +74,20 @@ private int getNextDrawable() { * Set the color of the button icon. * @param color Color for the button icon. */ - public void setColor(int color) { + public void setColor(@ColorInt int color) { mColor = color; tintDrawables(); } + /** + * Set the color of the button icon. + * @param colorResId Color resource for the button icon. + */ + public void setColorRes(@ColorRes int colorResId) { + mColor = ContextCompat.getColor(getContext(), colorResId); + tintDrawables(); + } + private void tintDrawables() { if (Utils.hasLollipop()) { mNextDrawable.setTint(mColor); From f65016544babeeb245ad896841e98024bdceaa96 Mon Sep 17 00:00:00 2001 From: Alexander Martinz Date: Wed, 4 May 2016 12:26:01 +0200 Subject: [PATCH 2/2] compare mProgressCircleColor with Integer.MIN instead of 0 * 0 is a valid color (Color.TRANSPARENT) so it might override the selected color with the default one * compare with Integer.MIN instead Change-Id: I62a250b7be7bcc38c72b9406ddbab62e90daf593 Signed-off-by: Alexander Martinz --- .../src/main/java/com/miz/introactivity/IntroActivity.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/src/main/java/com/miz/introactivity/IntroActivity.java b/library/src/main/java/com/miz/introactivity/IntroActivity.java index eed83f5..6cfce12 100644 --- a/library/src/main/java/com/miz/introactivity/IntroActivity.java +++ b/library/src/main/java/com/miz/introactivity/IntroActivity.java @@ -27,7 +27,7 @@ public abstract class IntroActivity extends AppCompatActivity { private IntroScreenPagerAdapter mPagerAdapter; private LinearLayout mProgressLayout; private boolean mShowSkipButton, mShowNextButton; - private int mProgressCircleColor; + private int mProgressCircleColor = Integer.MIN_VALUE; @Override protected final void onCreate(Bundle savedInstanceState) { @@ -185,7 +185,7 @@ private void setProgressSelection(int position) { R.drawable.progress_circle_selected : R.drawable.progress_circle); - if (mProgressCircleColor == 0) { + if (mProgressCircleColor == Integer.MIN_VALUE) { mProgressCircleColor = ContextCompat.getColor(this, R.color.progress_circle_color); }