How to add tabHost to Subactivity in Android?

I want to add TabHost to SubActivity.

eg. I have an Activity called MainActivity that represents a TabHost. I have five tabs in my tab. in each Host tab I open Activity A, B, C, D, E.

 Now when I want move from Activity A to some different activity e.g Z.
 then I am not able to add TabHost into Z. It totally disappeared from Activity Z.

 So Is there any solution for this issue ? Please help me.

      

Here is the source for MainActivity:

public class MainActivity extends TabActivity {

    public static final String TAG_1 = "tab1";
    public static final String TAG_2 = "tab2";
    public static final String TAG_3 = "tab3";
    public static final String TAG_4 = "tab4";
    public static final String TAG_5 = "tab5";

    public TabHost mTabHost;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mTabHost = getTabHost();
        setTabs();
    }

    public void setTabs() {
        mTabHost = (TabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup();

        addTab("Home", TAG_1, createTabDrawable(R.drawable.home),  FeedBack.class);
        addTab("Near Me", TAG_2, createTabDrawable(R.drawable.search), NearMe.class);
        addTab("Share", TAG_3, createTabDrawable(R.drawable.star),Home.class);
        addTab("FeedBack", TAG_4, createTabDrawable(R.drawable.settings),FeedBack.class);
        addTab("Options", TAG_5, createTabDrawable(R.drawable.settings),NearMe.class);


    }

    public Drawable createTabDrawable(int resId) {
        Resources res = getResources();
        StateListDrawable states = new StateListDrawable();

        final Options options = new Options();
        options.inPreferredConfig = Config.ARGB_8888;

        Bitmap icon = BitmapFactory.decodeResource(res, resId, options);

        Bitmap unselected = TabBitmap.createUnselectedBitmap(res, icon);
        Bitmap selected = TabBitmap.createSelectedBitmap(res, icon);

        icon.recycle();

        states.addState(new int[] { android.R.attr.state_selected }, new BitmapDrawable(res, selected));
        states.addState(new int[] { android.R.attr.state_enabled }, new BitmapDrawable(res, unselected));

        return states;
    }

    public View createTabIndicator(String label, Drawable drawable) {
        View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, mTabHost.getTabWidget(), false);

        TextView txtTitle = (TextView) tabIndicator.findViewById(R.id.text_view_tab_title);
        txtTitle.setText(label);
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) txtTitle.getLayoutParams();
        txtTitle.setLayoutParams(params);

        ImageView imgIcon = (ImageView) tabIndicator.findViewById(R.id.image_view_tab_icon);
        imgIcon.setImageDrawable(drawable);

        return tabIndicator;
    }

    public void addTab(String label, String tag, Drawable drawable, Class<?> c) {
        Intent intent = new Intent(this, c);
        TabHost.TabSpec spec = mTabHost.newTabSpec(tag);
        spec.setIndicator(createTabIndicator(label, drawable));
        spec.setContent(intent);

        mTabHost.addTab(spec);
    }
}

      

+3


source to share


2 answers


Ok Pravin, for the task you are looking for an ActivityGroup can be used

go through this link, hope you find your answer.



http://www.gamma-point.com/content/android-how-have-multiple-activities-under-single-tab-tabactivity

+1


source


Try it, it works for me .. in your main activity onCreate ()

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home);

    etSearch = (EditText)findViewById(R.id.etSearch);

    TabHost tabhost = getTabHost();

    TabSpec first = tabhost.newTabSpec("All Submissions");        
    first.setIndicator("All Submissions"     ,getResources().getDrawable(R.drawable.submission));
    Intent intent1 = new Intent(this, AllSubmissionActivity.class);
    first.setContent(intent1);
    //tabhost.addTab(first);

    TabSpec second = tabhost.newTabSpec("All Districts");        
    second.setIndicator("All Districts"  ,getResources().getDrawable(R.drawable.district));
    Intent intent2 = new Intent(this, AllDistrictActivity.class);
    second.setContent(intent2);
    //tabhost.addTab(second);

    TabSpec third = tabhost.newTabSpec("All Schools");
    third.setIndicator("All Schools" ,getResources().getDrawable(R.drawable.school));
    Intent intent3 = new Intent(this, AllSchoolActivity.class);
    third.setContent(intent3);
    //tabhost.addTab(third);  

    if(DataContainer.UserCredentials.uname.equals("sadmin"))
    {
        tabhost.addTab(first);
        tabhost.addTab(second);
        tabhost.addTab(third); 
    }
    else if(DataContainer.UserCredentials.uname.equals("b"))
    {
        tabhost.addTab(second);
        tabhost.addTab(third); 
    }
    else
    {
        tabhost.addTab(third); 
    }


    tabhost.setCurrentTab(0); 

}

      

you need to follow the same procedure to add tabs to your activity as basically ...

in ancillary activity



protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.allsubmission);

    initializeTabHost();

}

      

and this will initialize the TabHoast () method.

public void initializeTabHost(){



    tabhost = getTabHost();
    TabSpec first = tabhost.newTabSpec("Details"); 

    first.setIndicator("Details" ,null);
    Intent intent1 = new Intent(this, DetailsCrimeOnLink.class);

    intent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);


    first.setContent(intent1);
    tabhost.addTab(first);

    TabSpec second = tabhost.newTabSpec("Maps");        
    second.setIndicator("Maps" ,null);
    Intent intent2 = new Intent(this, MapsCrimeOnLink.class);
    second.setContent(intent2);
    tabhost.addTab(second);

    TabSpec third = tabhost.newTabSpec("Notes");
    third.setIndicator("Notes" ,null);
    Intent intent3 = new Intent(this, NotesCrimeOnLink.class);
    third.setContent(intent3);
    tabhost.addTab(third); 

    TabSpec fourth = tabhost.newTabSpec("History");
    fourth.setIndicator("History" ,null);
    Intent intent4 = new Intent(this, HistoryCrimOnLink.class);
    fourth.setContent(intent4);
    tabhost.addTab(fourth);  

    tabhost.setCurrentTab(0);

            }

      

0


source







All Articles