Question

I am developing an application in which I want to use tab bar. For that I followed This link. The code is running good. But in that code the tabs are predefined.

In my application the need is: I want to generate tabs at run time as depends on data coming from a SQLite database. As the number of tabs, tab widget text and data of the tab are coming from the database.

Edit: My code which I tried is:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    DataBaseHelper dataBaseHelper = new DataBaseHelper(this);
      try {
          dataBaseHelper.createDataBase();
      } catch (IOException e) {
          // TODO Auto-generated catch block
          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++){
        TabSpec tab = tabHost.newTabSpec(name[j]);
        Intent intent = new Intent(this, SecondActivity.class);
        tab.setContent(intent);
        tabHost.addTab(tab);
    }

But I am getting error as :

03-03 13:49:55.400: I/Process(26507): Sending signal. PID: 26507 SIG: 9
03-03 13:50:02.415: D/dalvikvm(26665): GC_EXTERNAL_ALLOC freed 46K, 53% free 2542K/5379K, external 0K/0K, paused 144ms
03-03 13:50:02.505: D/AndroidRuntime(26665): Shutting down VM
03-03 13:50:02.505: W/dalvikvm(26665): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
03-03 13:50:02.510: E/AndroidRuntime(26665): FATAL EXCEPTION: main
03-03 13:50:02.510: E/AndroidRuntime(26665): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidtablayout/com.example.androidtablayout.AndroidTabLayoutActivity}: java.lang.IllegalArgumentException: you must specify a way to create the tab indicator.
03-03 13:50:02.510: E/AndroidRuntime(26665):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
03-03 13:50:02.510: E/AndroidRuntime(26665):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
03-03 13:50:02.510: E/AndroidRuntime(26665):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-03 13:50:02.510: E/AndroidRuntime(26665):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
03-03 13:50:02.510: E/AndroidRuntime(26665):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-03 13:50:02.510: E/AndroidRuntime(26665):    at android.os.Looper.loop(Looper.java:123)
03-03 13:50:02.510: E/AndroidRuntime(26665):    at android.app.ActivityThread.main(ActivityThread.java:3691)
03-03 13:50:02.510: E/AndroidRuntime(26665):    at java.lang.reflect.Method.invokeNative(Native Method)
03-03 13:50:02.510: E/AndroidRuntime(26665):    at java.lang.reflect.Method.invoke(Method.java:507)
03-03 13:50:02.510: E/AndroidRuntime(26665):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
03-03 13:50:02.510: E/AndroidRuntime(26665):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
03-03 13:50:02.510: E/AndroidRuntime(26665):    at dalvik.system.NativeStart.main(Native Method)
03-03 13:50:02.510: E/AndroidRuntime(26665): Caused by: java.lang.IllegalArgumentException: you must specify a way to create the tab indicator.
03-03 13:50:02.510: E/AndroidRuntime(26665):    at android.widget.TabHost.addTab(TabHost.java:206)
03-03 13:50:02.510: E/AndroidRuntime(26665):    at com.example.androidtablayout.AndroidTabLayoutActivity.onCreate(AndroidTabLayoutActivity.java:47)
03-03 13:50:02.510: E/AndroidRuntime(26665):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-03 13:50:02.510: E/AndroidRuntime(26665):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
03-03 13:50:02.510: E/AndroidRuntime(26665):    ... 11 more
03-03 13:55:06.845: I/Process(26665): Sending signal. PID: 26665 SIG: 9

Please guide me.

Was it helpful?

Solution

I did the same this in past. Follow the steps:

  1. First get all the Tab Setup data from DB.

  2. add Tab as per there Number which is coming from DB and in my requirement i want to show 4 Tab only. Create a loop like

    for (int j = 0; j < 4 && j < cursor.getCount(); j++) {
      addTab1(tab_title_from_DB, tab_image_from_DB,tab_fragement_class, 0, 1);
    }
    
  3. Now,in addTab1(...) i loaded my Custom_tab_indicator layout and bind the data and add tab into Tabhost like mTabHost.addTab(spec);

  4. At last step i set mTabHost.setCurrentTab(0); to select my First Tab

Update: you must set Tab Indicator like photospec.setIndicator("", ressources.getDrawable(R.drawable.icon_windows_config)) to your TabSpec

TabSpec is used to set Indicator, Content, Label, Icon, etc. on the particular tab. I have created TabSpec object which will initialized by newTabSpec method or TabHost class, which will have tag or title as parameter.

setIndicator will indicate TabHost that particular tab is selected or not. The file icon_windows_config in /res/drawable will indicate that tab is selected of not. This file will set background of tabs on the basis of that tab is selected of not.

You can to it for your all Tab.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top