Commit 3d855539 authored by Yusuke Iwaki's avatar Yusuke Iwaki

fix type: budge -> badge; modify badge size with no text

parent e54692a4
...@@ -2,13 +2,13 @@ package chat.rocket.android.widget; ...@@ -2,13 +2,13 @@ package chat.rocket.android.widget;
import android.content.Context; import android.content.Context;
import android.content.res.TypedArray; import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Typeface; import android.graphics.Typeface;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes; import android.support.annotation.DrawableRes;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.annotation.StringRes; import android.support.annotation.StringRes;
import android.support.graphics.drawable.VectorDrawableCompat; import android.support.graphics.drawable.VectorDrawableCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.TextViewCompat; import android.support.v4.widget.TextViewCompat;
import android.support.v7.widget.AppCompatImageView; import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
...@@ -24,7 +24,7 @@ import java.lang.reflect.Field; ...@@ -24,7 +24,7 @@ import java.lang.reflect.Field;
public class RoomToolbar extends Toolbar { public class RoomToolbar extends Toolbar {
private TextView titleTextView; private TextView titleTextView;
private ImageView budgeImageView; private ImageView badgeImageView;
public RoomToolbar(Context context) { public RoomToolbar(Context context) {
super(context); super(context);
...@@ -110,30 +110,35 @@ public class RoomToolbar extends Toolbar { ...@@ -110,30 +110,35 @@ public class RoomToolbar extends Toolbar {
return; return;
} }
if (budgeImageView == null) { if (badgeImageView == null) {
budgeImageView = new AppCompatImageView(getContext()); badgeImageView = new AppCompatImageView(getContext());
badgeImageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
} }
if (budgeImageView.getParent() != this) { //ref: Toolbar#isChildOrHidden if (badgeImageView.getParent() != this) { //ref: Toolbar#isChildOrHidden
addView(budgeImageView, generateDefaultLayoutParams()); addView(badgeImageView, generateDefaultLayoutParams());
} }
if (numUnreadChannels > 0) { if (numUnreadChannels > 0) {
budgeImageView.setImageDrawable(getBudgeDrawable(numMentionsSum)); if (numMentionsSum > 0) {
budgeImageView.setVisibility(View.VISIBLE); badgeImageView.setImageDrawable(getBadgeDrawable(numMentionsSum));
} else {
badgeImageView.setScaleType(ImageView.ScaleType.CENTER);
badgeImageView.setImageResource(R.drawable.badge_without_number);
}
badgeImageView.setVisibility(View.VISIBLE);
} else { } else {
budgeImageView.setVisibility(View.GONE); badgeImageView.setVisibility(View.GONE);
} }
} }
private Drawable getBudgeDrawable(int number) { private Drawable getBadgeDrawable(int number) {
String icon = number > 99 ? "99+" : String icon = number > 99 ? "99+" : Integer.toString(number);
(number <= 0 ? "" : Integer.toString(number));
return TextDrawable.builder() return TextDrawable.builder()
.beginConfig() .beginConfig()
.useFont(Typeface.SANS_SERIF) .useFont(Typeface.SANS_SERIF)
.endConfig() .endConfig()
.buildRound(icon, Color.RED); .buildRound(icon, ContextCompat.getColor(getContext(), R.color.badge_color));
} }
...@@ -142,7 +147,7 @@ public class RoomToolbar extends Toolbar { ...@@ -142,7 +147,7 @@ public class RoomToolbar extends Toolbar {
protected void onLayout(boolean changed, int left, int top, int right, int bottom) { protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom); super.onLayout(changed, left, top, right, bottom);
if (budgeImageView != null && budgeImageView.getVisibility() != View.GONE) { if (badgeImageView != null && badgeImageView.getVisibility() != View.GONE) {
try { try {
Field field = Toolbar.class.getDeclaredField("mNavButtonView"); Field field = Toolbar.class.getDeclaredField("mNavButtonView");
field.setAccessible(true); field.setAccessible(true);
...@@ -156,7 +161,7 @@ public class RoomToolbar extends Toolbar { ...@@ -156,7 +161,7 @@ public class RoomToolbar extends Toolbar {
int budgeRight = iconLeft + (iconRight - iconLeft) * 7 / 8; int budgeRight = iconLeft + (iconRight - iconLeft) * 7 / 8;
int budgeTop = iconTop + (iconBottom - iconTop) / 8; int budgeTop = iconTop + (iconBottom - iconTop) / 8;
int budgeBottom = iconTop + (iconBottom - iconTop) * 3 / 8; int budgeBottom = iconTop + (iconBottom - iconTop) * 3 / 8;
budgeImageView.layout(budgeLeft, budgeTop, budgeRight, budgeBottom); badgeImageView.layout(budgeLeft, budgeTop, budgeRight, budgeBottom);
} catch (Exception exception) { } catch (Exception exception) {
return; return;
} }
......
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@color/badge_color" />
<size android:width="@dimen/badge_size" android:height="@dimen/badge_size"/>
</shape>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="badge_color">#FFFF0000</color>
<dimen name="badge_size">8dp</dimen>
</resources>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment