Wednesday, 27 January 2016

scorlling activity with navigation drawer activity

01:53

Scrolling activity with navigation drawer activity:



(1)write  code for MainActivity2.java file:
package mypractice.om.com.mypractice;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

public class Main2Activity extends AppCompatActivity
       
implements NavigationView.OnNavigationItemSelectedListener {

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main2);
        Toolbar toolbar = (Toolbar) findViewById(R.id.
toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.
fab);
        fab.setOnClickListener(
new View.OnClickListener() {
           
@Override
           
public void onClick(View view) {
                Snackbar.make(view,
"Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction(
"Action", null).show();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.
drawer_layout);
        ActionBarDrawerToggle toggle =
new ActionBarDrawerToggle(
               
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.
nav_view);
        navigationView.setNavigationItemSelectedListener(
this);
    }

   
@Override
   
public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.
drawer_layout);
       
if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.
START);
        }
else {
           
super.onBackPressed();
        }
    }

   
@Override
   
public boolean onCreateOptionsMenu(Menu menu) {
       
// Inflate the menu; this adds items to the action bar if it is present.
       
getMenuInflater().inflate(R.menu.main2, menu);
       
return true;
    }

   
@Override
   
public boolean onOptionsItemSelected(MenuItem item) {
       
// Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
       
int id = item.getItemId();

       
//noinspection SimplifiableIfStatement
       
if (id == R.id.action_settings) {
           
return true;
        }

       
return super.onOptionsItemSelected(item);
    }

   
@SuppressWarnings("StatementWithEmptyBody")
    
@Override
   
public boolean onNavigationItemSelected(MenuItem item) {
       
// Handle navigation view item clicks here.
       
int id = item.getItemId();

       
if (id == R.id.nav_camara) {
           
// Handle the camera action
       
} else if (id == R.id.nav_gallery) {

        }
else if (id == R.id.nav_slideshow) {

        }
else if (id == R.id.nav_manage) {

        }
else if (id == R.id.nav_share) {

        }
else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.
drawer_layout);
        drawer.closeDrawer(GravityCompat.
START);
       
return true;
    }
}

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

<android.support.v4.widget.DrawerLayout 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" android:id="@+id/drawer_layout"

    android:layout_width="match_parent" android:layout_height="match_parent"

    android:fitsSystemWindows="true" tools:openDrawer="start">



    <include layout="@layout/app_bar_main2" android:layout_width="match_parent"

        android:layout_height="match_parent" />



    <android.support.design.widget.NavigationView android:id="@+id/nav_view"

        android:layout_width="wrap_content" android:layout_height="match_parent"

        android:layout_gravity="start" android:fitsSystemWindows="true"

        app:headerLayout="@layout/nav_header_main2" app:menu="@menu/activity_main2_drawer" />



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












(3)write code  app_bar_main2.xml   file :
<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout

    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" android:layout_width="match_parent"

    android:layout_height="match_parent" android:fitsSystemWindows="true"

    tools:context="mypractice.om.com.mypractice.Main2Activity">



    <android.support.design.widget.AppBarLayout android:id="@+id/app_bar"

        android:fitsSystemWindows="true" android:layout_height="@dimen/app_bar_height"

        android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">



        <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/toolbar_layout"

            android:fitsSystemWindows="true" android:layout_width="match_parent"

            android:layout_height="match_parent" app:layout_scrollFlags="scroll|exitUntilCollapsed"

            app:contentScrim="?attr/colorPrimary">



            <android.support.v7.widget.Toolbar android:id="@+id/toolbar"

                android:layout_height="?attr/actionBarSize" android:layout_width="match_parent"

                app:layout_collapseMode="pin" app:popupTheme="@style/AppTheme.PopupOverlay" />



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

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



    <include layout="@layout/content_main2" />



    <android.support.design.widget.FloatingActionButton android:id="@+id/fab"

        android:layout_width="wrap_content" android:layout_height="wrap_content"

        android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"

        android:src="@android:drawable/ic_dialog_email" />



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

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

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

    xmlns:tools="http://schemas.android.com/tools"

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

    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:paddingBottom="@dimen/activity_vertical_margin"

    app:layout_behavior="@string/appbar_scrolling_view_behavior"

    tools:showIn="@layout/app_bar_main2" tools:context="mypractice.om.com.mypractice.Main2Activity">



</RelativeLayout>
 
(5)now nav_header_main.xml   file:
 
<?xml version="1.0" encoding="utf-8"?>

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

    android:layout_width="match_parent" android:layout_height="@dimen/nav_header_height"

    android:background="@drawable/side_nav_bar"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

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

    android:gravity="bottom">



    <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content"

        android:paddingTop="@dimen/nav_header_vertical_spacing"

        android:src="@android:drawable/sym_def_app_icon" android:id="@+id/imageView" />



    <TextView android:layout_width="match_parent" android:layout_height="wrap_content"

        android:paddingTop="@dimen/nav_header_vertical_spacing" android:text="Android Studio"

        android:textAppearance="@style/TextAppearance.AppCompat.Body1" />



    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"

        android:text="android.studio@android.com" android:id="@+id/textView" />



</LinearLayout>


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

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



    <group android:checkableBehavior="single">

        <item android:id="@+id/nav_camara" android:icon="@android:drawable/ic_menu_camera"

            android:title="Import" />

        <item android:id="@+id/nav_gallery" android:icon="@android:drawable/ic_menu_gallery"

            android:title="Gallery" />

        <item android:id="@+id/nav_slideshow" android:icon="@android:drawable/ic_menu_slideshow"

            android:title="Slideshow" />

        <item android:id="@+id/nav_manage" android:icon="@android:drawable/ic_menu_manage"

            android:title="Tools" />

    </group>



    <item android:title="Communicate">

        <menu>

            <item android:id="@+id/nav_share" android:icon="@android:drawable/ic_menu_share"

                android:title="Share" />

            <item android:id="@+id/nav_send" android:icon="@android:drawable/ic_menu_send"

                android:title="Send" />

        </menu>

    </item>



</menu>
 
 
(7)Write code for  menu/main2.xml file:
 
<?xml version="1.0" encoding="utf-8"?>

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

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

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

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

</menu>
 
 
 
 
 
 
(8)write code for    drawable/side_nav_bar.xml   file:
<shape xmlns:android="http://schemas.android.com/apk/res/android"

    android:shape="rectangle" >

    <gradient

        android:startColor="#81C784"

        android:centerColor="#4CAF50"

        android:endColor="#2E7D32"

        android:type="linear"

        android:angle="135"/>

</shape>
 
 


Written by

We are Creative Blogger Theme Wavers which provides user friendly, effective and easy to use themes. Each support has free and providing HD support screen casting.

0 comments:

Post a Comment

 

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

Back To Top