Skip to content
This repository has been archived by the owner on Mar 8, 2020. It is now read-only.

Commit

Permalink
Added ability to change the text colours on the GridItemView.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Lord committed Apr 4, 2016
1 parent 8d1949a commit cc0cdaa
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.AttrRes;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.IntDef;
import android.support.annotation.LayoutRes;
Expand Down Expand Up @@ -70,6 +72,15 @@ public class GridItemView extends FrameLayout {
private CharSequence secondaryText;
private Drawable icon;

@ColorInt
private int primaryTextColor;

@ColorInt
private int secondaryTextColor;

private ColorStateList primaryTextColorStateList;
private ColorStateList secondaryTextColorStateList;

@GridItemVariant
private int variant;

Expand Down Expand Up @@ -167,9 +178,9 @@ private void loadVariant(TypedArray typedAttrs) {

private void loadText(TypedArray typedAttrs) {
String primaryText = typedAttrs.getString(R.styleable.MDGridItemView_md_grid_text_primary);
setTextPrimary(primaryText);
setPrimaryText(primaryText);
String secondaryText = typedAttrs.getString(R.styleable.MDGridItemView_md_grid_text_secondary);
setTextSecondary(secondaryText);
setSecondaryText(secondaryText);
}

private void loadDefaults() {
Expand Down Expand Up @@ -242,16 +253,36 @@ public void setVariant(@GridItemVariant int variant) {
}

private void setTexts() {
setTextPrimary(primaryText);
setTextSecondary(secondaryText);
setPrimaryText(primaryText);
setPrimaryTextColors();
setSecondaryText(secondaryText);
setSecondaryTextColors();
}

private void setPrimaryTextColors() {
if (primaryTextColorStateList != null) {
setPrimaryTextColor(primaryTextColorStateList);
}
if (primaryTextColor != 0) {
setPrimaryTextColor(primaryTextColor);
}
}

private void setSecondaryTextColors() {
if (secondaryTextColorStateList != null) {
setSecondaryTextColor(secondaryTextColorStateList);
}
if (secondaryTextColor != 0) {
setSecondaryTextColor(secondaryTextColor);
}
}

/**
* Get the primary text (line 1).
*
* @return The primary text.
*/
public CharSequence getTextPrimary() {
public CharSequence getPrimaryText() {
return primaryTextView.getText();
}

Expand All @@ -260,7 +291,7 @@ public CharSequence getTextPrimary() {
*
* @param text The primary text.
*/
public void setTextPrimary(CharSequence text) {
public void setPrimaryText(CharSequence text) {
primaryText = text;
primaryTextView.setText(text);
}
Expand All @@ -270,25 +301,64 @@ public void setTextPrimary(CharSequence text) {
*
* @param textRes The primary text.
*/
public void setTextPrimary(@StringRes int textRes) {
setTextPrimary(getContext().getString(textRes));
public void setPrimaryText(@StringRes int textRes) {
setPrimaryText(getContext().getString(textRes));
}

/**
* Get the primary text color state list (line 1).
*
* @return The primary text color state list.
*/
public ColorStateList getPrimaryTextColors() {
return primaryTextView.getTextColors();
}

/**
* Get the primary text color (line 1).
*
* @return The primary text color.
*/
@ColorInt
public int getCurrentPrimaryTextColor() {
return primaryTextView.getCurrentTextColor();
}

/**
* Set the primary text color (line 1).
*
* @param color The primary text color.
*/
public void setPrimaryTextColor(ColorStateList color) {
primaryTextColorStateList = color;
primaryTextView.setTextColor(color);
}

/**
* Set the primary text color (line 1).
*
* @param color The primary text color.
*/
public void setPrimaryTextColor(@ColorInt int color) {
primaryTextColor = color;
primaryTextView.setTextColor(color);
}

/**
* Get the primary text view, so you can customise it.
* Set the primary text color (line 1).
*
* @return The primary text view.
* @param colorRes The primary text color.
*/
public TextView getTextPrimaryView() {
return primaryTextView;
public void setPrimaryTextColorRes(@ColorRes int colorRes) {
setPrimaryTextColor(getColor(colorRes));
}

/**
* Get the secondary text (line 2).
*
* @return The secondary text.
*/
public CharSequence getTextSecondary() {
public CharSequence getSecondaryText() {
if (secondaryTextView != null) {
return secondaryTextView.getText();
}
Expand All @@ -300,7 +370,7 @@ public CharSequence getTextSecondary() {
*
* @param text The secondary text.
*/
public void setTextSecondary(CharSequence text) {
public void setSecondaryText(CharSequence text) {
secondaryText = text;
if (secondaryTextView != null) {
secondaryTextView.setText(text);
Expand All @@ -312,17 +382,66 @@ public void setTextSecondary(CharSequence text) {
*
* @param textRes The secondary text.
*/
public void setTextSecondary(@StringRes int textRes) {
setTextSecondary(getContext().getString(textRes));
public void setSecondaryText(@StringRes int textRes) {
setSecondaryText(getContext().getString(textRes));
}

/**
* Get the secondary text view, so you can customise it.
* Get the secondary text color state list (line 2).
*
* @return The secondary text view.
* @return The secondary text color state list.
*/
public TextView getTextSecondaryView() {
return secondaryTextView;
public ColorStateList getSecondaryTextColors() {
if (secondaryTextView != null) {
return secondaryTextView.getTextColors();
}
return null;
}

/**
* Get the secondary text color (line 2).
*
* @return The secondary text color.
*/
@ColorInt
public int getCurrentSecondaryTextColor() {
if (secondaryTextView != null) {
return secondaryTextView.getCurrentTextColor();
}
return 0;
}

/**
* Set the secondary text color (line 2).
*
* @param color The secondary text color.
*/
public void setSecondaryTextColor(ColorStateList color) {
if (secondaryTextView != null) {
secondaryTextColorStateList = color;
secondaryTextView.setTextColor(color);
}
}

/**
* Set the secondary text color (line 2).
*
* @param color The secondary text color.
*/
public void setSecondaryTextColor(@ColorInt int color) {
if (secondaryTextView != null) {
secondaryTextColor = color;
secondaryTextView.setTextColor(color);
}
}

/**
* Set the secondary text color (line 2).
*
* @param colorRes The secondary text color.
*/
public void setSecondaryTextColorRes(@ColorRes int colorRes) {
setSecondaryTextColor(getColor(colorRes));
}

/**
Expand Down Expand Up @@ -380,4 +499,8 @@ public void setIconGravity(@IconGravity int iconGravity) {
setVariant(variant);
}

private int getColor(@ColorRes int colorRes) {
return ContextCompat.getColor(getContext(), colorRes);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.github.andrewlord1990.materialandroid.component.grid.GridItemView;
import com.github.andrewlord1990.materialandroid.component.grid.GridItemView.GridItemVariant;
Expand Down Expand Up @@ -126,18 +125,11 @@ public GridViewHolder(View itemView, String primaryText) {

imageView = (ImageView) itemView.findViewById(R.id.image);
labelView = (GridItemView) itemView.findViewById(R.id.label);
labelView.setTextPrimary(primaryText);
labelView.getTextPrimaryView().setTextColor(getColor(R.color.md_light_primary_text_100));
labelView.setTextSecondary(R.string.sample_lists_secondary);
TextView secondaryTextView = labelView.getTextSecondaryView();
if (secondaryTextView != null) {
secondaryTextView.setTextColor(getColor(R.color.md_light_primary_text_100));
}
labelView.setPrimaryText(primaryText);
labelView.setPrimaryTextColorRes(R.color.md_light_primary_text_100);
labelView.setSecondaryText(R.string.sample_lists_secondary);
labelView.setSecondaryTextColorRes(R.color.md_light_primary_text_100);
labelView.setIcon(R.drawable.ic_star_white);
}

private int getColor(@ColorRes int colorRes) {
return ContextCompat.getColor(itemView.getContext(), colorRes);
}
}
}

0 comments on commit cc0cdaa

Please sign in to comment.