0

I'm trying to implement an ViewPager with the page title containing only an image/icon (Drawable).

I tried using J.W. ViewPagerIndicator, but it doesn't support icons only. The best I got is that I managed to display the icon, but is not centered(it is displayed on the left - like there is some text in the page title, but is not visible). I also tried to modify the library, but haven't managed to implement something that satisfies my needs for this (also I would prefer not to modify an library).

Can you guys recommend some other library or some good tutorial to implement custom ViewPager ?

PS: I'm trying to do something like the Twitter android application has.

Thank you.

LE:

After more searches and documentation I found that it can be achieved through reflection ... also I have found this method that works for now:

private void forceStackedTabs() {
    ActionBar ab = getSupportActionBar();
    if ( ab instanceof ActionBarImpl ) {
        // Pre-ICS
        disableEmbeddedTabs( ab );
    } else if ( ab instanceof ActionBarWrapper ) {
        // ICS
        try {
            Field abField = ab.getClass().getDeclaredField( "mActionBar" );
            abField.setAccessible( true );
            disableEmbeddedTabs( abField.get( ab ) );
        } catch (NoSuchFieldException e) {
            Log.e( TAG, "Error disabling actionbar embedded", e );
        } catch (IllegalArgumentException e) {
            Log.e( TAG, "Error disabling actionbar embedded", e );
        } catch (IllegalAccessException e) {
            Log.e( TAG, "Error disabling actionbar embedded", e );
        }
    }
}
private void disableEmbeddedTabs(Object ab) {
    try {
        Method setHasEmbeddedTabsMethod = ab.getClass().getDeclaredMethod("setHasEmbeddedTabs", boolean.class);
        setHasEmbeddedTabsMethod.setAccessible(true);
        setHasEmbeddedTabsMethod.invoke(ab, false);
    } catch (Exception e) {
        Log.e( TAG, "Error disabling actionbar embedded", e );
    }
}

This is the link where I found it: forceStackedTabs()

But I would prefer another method, because through reflection this code can break at any time!

Community
  • 1
  • 1
Ionut Negru
  • 6,186
  • 4
  • 48
  • 78

1 Answers1

1

use ActiconBar Sherlock library if you want twitter like application, link

Smogger
  • 553
  • 5
  • 17
  • You mean I should use ActionBar.NAVIGATION_MODE_TABS, and add tabs? And how should I implement icon's only for the tabs ? (the page indicator should still be there) ... Also, the link you gave uses Text for the tabs, that's not what I'm looking for ... please see my question again... – Ionut Negru Oct 14 '13 at 10:16
  • Inside mainActivity that example making tabs , you can set only icon without text using Tab1 = actionBar.newTab().setIcon(R.drawable.tab1); dont use settext("tab1") – Smogger Oct 14 '13 at 10:21
  • I see, and this will also have swipe capabilities like ViewPager has ? Or I should implement my gesture detector and change programmatically ? I was thinking about combining TabHost widget and ViewPager LayoutManager ... can this be done with the ActionBar ? – Ionut Negru Oct 14 '13 at 10:35
  • I have tried the example from here http://developer.android.com/training/implementing-navigation/lateral.html and added tabs like you recommended with setIcon() and it is what I'm looking for :) – Ionut Negru Oct 14 '13 at 10:55
  • I have encountered a problem with using ActionBar for the tabs ... When changing from portrait to landscape the tabs move to the same line as the title and it seems I can't lock the ActionBar into Stacked mode. What alternative do I have here in this case ? – Ionut Negru Oct 15 '13 at 13:10