78981

Tabs in a class that extends fragment

I'm reading many tutorial how to implement tabs in a class that extends FragmentActivity,but no one about how to implement tabs in a class that extends Fragment I created with AndroidStudio a new project with a NavigationDrawerFragment:this navigationDrawerFragment for every option menu that it displays create a new Fragment. Inside this fragment i need to have some tabs. how can i add tabs inside onCreateView method of the fragment? someone can give to me a very simple example? <strong>What i'm doing to try to solve my problem:</strong> <strong>Step 1</strong> - i created a new LinearLayout, it contains the tabHost and it's the main fragment:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragment1" 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" android:orientation="horizontal"> </TabWidget> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:id="@+id/tab1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal"> </LinearLayout> <LinearLayout android:id="@+id/tab2" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal"> </LinearLayout> <LinearLayout android:id="@+id/tab3" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal"> </LinearLayout> </FrameLayout> </LinearLayout> </TabHost> </LinearLayout>

<strong>Step 2</strong> - on the main fragment I put this rows of code:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //View rootView = inflater.inflate(R.layout.vediamo, container, false); mTabHost = new FragmentTabHost(getActivity()); mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.fragment1); mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"), CreaAnnoFragment.class, null); mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"), ModificaAnnoFragment.class, null); mTabHost.addTab(mTabHost.newTabSpec("throttle").setIndicator("Throttle"), EliminaAnnoFragment.class, null); return mTabHost; } @Override public void onDestroyView() { super.onDestroyView(); mTabHost = null; }

If i run the program i get this error:

java.lang.NullPointerException at myapps.studentsmarks.CreaAnnoFragment.onAttach(CreaAnnoFragment.java:100)

<strong>FIXED - solution</strong> "the wrapped fragment" don't need the onAttach() method and this is logic. So just delete it

Answer1:

For that you can use FragmentTabHost class, that let's you add tabs to your Fragment, just as you do for Activities.

Here's an example:

public class FragmentTabsFragmentSupport extends Fragment { private FragmentTabHost mTabHost; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mTabHost = new FragmentTabHost(getActivity()); mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.fragment1); mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"), FirstFragment.class, null); mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"), SecondFragment.class, null); mTabHost.addTab(mTabHost.newTabSpec("throttle").setIndicator("Throttle"), ThirdFragment.class, null); return mTabHost; } @Override public void onDestroyView() { super.onDestroyView(); mTabHost = null; }

}

For more information, check the official documentation.

Recommend

  • how to use maps fragment in navigation drawer?
  • Android Viewpager show wrong pages
  • Can a fragment extend a fragment activity? [Android]
  • Edittext requestFocus() dynamically not working
  • adMob | Extend Banner size?
  • Android : Strike out Text with bold or thicker line than default STRIKE_THRU_TEXT_FLAG
  • Background image crashes android
  • Float Label hint (TextInputLayout) not works with Android Data Binding
  • Could be Text orientation of JTextArea changed by keyboard shortcut?
  • Android Custom dialog inflating from layout - alignment issue
  • EditText ignores the ImeActionLabel
  • jqPlot date axis - bars drawn on wrong day?
  • Complex Silverlight TreeView, is nested hierarchy possible?
  • Toolbar shifts up, when soft keyboard appears
  • Multibinding Multiselection ListView
  • Gray line inside cardview :)?
  • my tic-tac-toe program in matlab does not work [closed]
  • C# List of Panels
  • Using MouseListener to select a range of cells in a grid
  • How Lists (specifically, RecyclerView with CardViews) in Android work
  • FileReader+canvas image loading problem
  • Pass value from viewmodel to script in zk
  • Using $this when not in object context
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Deselecting radio buttons while keeping the View Model in synch
  • Getting last autonumber in access
  • Counter field in MS Access, how to generate?
  • How to draw moving and Running sine wave chart using JFree chart in java?
  • Incrementing object id automatically JS constructor (static method and variable)
  • How to check if every primary key value is being referenced as foreign key in another table
  • Can I have the cursor start on a particular column by default in jqgrid's edit mode?
  • jquery mobile loadPage not working
  • how to add data labels for bar graph in matlab
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How to stop GridView from loading again when I press back button?
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?