Skip to content

Commit

Permalink
feat(android): scale top tab style to fit icon
Browse files Browse the repository at this point in the history
  • Loading branch information
jquick-axway authored and sgtcoolguy committed Mar 16, 2021
1 parent cafe9d5 commit 8d2831d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.HashMap;

import ti.modules.titanium.ui.android.AndroidModule;
import ti.modules.titanium.ui.widget.tabgroup.TiUITabLayoutTabGroup;

public class TiUITabbedBar extends TiUIView implements MenuItem.OnMenuItemClickListener, TabLayout.OnTabSelectedListener
{
Expand Down Expand Up @@ -207,6 +208,7 @@ private void addItem(Object value)
if (value != null) {
if (value instanceof Drawable) {
tab.setIcon(((Drawable) value));
TiUITabLayoutTabGroup.scaleIconToFit(tab);
} else {
tab.setText(value.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

Expand Down Expand Up @@ -325,8 +326,9 @@ public void updateTabIcon(int index)
return;
}

Drawable drawable = TiUIHelper.getResourceDrawable(tabProxy.getProperty(TiC.PROPERTY_ICON));
this.mTabLayout.getTabAt(index).setIcon(drawable);
TabLayout.Tab tab = this.mTabLayout.getTabAt(index);
tab.setIcon(TiUIHelper.getResourceDrawable(tabProxy.getProperty(TiC.PROPERTY_ICON)));
scaleIconToFit(tab);
}

@Override
Expand Down Expand Up @@ -408,4 +410,19 @@ public void selectTab(int tabIndex)
updateIconTint();
updateTabBackgroundDrawable(tabIndex);
}

public static void scaleIconToFit(TabLayout.Tab tab)
{
if ((tab == null) || (tab.view == null)) {
return;
}

for (int childIndex = 0; childIndex < tab.view.getChildCount(); childIndex++) {
View childView = tab.view.getChildAt(childIndex);
if (childView instanceof ImageView) {
((ImageView) childView).setScaleType(ImageView.ScaleType.FIT_CENTER);
break;
}
}
}
}

0 comments on commit 8d2831d

Please sign in to comment.