Skip to content

Commit

Permalink
Set a meaningful accessibility label on tabs
Browse files Browse the repository at this point in the history
While this label isn't translated, it's better than nothing.
  • Loading branch information
guyca committed Jan 12, 2020
1 parent c914267 commit 54f76ab
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ protected void createItems() {
addView(linearLayoutContainer, layoutParams);

if (isClassic()) {
createClassicItems(linearLayoutContainer);
createClassicItems(linearLayoutContainer);
} else {
createSmallItems(linearLayoutContainer);
}
Expand Down Expand Up @@ -455,13 +455,12 @@ private void createClassicItems(LinearLayout linearLayout) {
title.setTextColor(titleDisableColor.get(i));
}

if (item.getTag() != null) {
view.setTag(item.getTag());
}
if (item.getTag() != null) view.setTag(item.getTag());

LayoutParams params = new LayoutParams((int) itemWidth, (int) height);
LayoutParams params = new LayoutParams((int) itemWidth, (int) height);
linearLayout.addView(view, params);
views.add(view);
setTabAccessibilityLabel(itemIndex, currentItem);
}

updateNotifications(true, UPDATE_ALL_NOTIFICATIONS);
Expand Down Expand Up @@ -520,6 +519,7 @@ private void createSmallItems(LinearLayout linearLayout) {

for (int i = 0; i < items.size(); i++) {
final int itemIndex = i;
final boolean current = currentItem == i;
AHBottomNavigationItem item = items.get(itemIndex);

View view = inflater.inflate(R.layout.bottom_navigation_small_item, this, false);
Expand All @@ -539,7 +539,7 @@ private void createSmallItems(LinearLayout linearLayout) {

title.setTypeface(titleTypeface.get(i));

if (i == currentItem) {
if (current) {
if (selectedBackgroundVisible) {
view.setSelected(true);
}
Expand Down Expand Up @@ -599,18 +599,28 @@ private void createSmallItems(LinearLayout linearLayout) {
width = (int) (itemWidth * 1.16);
}

if (item.getTag() != null) {
view.setTag(item.getTag());
}
if (item.getTag() != null) view.setTag(item.getTag());

LayoutParams params = new LayoutParams(width, (int) height);
linearLayout.addView(view, params);
views.add(view);
}
setTabAccessibilityLabel(itemIndex, currentItem);
}

updateNotifications(true, UPDATE_ALL_NOTIFICATIONS);
}

private void setTabAccessibilityLabel(int itemIndex, int currentItem) {
AHBottomNavigationItem item = items.get(itemIndex);
String contentDescription = currentItem == itemIndex ? "selected, " : "";
if (item.getTitle(context) != null) contentDescription += (item.getTitle(context) + ", ");
if (AHHelper.isInteger(notifications.get(itemIndex).getReadableText())) {
int num = Integer.parseInt(notifications.get(itemIndex).getReadableText());
contentDescription += (num + " new item" + (num == 1 ? "" : "s") + ", ");
}
contentDescription += "tab, " + (itemIndex + 1) + " out of " + getItemsCount();
views.get(itemIndex).setContentDescription(contentDescription);

This comment has been minimized.

Copy link
@asaharan

asaharan Jan 16, 2020

@guyca This line is causing index out of bounds exception.

This comment has been minimized.

Copy link
@guyca

guyca Jan 20, 2020

Author Collaborator

Thanks @asaharan! This should be fixed in latest version 👍

This comment has been minimized.

Copy link
@asaharan

asaharan Jan 21, 2020

Yes that fixes it. Thanks.

}

/**
* Update Items UI
Expand All @@ -619,7 +629,9 @@ private void createSmallItems(LinearLayout linearLayout) {
* @param useCallback boolean: Use or not the callback
*/
private void updateItems(final int itemIndex, boolean useCallback) {

for (int i = 0; i < items.size(); i++) {
setTabAccessibilityLabel(i, itemIndex);
}
if (currentItem == itemIndex) {
if (tabSelectedListener != null && useCallback) {
tabSelectedListener.onTabSelected(itemIndex, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public AHBottomNavigationItem(String title, Drawable icon, Drawable selectedIcon
this.selectedIcon = selectedIcon;
this.tag = tag;
}

/**
* Constructor
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Build;
import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;
import androidx.core.graphics.drawable.DrawableCompat;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
Expand All @@ -22,6 +19,10 @@

import java.util.ArrayList;

import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;
import androidx.core.graphics.drawable.DrawableCompat;

/**
*
*/
Expand Down Expand Up @@ -256,4 +257,15 @@ public static <T> void fill(ArrayList<T> al, int count, T value) {
public static boolean equals(@Nullable Object o1, @Nullable Object o2) {
return o1 == null && o2 == null || o1 != null && o1.equals(o2);
}

public static boolean isInteger(String s) {
try {
Integer.parseInt(s);
} catch(NumberFormatException e) {
return false;
} catch(NullPointerException e) {
return false;
}
return true;
}
}

0 comments on commit 54f76ab

Please sign in to comment.