Monday, 1 February 2016

Android TabLayout with Navigation Drawer

Android TabLayout   with  Navigation Drawer




(1)write code  for    “MainActivity.java”   file :
package com.androidbelieve.drawerwithswipetabs;

import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity{
    DrawerLayout
mDrawerLayout;
    NavigationView
mNavigationView;
    FragmentManager
mFragmentManager;
    FragmentTransaction
mFragmentTransaction;

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);

       
/**
         *Setup the DrawerLayout and NavigationView
         */

            
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
            
mNavigationView = (NavigationView) findViewById(R.id.shitstuff) ;

       
/**
         * Lets inflate the very first fragment
         * Here , we are inflating the TabFragment as the first Fragment
         */

            
mFragmentManager = getSupportFragmentManager();
            
mFragmentTransaction = mFragmentManager.beginTransaction();
            
mFragmentTransaction.replace(R.id.containerView,new TabFragment()).commit();
       
/**
         * Setup click events on the Navigation View Items.
         */

            
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            
@Override
            
public boolean onNavigationItemSelected(MenuItem menuItem) {
               
mDrawerLayout.closeDrawers();



                
if (menuItem.getItemId() == R.id.nav_item_sent) {
                     FragmentTransaction fragmentTransaction =
mFragmentManager.beginTransaction();
                     fragmentTransaction.replace(R.id.
containerView,new SentFragment()).commit();

                 }

               
if (menuItem.getItemId() == R.id.nav_item_inbox) {
                    FragmentTransaction xfragmentTransaction =
mFragmentManager.beginTransaction();
                    xfragmentTransaction.replace(R.id.
containerView,new TabFragment()).commit();
                }

                
return false;
            }

        });

       
/**
         * Setup Drawer Toggle of the Toolbar
         */

               
android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
                ActionBarDrawerToggle mDrawerToggle =
new ActionBarDrawerToggle(this,mDrawerLayout, toolbar,R.string.app_name,
                R.string.
app_name);

               
mDrawerLayout.setDrawerListener(mDrawerToggle);

                mDrawerToggle.syncState();

    }
}

(2)write     code for     activity_main.xml      file:
<LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:fitsSystemWindows="true"

    android:orientation="vertical">



    <android.support.v7.widget.Toolbar

        xmlns:android="http://schemas.android.com/apk/res/android"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:background="@color/orange"

        android:id="@+id/toolbar"

        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"

        app:title="Palghar Distirc Police" />



    <android.support.v4.widget.DrawerLayout

        xmlns:android="http://schemas.android.com/apk/res/android"

        xmlns:app="http://schemas.android.com/apk/res-auto"

        android:layout_height="match_parent"

        android:layout_width="match_parent"

        android:id="@+id/drawerLayout"

        >







    <FrameLayout

        android:orientation="vertical"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:id="@+id/containerView">

     </FrameLayout>







    <android.support.design.widget.NavigationView

        xmlns:android="http://schemas.android.com/apk/res/android"

        xmlns:app="http://schemas.android.com/apk/res-auto"

        android:layout_width="wrap_content"

        android:layout_height="match_parent"

        android:layout_gravity="start"

        android:id="@+id/shitstuff"

        app:itemTextColor="@color/black"

        app:menu="@menu/drawermenu"

        android:layout_marginTop="-24dp"

        />







    </android.support.v4.widget.DrawerLayout>



    </LinearLayout>

(3)write code for     PrimaryFragment.java      file :
package com.androidbelieve.drawerwithswipetabs;



import android.os.Bundle;

import android.support.annotation.Nullable;

import android.support.v4.app.Fragment;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;



/**

 * Created by on 7/29/2015.

 */

public class PrimaryFragment extends Fragment {



    @Nullable

    @Override

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

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

    }

}
 
(4)write  code for primary_layout.xml 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">



    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginTop="40dp"

        android:textSize="30sp"

        android:gravity="center"

        android:id="@+id/textView"

        android:layout_centerHorizontal="true"

        android:textColor="@android:color/holo_blue_dark"

        android:text="Primary\nFragment"/>



    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:autoLink="web"

        android:textSize="15sp"

        android:layout_marginTop="10dp"

        android:layout_centerHorizontal="true"

        android:text="vasaitaluka"

        android:textColor="#000"

        android:layout_below="@+id/textView"

        android:textStyle="italic"/>



</RelativeLayout>
 
(5)write code for   SentFragment.java file:
 
package com.androidbelieve.drawerwithswipetabs;



import android.os.Bundle;

import android.support.annotation.Nullable;

import android.support.v4.app.Fragment;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;



/**

 * Created by  7/29/2015.

 */

public class SentFragment extends Fragment {



    @Nullable

    @Override

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

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

    }

}
 
(6)write code for   sent_layout.xml 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">



    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textSize="30sp"

        android:layout_marginTop="55dp"

        android:gravity="center"

        android:id="@+id/textView"

        android:layout_centerHorizontal="true"

        android:textColor="@android:color/holo_red_light"

        android:text="Sent\nFragment"/>



    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:autoLink="web"

        android:textSize="15sp"

        android:layout_marginTop="10dp"

        android:layout_centerHorizontal="true"

        android:text="vasaitaluka"

        android:textColor="#000"

        android:layout_below="@+id/textView"

        android:textStyle="italic"/>



</RelativeLayout>
 
 
 
(7)write   code for    SocialFragment.java     file:
package com.androidbelieve.drawerwithswipetabs;



import android.os.Bundle;

import android.support.annotation.Nullable;

import android.support.v4.app.Fragment;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;



/**

 * Created by  on 7/29/2015.

 */

public class SocialFragment extends Fragment {



    @Nullable

    @Override

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

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

    }





}
 
(8)write code for  social_layout.xml 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">



    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginTop="40dp"

        android:textSize="30sp"

        android:gravity="center"

        android:id="@+id/textView"

        android:layout_centerHorizontal="true"

        android:textColor="@android:color/holo_green_dark"

        android:text="Social\nFragment"/>



    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:autoLink="web"

        android:textSize="15sp"

        android:layout_marginTop="10dp"

        android:layout_centerHorizontal="true"

        android:text="vasaitaluka"

        android:textColor="#000"

        android:layout_below="@+id/textView"

        android:textStyle="italic"/>



</RelativeLayout>
 
(9)write code for TabFragment.java file:
 
package com.androidbelieve.drawerwithswipetabs;



import android.os.Bundle;

import android.support.annotation.Nullable;

import android.support.design.widget.TabLayout;

import android.support.v4.app.Fragment;

import android.support.v4.app.FragmentManager;

import android.support.v4.app.FragmentPagerAdapter;

import android.support.v4.view.ViewPager;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;



/**

 * Created by   7/27/2015.

 */

public class TabFragment extends Fragment {



    public static TabLayout tabLayout;

    public static ViewPager viewPager;

    public static int int_items = 3 ;



    @Nullable

    @Override

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

        /**

         *Inflate tab_layout and setup Views.

         */

            View x =  inflater.inflate(R.layout.tab_layout,null);

            tabLayout = (TabLayout) x.findViewById(R.id.tabs);

            viewPager = (ViewPager) x.findViewById(R.id.viewpager);



        /**

         *Set an Apater for the View Pager

         */

        viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));



        /**

         * Now , this is a workaround ,

         * The setupWithViewPager dose't works without the runnable .

         * Maybe a Support Library Bug .

         */



        tabLayout.post(new Runnable() {

            @Override

            public void run() {

                    tabLayout.setupWithViewPager(viewPager);

                   }

        });



        return x;



    }



    class MyAdapter extends FragmentPagerAdapter{



        public MyAdapter(FragmentManager fm) {

            super(fm);

        }



        /**

         * Return fragment with respect to Position .

         */



        @Override

        public Fragment getItem(int position)

        {

          switch (position){

              case 0 : return new PrimaryFragment();

              case 1 : return new SocialFragment();

              case 2 : return new UpdatesFragment();

          }

        return null;

        }



        @Override

        public int getCount() {



            return int_items;



        }



        /**

         * This method returns the title of the tab according to the position.

         */



        @Override

        public CharSequence getPageTitle(int position) {



            switch (position){

                case 0 :

                    return "Primary";

                case 1 :

                    return "Social";

                case 2 :

                    return "Updates";

            }

                return null;

        }

    }



}
 

(10) write   code    for    tab_layout.xml    file  :
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    android:layout_width="match_parent"

    android:orientation="vertical"

    android:layout_height="wrap_content">





    <android.support.design.widget.TabLayout

        android:id="@+id/tabs"

        app:tabGravity="fill"

        app:tabMode="fixed"

        android:background="@color/material_blue_grey_800"

        app:tabIndicatorColor="@color/orange"

        app:tabSelectedTextColor="@color/orange"

        app:tabTextColor="@color/white"

        android:layout_width="match_parent"

        android:layout_height="wrap_content">

    </android.support.design.widget.TabLayout>



    <android.support.v4.view.ViewPager

        android:id="@+id/viewpager"

        android:layout_width="match_parent"

        android:layout_height="match_parent">



    </android.support.v4.view.ViewPager>







</LinearLayout>

(11)write     code    for    UpdatesFragement.java      file  :
package com.androidbelieve.drawerwithswipetabs;



import android.os.Bundle;

import android.support.annotation.Nullable;

import android.support.v4.app.Fragment;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;



/**

 * Created by  on 7/29/2015.

 */

public class UpdatesFragment extends Fragment {



    @Nullable

    @Override

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

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

    }

}

(12)write code for  updates_layout.xml 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">



    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textSize="30sp"

        android:layout_marginTop="40dp"

        android:gravity="center"

        android:id="@+id/textView"

        android:layout_centerHorizontal="true"

        android:textColor="@android:color/holo_red_light"

        android:text="Updates\nFragment"/>



    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:autoLink="web"

        android:textSize="15sp"

        android:layout_marginTop="10dp"

        android:layout_centerHorizontal="true"

        android:text="vasaitaluka"

        android:textColor="#000"

        android:layout_below="@+id/textView"

        android:textStyle="italic"/>



</RelativeLayout>


(13)under menu  folder:
(a)write code for drawer.xm l  file:
<menu xmlns:android="http://schemas.android.com/apk/res/android">



            <item android:title="Compose" android:id="@+id/navigation_item_2" android:icon="@drawable/composea" ></item>

            <item android:title="Inbox"  android:icon="@drawable/inbox" ></item>

            <item android:title="Sent" android:icon="@drawable/senta"></item>

            <item android:title="Drafts" android:icon="@drawable/draft"></item>









    <item android:title="Others">

        <menu>

            <item

                android:title="Spam" android:icon="@drawable/spam"/>

            <item

                android:title="Bin" android:icon="@drawable/bin"/>

        </menu>

    </item>



    <group android:id="@+id/shitss">



            <item android:title="Settings" android:icon="@drawable/settings"></item>

            <item android:title="Help" android:icon="@drawable/help"></item>

    </group>











</menu>
(b)write    code   for   drawermenu.xml    file:
<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android">







    <item android:title="Inbox"

          android:id="@+id/nav_item_inbox"

          android:icon="@drawable/inbox"/>



    <item android:title="Sent"

          android:id="@+id/nav_item_sent"

          android:icon="@drawable/sent"/>



    <item android:title="Drafts"

          android:id="@+id/nav_item_draft"

          android:icon="@drawable/draft"/>







    <item android:title="Others">

        <menu>

            <item

                android:title="Spam"

                android:icon="@drawable/spam"/>

            <item

                android:title="Bin"

                android:icon="@drawable/bin"/>

        </menu>

    </item>



    <group android:id="@+id/group_settings_id">



        <item android:title="Settings"

              android:icon="@drawable/settings"/>

        <item android:title="Help"

             android:icon="@drawable/help"/>

    </group>





</menu>

( c) write code for  menu_main.xml file:
<menu xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">

    <item android:id="@+id/action_settings" android:title="@string/action_settings"

        android:orderInCategory="100" app:showAsAction="never" />



</menu>




output:





Recent Posts

Powered by Blogger.

Pages

About Me

My Photo
B.E.(COMPUTER ENGINEER) PHP Web developer & Provide Training for Web Development , Digital Marketing ,Android Mobile Apps Development

 

© 2013 ANDROID TRAINING IN MUMBAI BY OM SIR. All rights resevered. Designed by Templateism

Back To Top