1. Are you ready for the Galaxy S20? Here is everything we know so far!

Android: Create tabs in fragmen

Discussion in 'Android Development' started by systx, Jan 28, 2016.

  1. systx

    systx Lurker
    Thread Starter

    I,ve application with NavigationDrawer. When i select an item, it open fragment.

    I've got problem with add tabs in Fragments.

    I like when i click on Item1 in Navigation drawer it open FirstFragment with 4 tabs for navigate betwen
    FirstFragment(first_fragment.xml),<br>
    FirstFragment1(first_fragment1.xml),<br>
    FirstFragment2(first_fragment2.xml) and <br>
    FirstFragment3(first_fragment3.xml)<br>

    and when i click on item2 it open SecondFragment(second_fragment.xml) with 4 tabs for navigate betwen SecondFragment(second_fragment.xml),SecondFragment1(second_fragment1.xml),SecondFragment2(second_fragment2.xml) and SecondFragment3(second_fragment3.xml)

    Can anyone help me with this problem?

    MainActivity.java

    Code (Text):
    1.  
    2.     public class MainActivity extends AppCompatActivity
    3.             implements NavigationView.OnNavigationItemSelectedListener {
    4.    
    5.         @Override
    6.         protected void onCreate(Bundle savedInstanceState) {
    7.             super.onCreate(savedInstanceState);
    8.             setContentView(R.layout.activity_main);
    9.             Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    10.             setSupportActionBar(toolbar);
    11.    
    12.             FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    13.             fab.setOnClickListener(new View.OnClickListener() {
    14.                 @Override
    15.                 public void onClick(View view) {
    16.                     Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
    17.                             .setAction("Action", null).show();
    18.                 }
    19.             });
    20.    
    21.             DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    22.             ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
    23.                     this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    24.             drawer.setDrawerListener(toggle);
    25.             toggle.syncState();
    26.    
    27.             NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    28.             navigationView.setNavigationItemSelectedListener(this);
    29.    
    30.             FragmentManager fm = getFragmentManager();
    31.             fm.beginTransaction().replace(R.id.content_frame, new FirstFragment()).commit();
    32.         }
    33.    
    34.         @Override
    35.         public void onBackPressed() {
    36.             DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    37.             if (drawer.isDrawerOpen(GravityCompat.START)) {
    38.                 drawer.closeDrawer(GravityCompat.START);
    39.             } else {
    40.                 super.onBackPressed();
    41.             }
    42.         }
    43.    
    44.         @Override
    45.         public boolean onCreateOptionsMenu(Menu menu) {
    46.             // Inflate the menu; this adds items to the action bar if it is present.
    47.             getMenuInflater().inflate(R.menu.main, menu);
    48.             return true;
    49.         }
    50.    
    51.         @Override
    52.         public boolean onOptionsItemSelected(MenuItem item) {
    53.             // Handle action bar item clicks here. The action bar will
    54.             // automatically handle clicks on the Home/Up button, so long
    55.             // as you specify a parent activity in AndroidManifest.xml.
    56.             int id = item.getItemId();
    57.    
    58.             //noinspection SimplifiableIfStatement
    59.             if (id == R.id.action_settings) {
    60.                 return true;
    61.             }
    62.    
    63.             return super.onOptionsItemSelected(item);
    64.         }
    65.    
    66.         @SuppressWarnings("StatementWithEmptyBody")
    67.         @Override
    68.         public boolean onNavigationItemSelected(MenuItem item) {
    69.             FragmentManager fm = getFragmentManager();
    70.             int id = item.getItemId();
    71.    
    72.             if (id == R.id.nav_camera) {
    73.                 fm.beginTransaction().replace(R.id.content_frame, new FirstFragment()).commit();
    74.             } else if (id == R.id.nav_gallery) {
    75.                 fm.beginTransaction().replace(R.id.content_frame, new SecondFragment()).commit();
    76.             } else if (id == R.id.nav_slideshow) {
    77.    
    78.             } else if (id == R.id.nav_manage) {
    79.    
    80.             } else if (id == R.id.nav_share) {
    81.    
    82.             } else if (id == R.id.nav_send) {
    83.    
    84.             }
    85.    
    86.             DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    87.             drawer.closeDrawer(GravityCompat.START);
    88.             return true;
    89.         }
    90.     }
    91.  
    92.  
    ----------


    FirstFragment.java


    Code (Text):
    1.  
    2.      public class FirstFragment extends Fragment {
    3.    
    4.         @Nullable
    5.         @Override
    6.         public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    7.             return inflater.inflate(R.layout.first_fragment,container,false);
    8.         }
    9.     }
    10.  
    ----------

    SecondFragment.java
    Code (Text):
    1.  
    2.     public class SecondFragment extends Fragment {
    3.  
    4.     @Nullable
    5.     @Override
    6.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    7.         return inflater.inflate(R.layout.second_fragment,container,false);
    8.     }
    9. }
    10.  
    ----------


    activity_main.xml
    Code (Text):
    1.  
    2.      <?xml version="1.0" encoding="utf-8"?>
    3.     <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    4.         xmlns:app="http://schemas.android.com/apk/res-auto"
    5.         xmlns:tools="http://schemas.android.com/tools"
    6.         android:id="@+id/drawer_layout"
    7.         android:layout_width="match_parent"
    8.         android:layout_height="match_parent"
    9.         android:fitsSystemWindows="true"
    10.         tools:openDrawer="start">
    11.    
    12.         <include
    13.             layout="@layout/app_bar_main"
    14.             android:layout_width="match_parent"
    15.             android:layout_height="match_parent" />
    16.    
    17.         <android.support.design.widget.NavigationView
    18.             android:id="@+id/nav_view"
    19.             android:layout_width="wrap_content"
    20.             android:layout_height="match_parent"
    21.             android:layout_gravity="start"
    22.             android:fitsSystemWindows="true"
    23.             app:headerLayout="@layout/nav_header_main"
    24.             app:menu="@menu/activity_main_drawer" />
    25.  
    26. </android.support.v4.widget.DrawerLayout>
    27.  
    28.  
    ----------


    app_bar_main.xml

    Code (Text):
    1.  
    2.  
    3.        <?xml version="1.0" encoding="utf-8"?>
    4.     <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    5.         xmlns:app="http://schemas.android.com/apk/res-auto"
    6.         xmlns:tools="http://schemas.android.com/tools"
    7.         android:layout_width="match_parent"
    8.         android:layout_height="match_parent"
    9.         android:fitsSystemWindows="true"
    10.         tools:context="com.example.sasor.test.MainActivity">
    11.    
    12.         <android.support.design.widget.AppBarLayout
    13.             android:layout_width="match_parent"
    14.             android:layout_height="wrap_content"
    15.             android:theme="@style/AppTheme.AppBarOverlay">
    16.    
    17.             <android.support.v7.widget.Toolbar
    18.                 android:id="@+id/toolbar"
    19.                 android:layout_width="match_parent"
    20.                 android:layout_height="?attr/actionBarSize"
    21.                 android:background="?attr/colorPrimary"
    22.                 app:popupTheme="@style/AppTheme.PopupOverlay" />
    23.    
    24.         </android.support.design.widget.AppBarLayout>
    25.    
    26.         <include layout="@layout/content_main" />
    27.    
    28.         <android.support.design.widget.FloatingActionButton
    29.             android:id="@+id/fab"
    30.             android:layout_width="wrap_content"
    31.             android:layout_height="wrap_content"
    32.             android:layout_gravity="bottom|end"
    33.             android:layout_margin="@dimen/fab_margin"
    34.             android:src="@android:drawable/ic_dialog_email" />
    35.    
    36.     </android.support.design.widget.CoordinatorLayout>
    37.  
    38.  
    ----------


    nav_header_main.xml

    Code (Text):
    1.  
    2.  
    3.        <?xml version="1.0" encoding="utf-8"?>
    4.     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    5.         android:layout_width="match_parent"
    6.         android:layout_height="@dimen/nav_header_height"
    7.         android:background="@drawable/side_nav_bar"
    8.         android:gravity="bottom"
    9.         android:orientation="vertical"
    10.         android:paddingBottom="@dimen/activity_vertical_margin"
    11.         android:paddingLeft="@dimen/activity_horizontal_margin"
    12.         android:paddingRight="@dimen/activity_horizontal_margin"
    13.         android:paddingTop="@dimen/activity_vertical_margin"
    14.         android:theme="@style/ThemeOverlay.AppCompat.Dark">
    15.    
    16.         <ImageView
    17.             android:id="@+id/imageView"
    18.             android:layout_width="wrap_content"
    19.             android:layout_height="wrap_content"
    20.             android:paddingTop="@dimen/nav_header_vertical_spacing"
    21.             android:src="@android:drawable/sym_def_app_icon" />
    22.    
    23.         <TextView
    24.             android:layout_width="match_parent"
    25.             android:layout_height="wrap_content"
    26.             android:paddingTop="@dimen/nav_header_vertical_spacing"
    27.             android:text="Android Studio"
    28.             android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
    29.    
    30.         <TextView
    31.             android:id="@+id/textView"
    32.             android:layout_width="wrap_content"
    33.             android:layout_height="wrap_content"
    34.             android:text="android.studio@android.com" />
    35.  
    36. </LinearLayout>
    37.  
    38.  
    39.  
     



    1. Download the Forums for Android™ app!


      Download

       
Loading...
Similar Threads - Android Create tabs
  1. My project
    Replies:
    9
    Views:
    729
  2. tiho_bg
    Replies:
    1
    Views:
    4,841
  3. CCCKP
    Replies:
    0
    Views:
    1,453
  4. Anmol hira
    Replies:
    0
    Views:
    1,779
  5. xpressive
    Replies:
    0
    Views:
    1,904
  6. Nabeel Rajabali
    Replies:
    0
    Views:
    1,862
  7. Youssef Hammad
    Replies:
    0
    Views:
    3,206
  8. Chen Dol Dawd
    Replies:
    1
    Views:
    1,181
  9. chethan1703
    Replies:
    0
    Views:
    2,232
  10. Dan Coleman
    Replies:
    0
    Views:
    2,252

Share This Page

Loading...