Unable to use strings for tab names

361 views Asked by At

I'm following this tutorial and want to use string resources for the names of my tabs but apparently I can't do that in this tutorial. Does anyone know anyway around this?

    public class MainActivity extends ActionBarActivity {

    // Declaring Your View and Variables

    Toolbar toolbar;
    ViewPager pager;
    ViewPagerAdapter adapter;
    SlidingTabLayout tabs;
    CharSequence Titles[]={"Home","Events"};
    int Numboftabs =2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        // Creating The Toolbar and setting it as the Toolbar for the activity

        toolbar = (Toolbar) findViewById(R.id.tool_bar);
        setSupportActionBar(toolbar);


        // Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
        adapter =  new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs);

        // Assigning ViewPager View and setting the adapter
        pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(adapter);

        // Assiging the Sliding Tab Layout View
        tabs = (SlidingTabLayout) findViewById(R.id.tabs);
        tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width

        // Setting Custom Color for the Scroll bar indicator of the Tab View
        tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
            @Override
            public int getIndicatorColor(int position) {
                return getResources().getColor(R.color.tabsScrollColor);
            }
        });

        // Setting the ViewPager For the SlidingTabsLayout
        tabs.setViewPager(pager);
    }
}

Android Sliding Tabs with Material Design | Exoguru

2

There are 2 answers

5
Luiz On BEST ANSWER

For use String resources inside de FragmentStatePagerAdapter you need a Context, so you need to change the constructor of the class and pass a Context too.

public ViewPagerAdapter(FragmentManager fm, int mNumbOfTabsumb, Context context) {
        super(fm);
        this.NumbOfTabs = mNumbOfTabsumb;
        this.context = context;

    }

So in you getPageTitle, you don't need to return your Titles[position].

You can do something like this:

@Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case FRAGMENT_1:
                return context.getResources().getString(R.string.some_string);
        }
        return super.getPageTitle(position);
    }

Where FRAGMENT_1 is a constant of the class. If you don't use your Titles anymore, you can remove it from the constructor, because of this I removed.

0
Roel Strolenberg On

Put a text file in your assets folder which holds the titles you wish to use.

Get the file:

AssetManager assetManager = getAssets();
InputStream ims = assetManager.open("helloworld.txt");

Read the file using the InputStream (google it to get a template)

Whilst reading, do something like:

CharSequence Titles[] = new CharSequence[2];
int index = 0;
while(textFile.hasNext() && index < Titles.length){
    String lineInTextFile = textFile.next();
    Titles[index] = lineInTextFile;
    index++;
}