Skip to content

Commit

Permalink
Support setting font size per tab
Browse files Browse the repository at this point in the history
  • Loading branch information
guyca committed Jul 9, 2018
1 parent 95a2779 commit 519e24f
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ public enum TitleState {

private ArrayList<Integer> coloredTitleColorActive = new ArrayList<>(MAX_ITEMS);
private ArrayList<Integer> coloredTitleColorInactive = new ArrayList<>(MAX_ITEMS);
private float titleActiveTextSize, titleInactiveTextSize;
private ArrayList<Float> titleActiveTextSize = new ArrayList<>(MAX_ITEMS);
private ArrayList<Float> titleInactiveTextSize = new ArrayList<>(MAX_ITEMS);
private int bottomNavigationHeight, navigationBarHeight = 0;
private float selectedItemWidth, notSelectedItemWidth;
private boolean forceTint = false;
Expand Down Expand Up @@ -209,6 +210,8 @@ private void init(Context context, AttributeSet attrs) {
fill(titleDisableColor, MAX_ITEMS, null);

fill(titleTypeface, MAX_ITEMS, null);
fill(titleActiveTextSize, MAX_ITEMS, null);
fill(titleInactiveTextSize, MAX_ITEMS, null);

// Colors for colored bottom navigation
fill(coloredTitleColorActive, MAX_ITEMS, ContextCompat.getColor(context, R.color.colorBottomNavigationActiveColored));
Expand Down Expand Up @@ -382,18 +385,8 @@ private void createClassicItems(LinearLayout linearLayout) {
itemWidth = maxWidth;
}

float activeSize = resources.getDimension(R.dimen.bottom_navigation_text_size_active);
float inactiveSize = resources.getDimension(R.dimen.bottom_navigation_text_size_inactive);
int activePaddingTop = (int) resources.getDimension(R.dimen.bottom_navigation_margin_top_active);

if (titleActiveTextSize != 0 && titleInactiveTextSize != 0) {
activeSize = titleActiveTextSize;
inactiveSize = titleInactiveTextSize;
} else if (titleState == TitleState.ALWAYS_SHOW && items.size() > MIN_ITEMS) {
activeSize = resources.getDimension(R.dimen.bottom_navigation_text_size_forced_active);
inactiveSize = resources.getDimension(R.dimen.bottom_navigation_text_size_forced_inactive);
}

for (int i = 0; i < items.size(); i++) {
final boolean current = currentItem == i;
final int itemIndex = i;
Expand Down Expand Up @@ -448,7 +441,7 @@ private void createClassicItems(LinearLayout linearLayout) {
}
}

title.setTextSize(TypedValue.COMPLEX_UNIT_PX, current ? activeSize : inactiveSize);
title.setTextSize(TypedValue.COMPLEX_UNIT_PX, current ? getActiveTextSize(i) : getInactiveTextSize(i));

if (itemsEnabledStates[i]) {
view.setOnClickListener(v -> updateItems(itemIndex, true));
Expand All @@ -468,6 +461,24 @@ private void createClassicItems(LinearLayout linearLayout) {
updateNotifications(true, UPDATE_ALL_NOTIFICATIONS);
}

private float getInactiveTextSize(int index) {
if (titleInactiveTextSize.get(index) != null) return titleInactiveTextSize.get(index);
if (titleState == TitleState.ALWAYS_SHOW && items.size() > MIN_ITEMS) {
return resources.getDimension(R.dimen.bottom_navigation_text_size_forced_inactive);
} else {
return resources.getDimension(R.dimen.bottom_navigation_text_size_inactive);
}
}

private float getActiveTextSize(int index) {
if (titleActiveTextSize.get(index) != null) return titleActiveTextSize.get(index).floatValue();
if (titleState == TitleState.ALWAYS_SHOW && items.size() > MIN_ITEMS) {
return resources.getDimension(R.dimen.bottom_navigation_text_size_forced_active);
} else {
return resources.getDimension(R.dimen.bottom_navigation_text_size_active);
}
}

/**
* Create small items (more than 3 items in the bottom navigation)
*
Expand Down Expand Up @@ -517,8 +528,9 @@ private void createSmallItems(LinearLayout linearLayout) {
title.setText(item.getTitle(context));
}

if (titleActiveTextSize != 0) {
title.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleActiveTextSize);
float activeTextSize = getActiveTextSize(i);
if (activeTextSize != 0) {
title.setTextSize(TypedValue.COMPLEX_UNIT_PX, activeTextSize);
}

title.setTypeface(titleTypeface.get(i));
Expand Down Expand Up @@ -614,16 +626,6 @@ private void updateItems(final int itemIndex, boolean useCallback) {

int activeMarginTop = (int) resources.getDimension(R.dimen.bottom_navigation_margin_top_active);
int inactiveMarginTop = (int) resources.getDimension(R.dimen.bottom_navigation_margin_top_inactive);
float activeSize = resources.getDimension(R.dimen.bottom_navigation_text_size_active);
float inactiveSize = resources.getDimension(R.dimen.bottom_navigation_text_size_inactive);

if (titleActiveTextSize != 0 && titleInactiveTextSize != 0) {
activeSize = titleActiveTextSize;
inactiveSize = titleInactiveTextSize;
} else if (titleState == TitleState.ALWAYS_SHOW && items.size() > MIN_ITEMS) {
activeSize = resources.getDimension(R.dimen.bottom_navigation_text_size_forced_active);
inactiveSize = resources.getDimension(R.dimen.bottom_navigation_text_size_forced_inactive);
}

for (int i = 0; i < views.size(); i++) {

Expand All @@ -641,7 +643,7 @@ private void updateItems(final int itemIndex, boolean useCallback) {
AHHelper.updateTopMargin(icon, inactiveMarginTop, activeMarginTop);
AHHelper.updateLeftMargin(notification, notificationInactiveMarginLeft, notificationActiveMarginLeft);
AHHelper.updateTextColor(title, titleInactiveColor.get(i), titleActiveColor.get(i));
AHHelper.updateTextSize(title, inactiveSize, activeSize);
AHHelper.updateTextSize(title, getInactiveTextSize(i), getActiveTextSize(i));
AHHelper.updateDrawableColor(items.get(itemIndex).getDrawable(context), icon, iconInactiveColor.get(i), iconActiveColor.get(i), forceTint);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && colored) {
Expand Down Expand Up @@ -699,7 +701,7 @@ public void onAnimationRepeat(Animator animation) {
AHHelper.updateTopMargin(icon, activeMarginTop, inactiveMarginTop);
AHHelper.updateLeftMargin(notification, notificationActiveMarginLeft, notificationInactiveMarginLeft);
AHHelper.updateTextColor(title, titleActiveColor.get(i), titleInactiveColor.get(i));
AHHelper.updateTextSize(title, activeSize, inactiveSize);
AHHelper.updateTextSize(title, getActiveTextSize(i), getInactiveTextSize(i));
AHHelper.updateDrawableColor(items.get(currentItem).getDrawable(context), icon, iconActiveColor.get(i), iconInactiveColor.get(i), forceTint);
}
}
Expand Down Expand Up @@ -1136,26 +1138,43 @@ public void setTitleTypeface(int index, @Nullable Typeface typeface) {
}

/**
* Set title text size in pixels
* Set title active text size in pixels
*/
public void setTitleTextSize(float activeSize, float inactiveSize) {
this.titleActiveTextSize = activeSize;
this.titleInactiveTextSize = inactiveSize;
public void setTitleActiveTextSize(int index, float activeSize) {
if (titleActiveTextSize.get(index) != null && titleActiveTextSize.get(index) == activeSize) return;
titleActiveTextSize.set(index, activeSize);
createItems();
}

/**
* Set title inactive text size in pixels
*/
public void setTitleInactiveTextSize(int index, float inactiveSize) {
if (titleInactiveTextSize.get(index) != null && titleInactiveTextSize.get(index) == inactiveSize) return;
titleInactiveTextSize.set(index, inactiveSize);
createItems();
}

/**
* Set title text size in SP
*
+ * @param activeSize in sp
+ * @param inactiveSize in sp
* @param activeSize in sp
*/
public void setTitleTextSizeInSp(float activeSize, float inactiveSize) {
this.titleActiveTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, activeSize, resources.getDisplayMetrics());
this.titleInactiveTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, inactiveSize, resources.getDisplayMetrics());
public void setTitleActiveTextSizeInSp(int index, float activeSize) {
this.titleActiveTextSize.set(index, (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, activeSize, resources.getDisplayMetrics())));
createItems();
}

/**
* Set title text size in SP
*
* @param inactiveSize in sp
*/
public void setTitleInactiveTextSizeInSp(int index, float inactiveSize) {
this.titleInactiveTextSize.set(index, (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, inactiveSize, resources.getDisplayMetrics())));
createItems();
}

/**
* Get item at the given index
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,4 +323,9 @@ public void tab3SelectedIconColor(View view) {
// bottomNavigation.setTitleActiveColor(2, Color.parseColor("#6a1b9a"));
// bottomNavigation.setTitleInactiveColor(2, Color.parseColor("#6a1b9a"));
}

public void setTab3FontSize(View view) {
bottomNavigation.setTitleActiveTextSizeInSp(2, 19);
bottomNavigation.setTitleInactiveTextSizeInSp(2, 16);
}
}
7 changes: 6 additions & 1 deletion demo/src/main/res/layout/fragment_demo_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@
android:layout_height="wrap_content"
android:text="@string/tab3_selected_icon_color"
android:onClick="tab3SelectedIconColor"/>
<Button
android:id="@+id/fontSize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/fontSize"
android:onClick="setTab3FontSize"/>
</LinearLayout>

</LinearLayout>
1 change: 1 addition & 0 deletions demo/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
<string name="tab1_custom_colors">Tab1 custom colors</string>
<string name="tab2_different_colors">Tab2 different colors</string>
<string name="tab3_selected_icon_color">Tab3 colors</string>
<string name="fontSize">Font Size</string>

</resources>

0 comments on commit 519e24f

Please sign in to comment.