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

Add IconImageView and IconImageButton #147

Open
wants to merge 1 commit into
base: develop
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
2 changes: 1 addition & 1 deletion android-iconify/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.joanzapata.android.iconify">
<manifest package="com.joanzapata.iconify">

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,14 @@ public IconDrawable alpha(int alpha) {
return this;
}

/**
* Returns the icon to be displayed
* @return The icon
*/
public final Icon getIcon() {
return icon;
}

@Override
public int getIntrinsicHeight() {
return size;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.joanzapata.iconify.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageButton;

import com.joanzapata.iconify.Icon;
import com.joanzapata.iconify.IconDrawable;
import com.joanzapata.iconify.R;

public class IconImageButton extends ImageButton {

private int color;

public IconImageButton(Context context) {
super(context);
}

public IconImageButton(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

public IconImageButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
final TypedArray a = context.obtainStyledAttributes(
attrs, R.styleable.IconImageView, defStyleAttr, 0);
color = a.getColor(R.styleable.IconImageView_iconColor, Color.BLACK);
String iconKey = a.getString(R.styleable.IconImageView_iconName);
if (iconKey != null) {
setImageDrawable(new IconDrawable(context, iconKey));
}
a.recycle();
}

public void setIcon(Icon icon) {
setImageDrawable(new IconDrawable(getContext(), icon));
}

public final Icon getIcon() {
Drawable drawable = getDrawable();
if (drawable instanceof IconDrawable) {
return ((IconDrawable) drawable).getIcon();
}
return null;
}

public void setIconColor(int color) {
this.color = color;
Drawable drawable = getDrawable();
if (drawable instanceof IconDrawable) {
((IconDrawable) drawable).color(color);
}
}

public void setIconColorResource(int colorResId) {
setIconColor(getContext().getResources().getColor(colorResId));
}

@Override
public void setImageDrawable(Drawable drawable) {
if (drawable instanceof IconDrawable && drawable != getDrawable()) {
((IconDrawable) drawable).color(color);
}
super.setImageDrawable(drawable);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.joanzapata.iconify.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.joanzapata.iconify.Icon;
import com.joanzapata.iconify.IconDrawable;
import com.joanzapata.iconify.R;

public class IconImageView extends ImageView {

private int color;

public IconImageView(Context context) {
super(context);
}

public IconImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

public IconImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
final TypedArray a = context.obtainStyledAttributes(
attrs, R.styleable.IconImageView, defStyleAttr, 0);
color = a.getColor(R.styleable.IconImageView_iconColor, Color.BLACK);
String iconKey = a.getString(R.styleable.IconImageView_iconName);
if (iconKey != null) {
setImageDrawable(new IconDrawable(context, iconKey));
}
a.recycle();
}

public void setIcon(Icon icon) {
setImageDrawable(new IconDrawable(getContext(), icon));
}

public final Icon getIcon() {
Drawable drawable = getDrawable();
if (drawable instanceof IconDrawable) {
return ((IconDrawable) drawable).getIcon();
}
return null;
}

public void setIconColor(int color) {
this.color = color;
Drawable drawable = getDrawable();
if (drawable instanceof IconDrawable) {
((IconDrawable) drawable).color(color);
}
}

public void setIconColorResource(int colorResId) {
setIconColor(getContext().getResources().getColor(colorResId));
}

@Override
public void setImageDrawable(Drawable drawable) {
if (drawable instanceof IconDrawable && drawable != getDrawable()) {
((IconDrawable) drawable).color(color);
}
super.setImageDrawable(drawable);
}

}
7 changes: 7 additions & 0 deletions android-iconify/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="IconImageView">
<attr name="iconName" format="string" />
<attr name="iconColor" format="color" />
</declare-styleable>
</resources>