Skip to content

Commit

Permalink
Implement dynamic tint widget
Browse files Browse the repository at this point in the history
  • Loading branch information
pranavpandey committed Apr 18, 2021
1 parent d7669fb commit 278df55
Show file tree
Hide file tree
Showing 14 changed files with 137 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public class Defaults {
* Default tint background value for the widgets.
* <p>{@code true} to enable the background tinting.
*/
public static final boolean ADS_TINT_BACKGROUND = false;
public static final boolean ADS_TINT_BACKGROUND = true;

/**
* Default value to show a divider below the widgets.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@
* <p>Extend this activity and implement the various methods according to the need.
*/
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public abstract class DynamicSystemActivity extends AppCompatActivity implements
DynamicLocale, DynamicListener, DynamicTransitionListener,
public abstract class DynamicSystemActivity extends AppCompatActivity
implements DynamicLocale, DynamicListener, DynamicTransitionListener,
SharedPreferences.OnSharedPreferenceChangeListener {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
* A {@link DynamicTutorialFragment} with an image, title, subtitle and description that
* will be used with the {@link DynamicTutorialActivity}.
*/
public class DynamicTutorialFragment extends DynamicFragment implements
Tutorial<DynamicTutorial, DynamicTutorialFragment> {
public class DynamicTutorialFragment extends DynamicFragment
implements Tutorial<DynamicTutorial, DynamicTutorialFragment> {

/**
* Fragment argument key to set the dynamic tutorial.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
/**
* An {@link MaterialButton} to apply {@link DynamicTheme} according to the supplied parameters.
*/
public class DynamicButton extends MaterialButton implements
DynamicWidget, DynamicCornerWidget<Integer>, DynamicTintWidget {
public class DynamicButton extends MaterialButton implements DynamicWidget,
DynamicCornerWidget<Integer>, DynamicTintWidget {

/**
* Color type applied to this view.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.pranavpandey.android.dynamic.support.Dynamic;
import com.pranavpandey.android.dynamic.support.R;
import com.pranavpandey.android.dynamic.support.theme.DynamicTheme;
import com.pranavpandey.android.dynamic.support.widget.base.DynamicTintWidget;
import com.pranavpandey.android.dynamic.support.widget.base.DynamicWidget;
import com.pranavpandey.android.dynamic.theme.Theme;
import com.pranavpandey.android.dynamic.utils.DynamicColorUtils;
Expand All @@ -39,7 +40,7 @@
/**
* A {@link FrameLayout} to apply {@link DynamicTheme} according to the supplied parameters.
*/
public class DynamicFrameLayout extends FrameLayout implements DynamicWidget {
public class DynamicFrameLayout extends FrameLayout implements DynamicWidget, DynamicTintWidget {

/**
* Color type applied to this view.
Expand Down Expand Up @@ -84,6 +85,11 @@ public class DynamicFrameLayout extends FrameLayout implements DynamicWidget {
*/
private @Theme.BackgroundAware int mBackgroundAware;

/**
* {@code true} to tint background according to the widget color.
*/
private boolean mTintBackground;

public DynamicFrameLayout(@NonNull Context context) {
this(context, null);
}
Expand Down Expand Up @@ -122,6 +128,9 @@ public void loadFromAttributes(@Nullable AttributeSet attrs) {
mBackgroundAware = a.getInteger(
R.styleable.DynamicFrameLayout_ads_backgroundAware,
Defaults.getBackgroundAware());
mTintBackground = a.getBoolean(
R.styleable.DynamicImageButton_ads_tintBackground,
Defaults.ADS_TINT_BACKGROUND);
} finally {
a.recycle();
}
Expand Down Expand Up @@ -252,6 +261,18 @@ public void setOnLongClickListener(@Nullable OnLongClickListener l) {
setColor();
}

@Override
public boolean isTintBackground() {
return mTintBackground;
}

@Override
public void setTintBackground(boolean tintBackground) {
this.mTintBackground = tintBackground;

setColor();
}

@Override
public void setColor() {
if (mColor != Theme.Color.UNKNOWN) {
Expand All @@ -263,7 +284,11 @@ public void setColor() {
DynamicDrawableUtils.setBackground(this, new ColorDrawable(mAppliedColor));
}

if (isBackgroundAware()) {
if (getBackground() != null) {
getBackground().clearColorFilter();
}

if (isBackgroundAware() && isTintBackground()) {
Dynamic.tintBackground(this, mContrastWithColor);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import com.pranavpandey.android.dynamic.support.R;
import com.pranavpandey.android.dynamic.support.theme.DynamicTheme;
import com.pranavpandey.android.dynamic.support.utils.DynamicResourceUtils;
import com.pranavpandey.android.dynamic.support.utils.DynamicTintUtils;
import com.pranavpandey.android.dynamic.support.widget.base.DynamicTintWidget;
import com.pranavpandey.android.dynamic.support.widget.base.DynamicWidget;
import com.pranavpandey.android.dynamic.theme.Theme;
Expand Down Expand Up @@ -300,12 +299,16 @@ public void setColor() {
mAppliedColor = DynamicColorUtils.getContrastColor(mColor, mContrastWithColor);
}

DynamicTintUtils.setViewBackgroundTint(this, mContrastWithColor,
mTintBackground ? mAppliedColor : DynamicColorUtils.getTintColor(
mContrastWithColor), mStyleBorderless, false);

setSupportImageTintList(DynamicResourceUtils.getColorStateList(
mAppliedColor, mAppliedColor, mAppliedColor, false));
}

if (getBackground() != null) {
getBackground().clearColorFilter();
}

if (isBackgroundAware() && isTintBackground()) {
Dynamic.tintBackground(this, mContrastWithColor);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.pranavpandey.android.dynamic.support.Dynamic;
import com.pranavpandey.android.dynamic.support.R;
import com.pranavpandey.android.dynamic.support.theme.DynamicTheme;
import com.pranavpandey.android.dynamic.support.widget.base.DynamicTintWidget;
import com.pranavpandey.android.dynamic.support.widget.base.DynamicWidget;
import com.pranavpandey.android.dynamic.theme.Theme;
import com.pranavpandey.android.dynamic.utils.DynamicColorUtils;
Expand All @@ -39,7 +40,8 @@
* An {@link AppCompatImageView} to apply {@link DynamicTheme} according to the supplied
* parameters.
*/
public class DynamicImageView extends AppCompatImageView implements DynamicWidget {
public class DynamicImageView extends AppCompatImageView
implements DynamicWidget, DynamicTintWidget {

/**
* Color type applied to this view.
Expand Down Expand Up @@ -84,6 +86,11 @@ public class DynamicImageView extends AppCompatImageView implements DynamicWidge
*/
private @Theme.BackgroundAware int mBackgroundAware;

/**
* {@code true} to tint background according to the widget color.
*/
private boolean mTintBackground;

public DynamicImageView(@NonNull Context context) {
this(context, null);
}
Expand Down Expand Up @@ -131,6 +138,9 @@ public void loadFromAttributes(@Nullable AttributeSet attrs) {
mBackgroundAware = a.getInteger(
R.styleable.DynamicImageView_ads_backgroundAware,
Defaults.getBackgroundAware());
mTintBackground = a.getBoolean(
R.styleable.DynamicImageButton_ads_tintBackground,
Defaults.ADS_TINT_BACKGROUND);

if (mColorType == Theme.ColorType.NONE && mColor == Theme.Color.UNKNOWN) {
if (getId() == R.id.submenuarrow) {
Expand Down Expand Up @@ -267,6 +277,18 @@ public void setOnLongClickListener(@Nullable OnLongClickListener l) {
setColor();
}

@Override
public boolean isTintBackground() {
return mTintBackground;
}

@Override
public void setTintBackground(boolean tintBackground) {
this.mTintBackground = tintBackground;

setColor();
}

@Override
public void setColor() {
if (mColor != Theme.Color.UNKNOWN) {
Expand All @@ -282,7 +304,11 @@ public void setColor() {
clearColorFilter();
}

if (isBackgroundAware()) {
if (getBackground() != null) {
getBackground().clearColorFilter();
}

if (isBackgroundAware() && isTintBackground()) {
Dynamic.tintBackground(this, mContrastWithColor);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.pranavpandey.android.dynamic.support.Dynamic;
import com.pranavpandey.android.dynamic.support.R;
import com.pranavpandey.android.dynamic.support.theme.DynamicTheme;
import com.pranavpandey.android.dynamic.support.widget.base.DynamicTintWidget;
import com.pranavpandey.android.dynamic.support.widget.base.DynamicWidget;
import com.pranavpandey.android.dynamic.theme.Theme;
import com.pranavpandey.android.dynamic.utils.DynamicColorUtils;
Expand All @@ -39,7 +40,7 @@
/**
* A {@link LinearLayout} to apply {@link DynamicTheme} according to the supplied parameters.
*/
public class DynamicLinearLayout extends LinearLayout implements DynamicWidget {
public class DynamicLinearLayout extends LinearLayout implements DynamicWidget, DynamicTintWidget {

/**
* Color type applied to this view.
Expand Down Expand Up @@ -84,6 +85,11 @@ public class DynamicLinearLayout extends LinearLayout implements DynamicWidget {
*/
private @Theme.BackgroundAware int mBackgroundAware;

/**
* {@code true} to tint background according to the widget color.
*/
private boolean mTintBackground;

public DynamicLinearLayout(@NonNull Context context) {
this(context, null);
}
Expand Down Expand Up @@ -122,6 +128,9 @@ public void loadFromAttributes(@Nullable AttributeSet attrs) {
mBackgroundAware = a.getInteger(
R.styleable.DynamicLinearLayout_ads_backgroundAware,
Defaults.getBackgroundAware());
mTintBackground = a.getBoolean(
R.styleable.DynamicImageButton_ads_tintBackground,
Defaults.ADS_TINT_BACKGROUND);
} finally {
a.recycle();
}
Expand Down Expand Up @@ -252,6 +261,18 @@ public void setOnLongClickListener(@Nullable OnLongClickListener l) {
setColor();
}

@Override
public boolean isTintBackground() {
return mTintBackground;
}

@Override
public void setTintBackground(boolean tintBackground) {
this.mTintBackground = tintBackground;

setColor();
}

@Override
public void setColor() {
if (mColor != Theme.Color.UNKNOWN) {
Expand All @@ -263,7 +284,11 @@ public void setColor() {
DynamicDrawableUtils.setBackground(this, new ColorDrawable(mAppliedColor));
}

if (isBackgroundAware()) {
if (getBackground() != null) {
getBackground().clearColorFilter();
}

if (isBackgroundAware() && isTintBackground()) {
Dynamic.tintBackground(this, mContrastWithColor);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
/**
* A {@link MaterialCardView} to apply {@link DynamicTheme} according to the supplied parameters.
*/
public class DynamicMaterialCardView extends MaterialCardView implements
DynamicWidget, DynamicCornerWidget<Float>, DynamicSurfaceWidget, DynamicFloatingWidget {
public class DynamicMaterialCardView extends MaterialCardView implements DynamicWidget,
DynamicCornerWidget<Float>, DynamicSurfaceWidget, DynamicFloatingWidget {

/**
* Color type applied to this view.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.pranavpandey.android.dynamic.support.Dynamic;
import com.pranavpandey.android.dynamic.support.R;
import com.pranavpandey.android.dynamic.support.theme.DynamicTheme;
import com.pranavpandey.android.dynamic.support.widget.base.DynamicTintWidget;
import com.pranavpandey.android.dynamic.support.widget.base.DynamicWidget;
import com.pranavpandey.android.dynamic.theme.Theme;
import com.pranavpandey.android.dynamic.utils.DynamicColorUtils;
Expand All @@ -39,7 +40,8 @@
/**
* A {@link RelativeLayout} to apply background color according to the supplied parameters.
*/
public class DynamicRelativeLayout extends RelativeLayout implements DynamicWidget {
public class DynamicRelativeLayout extends RelativeLayout
implements DynamicWidget, DynamicTintWidget {

/**
* Color type applied to this view.
Expand Down Expand Up @@ -84,6 +86,11 @@ public class DynamicRelativeLayout extends RelativeLayout implements DynamicWidg
*/
private @Theme.BackgroundAware int mBackgroundAware;

/**
* {@code true} to tint background according to the widget color.
*/
private boolean mTintBackground;

public DynamicRelativeLayout(@NonNull Context context) {
this(context, null);
}
Expand Down Expand Up @@ -122,6 +129,9 @@ public void loadFromAttributes(@Nullable AttributeSet attrs) {
mBackgroundAware = a.getInteger(
R.styleable.DynamicRelativeLayout_ads_backgroundAware,
Defaults.getBackgroundAware());
mTintBackground = a.getBoolean(
R.styleable.DynamicImageButton_ads_tintBackground,
Defaults.ADS_TINT_BACKGROUND);
} finally {
a.recycle();
}
Expand Down Expand Up @@ -252,6 +262,18 @@ public void setOnLongClickListener(@Nullable OnLongClickListener l) {
setColor();
}

@Override
public boolean isTintBackground() {
return mTintBackground;
}

@Override
public void setTintBackground(boolean tintBackground) {
this.mTintBackground = tintBackground;

setColor();
}

@Override
public void setColor() {
if (mColor != Theme.Color.UNKNOWN) {
Expand All @@ -263,7 +285,11 @@ public void setColor() {
DynamicDrawableUtils.setBackground(this, new ColorDrawable(mAppliedColor));
}

if (isBackgroundAware()) {
if (getBackground() != null) {
getBackground().clearColorFilter();
}

if (isBackgroundAware() && isTintBackground()) {
Dynamic.tintBackground(this, mContrastWithColor);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
/**
* A {@link TabLayout} to apply {@link DynamicTheme} according to the supplied parameters.
*/
public class DynamicTabLayout extends TabLayout implements
DynamicBackgroundWidget, DynamicTextWidget {
public class DynamicTabLayout extends TabLayout implements DynamicBackgroundWidget,
DynamicTextWidget {

/**
* Color type applied to this view.
Expand Down Expand Up @@ -363,11 +363,8 @@ public void setColor() {
>= Defaults.ADS_CORNER_MIN_TABS
? R.drawable.ads_tabs_indicator_corner
: R.drawable.ads_tabs_indicator);
if (getTabSelectedIndicator() != null) {
if (indicator != null) {
indicator.setBounds(getTabSelectedIndicator().getBounds());
} else {
indicator.setBounds(0, 0, 0,
Defaults.ADS_HEIGHT_TAB_SELECTED_PIXEL);
}

setSelectedTabIndicator(indicator);
Expand Down

0 comments on commit 278df55

Please sign in to comment.