Skip to content

Instantly share code, notes, and snippets.

@selmanon
Created November 21, 2014 22:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save selmanon/4bb4a5b0fc7ab5580d50 to your computer and use it in GitHub Desktop.
Save selmanon/4bb4a5b0fc7ab5580d50 to your computer and use it in GitHub Desktop.
Change the width of all the ActionBar Tabs
private boolean changeTabsWidth(Object root) {
// Found the container, that holds the Tabs. This is the ScrollContainerView to be specific,
// but checking against that class is not possible, since it's part of the hidden API.
// We will check, if root is an instance of HorizontalScrollView instead,
// since ScrollContainerView extends HorizontalScrollView.
if (root instanceof HorizontalScrollView) {
// The Tabs are all wraped in a LinearLayout
root = ((ViewGroup) root).getChildAt(0);
if (root instanceof LinearLayout) {
// Found the Tabs. Now we can set a custom Font to all of them.
for (int i = 0; i < ((ViewGroup) root).getChildCount(); i++) {
final LinearLayout child = ((LinearLayout) ((ViewGroup) root).getChildAt(i));
ViewTreeObserver viewTreeObserver = child.getViewTreeObserver();
if (viewTreeObserver != null) {
viewTreeObserver
.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
child.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
child.getViewTreeObserver() .removeGlobalOnLayoutListener(this);
}
int width = child.getMeasuredWidth();
int height = child.getMeasuredHeight();
// screen dimenssion
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
size.set(display.getWidth(),display.getHeight());
//display.getSize(size);
int widthScreen = size.x;
int heightScreen = size.y;
LinearLayout.LayoutParams parms = new
LinearLayout.LayoutParams(widthScreen/5, height);
child.setLayoutParams(parms);
}
});
}
}
return true;
}
}
// Search ActionBar and the Tabs. Exclude the content of the app from this search.
else if (root instanceof ViewGroup) {
ViewGroup group = (ViewGroup) root;
if (group.getId() != android.R.id.content) {
// Found a container that isn't the container holding our screen layout.
// The Tabs have to be in here.
for (int i = 0; i < group.getChildCount(); i++) {
if (changeTabsWidth(group.getChildAt(i))) {
// Found and done searching the View tree
return true;
}
}
}
}
// Found nothing
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment