Question

I am developing an application which uses the tab bar. I followed This Link. I made some changes according to application as: My tabs text means title is coming from SQLite data base which i am storing in a array and passing to TabSpec in loop.

The code is as follows :

DataBaseHelper dataBaseHelper = new DataBaseHelper(this);
    try {
    dataBaseHelper.createDataBase();
} catch (IOException e) {
    e.printStackTrace();
}

    dataBaseHelper.openDataBase();
Cursor c = dataBaseHelper.getDataFromDataBase();
String[] name = new String[c.getCount()];
int i=0;

if(c.getCount() > 0){
    if(c.moveToFirst()){
        do{
            name[i] = c.getString(0);
            i++;
        } while(c.moveToNext());
    }
}

TabHost tabHost = getTabHost();

for(int j = 0; j < name.length; j++){
    String tabTitle = name[j];
    TabSpec photospec = tabHost.newTabSpec(tabTitle);
    photospec.setIndicator(tabTitle, null);
    Intent photosIntent = new Intent (this, PhotosActivity. class). addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    photosIntent.putExtra("name", tabTitle);
    photospec.setContent(photosIntent);

    tabHost.addTab(photospec);
}

This code is running fine. But now want to set backgroungd to my tabs. So for that i put some inmages n drawable folder and made an array as :

Integer[] imageIDsTemp= new Integer[]{
        R.drawable.tb01,
        R.drawable.tb02,
        R.drawable.tb03,
        R.drawable.tb04,
        R.drawable.tb05,
        R.drawable.tb06,
        R.drawable.tb07,
        R.drawable.tb08,
        R.drawable.tb09,
        R.drawable.tb10,
        R.drawable.tb11,
        R.drawable.tb12,
};

But how to pass this array to

TabSpec photospec = tabHost.newTabSpec(tabTitle);

I am getting eerror while doing this. What i tried is :

TabSpec photospec = tabHost.newTabSpec(tabTitle);
photospec.setIndicator(tabTitle, imageIDsTemp[j]);

Please guide me how to assign imahges and color to tabs.

Was it helpful?

Solution

Change this line

photospec.setIndicator(tabTitle, imageIDsTemp[j]);

to

photospec.setIndicator(tabTitle, getResources().getDrawable(imageIDsTemp[j]));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top