2

I have managed to add TabHost on my fragment but when i click on one tab i want another fragment to come in FrameLayout, same goes to another.

I have tried applying the method but it does not work in setup host.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent) .

What can be done to solve this issue ?

Here is my XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >


    <TabHost
        android:id="@+id/tabHost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true">

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

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="30dp" />

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

                <LinearLayout
                    android:id="@+id/tab1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tab2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                </LinearLayout>

            </FrameLayout>
        </LinearLayout>


    <FrameLayout
        android:id="@+id/realtabcontent"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@+id/tabLinear"
        />
    </TabHost>




</RelativeLayout>

Here is my Java Code:

public class HomeFragment extends Fragment {

  @Override
  public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


  }

  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_home,
            container, false);


    TabHost host = (TabHost) view.findViewById(R.id.tabHost);
    host.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);


    host.addTab(host.newTabSpec("tab11").setIndicator("Tab11"), PhotosFragment1.class, null);


    host.addTab(host.newTabSpec("tab22").setIndicator("Tab22"), PhotosFragment2.class, null);


    return view;


 }


}
Satan Pandeya
  • 3,747
  • 4
  • 27
  • 53
Rajiv Reddy
  • 411
  • 4
  • 15
  • Could you try to replace your `TabHost`with a `FragmentTabHost` and call `host.setup(getActivity(), getFragmentManager(), R.id.realtabcontent);` – redead Mar 27 '17 at 08:42
  • i first used FragmentTabHost but i keep getting null pointer exceptions in layout and java...so switched to TabHost – Rajiv Reddy Mar 27 '17 at 08:44
  • As stated in the answer to this [question](http://stackoverflow.com/questions/17227855/tabhost-with-fragments-and-fragmentactivity) you should be able to achieve it without any exception. I got it well running too. – redead Mar 27 '17 at 08:49
  • Exception raised during rendering: No tab known for tag null this is what happens in xml – Rajiv Reddy Mar 27 '17 at 09:07
  • Okay tnx bro it works for me when i run it perfect....but still the when i see the xml file it still shows (Exception raised during rendering: No tab known for tag null) – Rajiv Reddy Mar 27 '17 at 12:07

1 Answers1

0

Change your HomeFragments onCreateView() to

View view = inflater.inflate( R.layout.fragment_home, container, false );

FragmentTabHost host = ( FragmentTabHost ) view.findViewById( R.id.tabHost );
host.setup(getActivity(), getFragmentManager(), R.id.realtabcontent);

host.addTab( host.newTabSpec( "tab11" ).setIndicator( "Tab11" ), PhotosFragment1.class, null );
host.addTab( host.newTabSpec( "tab22" ).setIndicator( "Tab22" ), PhotosFragment2.class, null );

return view;

In your fragment_home.xml go for

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

<android.support.v4.app.FragmentTabHost
    android:id="@+id/tabHost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true">

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

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="30dp"/>

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

            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

            </LinearLayout>

        </FrameLayout>

    </LinearLayout>

    <FrameLayout
        android:id="@+id/realtabcontent"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@+id/tabLinear"/>

</android.support.v4.app.FragmentTabHost>

Have your PhotosFragment1 and PhotosFragment2 like

public class PhotosFragment1 extends Fragment
{
    @Nullable
    @Override
    public View onCreateView( LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState ) 
    {
        return View.inflate( getContext(), R.layout.fragment_photos_1, null ); 
    } 
}

and it should work fine.

redead
  • 360
  • 3
  • 16
  • Okay tnx bro it works for me when i run it perfect....but still the when i see the xml file it still shows (Exception raised during rendering: No tab known for tag null) – Rajiv Reddy Mar 27 '17 at 12:07
  • @RajivReddy have a look at this [question](http://stackoverflow.com/questions/15354165/android-fragmenttabhost-no-tab-known-for-tag-null). There could be additional info about your problem – redead Mar 27 '17 at 12:11