Tab Customization Sneak Peek

Posted: March 29, 2010 in Android
Tags: ,

Left to fix is the drawable, fix my image states. and the text colors as the text label is taking the default text colors. I had to switch co around and replace the bottom strip first and than replace the tab indicator:

for (int i =0; i < tabWidget.getChildCount(); i++) {
        	/**
             * set height and width
             * under TabActivity
             * setting width has no effect
             * due to fill_parent
             * layout parameter
             *
             */
            tabWidget.getChildAt(i).getLayoutParams().height = height;
            tabWidget.getChildAt(i).getLayoutParams().width = width;

            /**
             * In TabWidget:
             * void setDrawBottomStrips(boolean drawBottomStrips) {
             * mDrawBottomStrips = drawBottomStrips;
             * }
             *
             * is not public thus we use reflection
             */
            try {
               mBottomLeftStrip = tabWidget.getClass().getDeclaredField ("mBottomLeftStrip");
               mBottomRightStrip = tabWidget.getClass().getDeclaredField ("mBottomRightStrip");
               if(!mBottomLeftStrip.isAccessible()) {
                    mBottomLeftStrip.setAccessible(true);
               }
                if(!mBottomRightStrip.isAccessible()){
                    mBottomRightStrip.setAccessible(true);
                }
               mBottomLeftStrip.set(tabWidget, getResources().getDrawable (R.drawable.tabs_one));
                mBottomRightStrip.set(tabWidget, getResources().getDrawable (R.drawable.tabs_one));
            } catch (Exception e) {
                e.printStackTrace();
            }
        	View vvv = tabWidget.getChildAt(i);
        	/**
        	 * I kept all drawables in selector so that the
        	 * we could get correct drawablea applied to
        	 * tabs as the selector pointed to has
        	 * both the tabs and the bottom tab-bar
        	 * drawables referenced
        	 */
        	vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_one_indicator));

        }
        
Advertisements
Comments
  1. Romain Guy says:

    Do NOT use reflection to do this. This is a terrible technique that is very likely to break in future release. FroYo for instance makes changes to the bottom strip and your code will probably not run. Private APIs are private, do NOT use them.

    (FroYo will give you setters for the bottom strip btw.)

    • Fred Grott says:

      I agree that it will break api changes which is why in the final version that particular code part is inside a if statement to check against the Build.VERSION string to make sure its not triggered past 2.1..

      I am glad that of the upcoming changes in Froyo and looking forward to doing it in a more sane way..:)

  2. Y Ramesh Rao says:

    This is a beautiful tip for changing the Tab Widget Tabs helped me a lot. Thanks for the Great Info/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s