Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Adding Tab inside Fragment

I am Trying to add a TabHost Inside a Fragment ...The Code is given bellow.Here inside the Fragment I am Trying to add TabHost to show two Tab

public class TablesFragment extends Fragment {

TabHost tabHost;
TabHost.TabSpec spec;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.tables_fragment, container,
            false);
    tabHost = (TabHost) rootView.findViewById(android.R.id.tabhost);
    tabHost.setup();

    Intent intentAndroid = new Intent().setClass(getActivity(),
            NewsFragment.class);
    spec = tabHost.newTabSpec("Android").setContent(intentAndroid)
            .setIndicator("Android");
    tabHost.addTab(spec);

    Intent intentBus = new Intent().setClass(getActivity(), NewsFragment.class);
    spec = tabHost.newTabSpec("Welcome").setIndicator("Welcome")
            .setContent(intentBus).setIndicator("Welcome");
    tabHost.addTab(spec);
    return rootView;
}

}

This Is Giving me Error...Need Assistant with this code...

LogCat:

01-26 16:21:27.092: E/AndroidRuntime(5597): FATAL EXCEPTION: main
01-26 16:21:27.092: E/AndroidRuntime(5597): java.lang.IllegalStateException: Did you    forget to call 'public void setup(LocalActivityManager activityGroup)'?
01-26 16:21:27.092: E/AndroidRuntime(5597):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:692)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at android.widget.TabHost.setCurrentTab(TabHost.java:358)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at android.widget.TabHost.addTab(TabHost.java:236)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at com.fifaworldcup2014.TablesFragment.onCreateView(TablesFragment.java:31)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at com.fifaworldcup2014.MainActivity.onTabChanged(MainActivity.java:164)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at android.widget.TabHost.invokeOnTabChangeListener(TabHost.java:391)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at android.widget.TabHost.setCurrentTab(TabHost.java:376)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:150)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:546)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at android.view.View.performClick(View.java:4084)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at android.view.View$PerformClick.run(View.java:16966)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at android.os.Handler.handleCallback(Handler.java:615)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at android.os.Looper.loop(Looper.java:137)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at android.app.ActivityThread.main(ActivityThread.java:4745)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at java.lang.reflect.Method.invokeNative(Native Method)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at java.lang.reflect.Method.invoke(Method.java:511)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at dalvik.system.NativeStart.main(Native Method)
like image 560
MKDenys Avatar asked Jan 26 '14 14:01

MKDenys


People also ask

How to add ViewPager in Android?

To use ViewPager and tabs, you need to add a dependency on ViewPager and on Material Components to your project. To insert child views that represent each page, you need to hook this layout to a PagerAdapter .

How to create Tab View in Android Studio?

Tabs are created using newTab() method of TabLayout class. The title and icon of Tabs are set through setText(int) and setIcon(int) methods of TabListener interface respectively. Tabs of layout are attached over TabLayout using the method addTab(Tab) method.

How to use TabLayout and ViewPager in Android?

The tabs for the TabLayout is created using the newTab() method. To display this tab over the layout, we need to add this tab using addTab(Tab) method. Using the methods setText(int) and setIcon(int) we set the title and icon of TabLayout respectively. We can also integrate the ViewPager with TabLayout.

What is ViewPager in Android?

ViewPager in Android allows the user to flip left and right through pages of data. In our android ViewPager application we'll implement a ViewPager that swipes through three views with different images and texts.


2 Answers

I believe this could work for you, if you are targeting API17 +. If not you should take a look at ViewContainers, and swipeable views.

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class TabHostParentFragment extends Fragment {

private FragmentTabHost tabHost;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
tabHost = new FragmentTabHost(getActivity());
tabHost.setup(getActivity(), getChildFragmentManager(), R.layout.my_parent_fragment);

Bundle arg1 = new Bundle();
arg1.putInt("Arg for Frag1", 1);
tabHost.addTab(tabHost.newTabSpec("Tab1").setIndicator("Frag Tab1"),
    NewsFragment.class, arg1);

Bundle arg2 = new Bundle();
arg2.putInt("Arg for Frag2", 2);
tabHost.addTab(tabHost.newTabSpec("Tab2").setIndicator("Frag Tab2"),
    MyNestedFragment2.class, arg2);

return tabHost;
}
like image 160
chriskvik Avatar answered Sep 27 '22 20:09

chriskvik


checkout below code it will definitely help you if u still looking to for answer & and it will work on API 11 for old version

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AnalogClock;
import android.widget.TabHost;

public class TabFragment extends Fragment {

    private TabHost mTabHost;
    View rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.tabinterview, container, false);
        mTabHost = (TabHost) rootView.findViewById(R.id.tabhost);
        mTabHost.setup();
        TabHost.TabSpec spec = mTabHost.newTabSpec("tag");
        spec.setIndicator("Android");
        spec.setContent(new TabHost.TabContentFactory() {

            @Override
            public View createTabContent(String tag) {
                // TODO Auto-generated method stub
                return (new AnalogClock(getActivity()));
            }
        });
        mTabHost.addTab(spec);
        spec = mTabHost.newTabSpec("tag1");
        spec.setIndicator("java");
        spec.setContent(new TabHost.TabContentFactory() {

            @Override
            public View createTabContent(String tag) {
                // TODO Auto-generated method stub
                return (new AnalogClock(getActivity()));
            }
        });
        mTabHost.addTab(spec);
        spec = mTabHost.newTabSpec("tag2");
        spec.setIndicator("Favourate");
        spec.setContent(new TabHost.TabContentFactory() {

            @Override
            public View createTabContent(String tag) {
                // TODO Auto-generated method stub
                return (new AnalogClock(getActivity()));
            }
        });
        mTabHost.addTab(spec);
        return rootView;
    }
}
like image 40
Nawaf Avatar answered Sep 27 '22 22:09

Nawaf