Search code examples
androidandroid-tabhostandroid-dialogfragment

Placing Tabs in DialogFragment


I am attempting to place TabHost inside of DialogFragment but at present it is none functional and returns null on the line:

tabs.findViewById(R.id.tabHost);

How can tabhost be initialized in the DialogFragment to allow the tabs to appear?

Tabbed DialogLog class:

public class InviteFriendTabbedAlertDialog extends DialogFragment {

    TabHost tabs;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //return inflater.inflate(R.layout.invite_friend_tabbed_dialog, null);

        // Add tabs
        tabs.findViewById(R.id.tabHost);

        tabs.setup();

        TabHost.TabSpec tabpage1 = tabs.newTabSpec("one");
        tabpage1.setContent(R.id.shareIndividual);
        tabpage1.setIndicator("Tab 1", getResources().getDrawable(R.drawable.abc_ab_solid_dark_holo));

        TabHost.TabSpec tabpage2 = tabs.newTabSpec("two");
        tabpage2.setContent(R.id.shareGroup);
        tabpage2.setIndicator("Tab 2", getResources().getDrawable(R.drawable.abc_ab_bottom_transparent_light_holo));

        tabs.addTab(tabpage1);
        tabs.addTab(tabpage2);

        return inflater.inflate(R.layout.invite_friend_tabbed_dialog, null);

    }


}

tabbed layout file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TabHost
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tabHost"
        android:layout_gravity="center_horizontal">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical">

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"></TabWidget>

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">

                <LinearLayout
                    android:id="@+id/shareIndividual"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:orientation="vertical"></LinearLayout>

                <LinearLayout
                    android:id="@+id/shareGroup"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:orientation="vertical"></LinearLayout>

            </FrameLayout>
        </LinearLayout>
    </TabHost>
</RelativeLayout>

Solution

  • Initialize tabs and also use inflated view in which have TabHost with tabHost to initialize tabs variable :

    @Override
        public View onCreateView(LayoutInflater inflater, 
                              ViewGroup container, Bundle savedInstanceState) {
          View  view=inflater.inflate(R.layout.invite_friend_tabbed_dialog, null);
    
            // Add tabs
            tabs=(TabHost)view.findViewById(R.id.tabHost);
            //...your code here
            return view;
        }