Sunday, 18 September 2016

Navigation Drawer Menu Example in android studio|Navigation Drawer Slider Menu Example

Required Class and Layouts:
1.Main Activity | activity_main.xml
3.Create folder inside-res->(right click on res)->new->directory->menu
4.Folder:- menu | inside menu (right click on menu)->new->Menu resource file->drawer_view.xml
5.For Navigation Header-> res->layout->new->Layout resource file->nav_header.xml
6.Open drawable -> add some icon
7.String.xml
8.Create new file inside  drawable->new->Drawable resource file->pressed_color.xml
9.Open build.gradle  add following dependencies compile 'com.android.support:design:24.1.1'


Step 1:-strings.xml
<resources>
    <string name="app_name">NavigationDrawer</string>
    <string name="drawer_open">Open</string>
    <string name="drawer_close">Close</string>
</resources>


Step 2:-menu->drawer_view.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

        <item
            android:id="@+id/itemHome"
            android:icon="@drawable/home"
            android:title="Home"></item>
        <item
            android:id="@+id/itemHelp"
            android:icon="@drawable/help"
            android:title="Help"></item>
        <item
            android:id="@+id/itemUser"
            android:icon="@drawable/user"
            android:title="User"></item>
        <item
            android:id="@+id/logout"
            android:icon="@drawable/help"
            android:title="Logout"></item>
    <item android:id="@+id/subMenu"
        android:title="More..">
        <menu>
            <item
                android:id="@+id/itemSearch"
                android:icon="@drawable/search"
                android:title="Search"></item>
            <item
                android:id="@+id/itemLocation"
                android:icon="@drawable/location"
                android:title="location"></item>
            <item
                android:id="@+id/itemGift"
                android:icon="@drawable/gift"
                android:title="Gift"></item>
        </menu>
    </item>
</menu>
Step 3:-res->layout->nav_header.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:paddingTop="20dp"
    android:paddingBottom="20dp"
    android:background="@color/colorPrimaryDark"
    android:layout_width="match_parent"
    android:layout_height="190dp">

    <ImageView
        android:src="@drawable/user"
        android:layout_width="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:layout_height="0dp" />
    <TextView
        android:id="@+id/tv_email"
        android:textColor="#ffffff"
        android:textSize="18sp"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Supriya"/>
</LinearLayout>
Step 4:-drawable->pressed_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@color/colorAccent"
        android:state_pressed="true"></item>
    <item android:drawable="@android:color/transparent"></item>

</selector>
Step5:-activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/navDrawer">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Main Content"/>
    </LinearLayout>
    <android.support.design.widget.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:menu="@menu/drawer_view"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header"
        app:itemBackground="@drawable/pressed_color"
        android:id="@+id/navView">

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

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

Step 6:-MainActivity.java
package supriya.com.navigationdrawer;

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.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
ActionBarDrawerToggle toggle;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        DrawerLayout drawerLayout=(DrawerLayout)findViewById(R.id.navDrawer);
        toggle=new ActionBarDrawerToggle(this,drawerLayout,R.string.drawer_open,R.string.drawer_close);
        drawerLayout.addDrawerListener(toggle);
        toggle.syncState();
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        NavigationView navigationView=(NavigationView)findViewById(R.id.navView);
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override            public boolean onNavigationItemSelected(MenuItem item) {
                switch (item.getItemId()){
                    case R.id.itemGift:
                    Toast.makeText(getApplicationContext(),"Gift",Toast.LENGTH_LONG).show();
                        break;
                    case R.id.itemLocation:
                        Toast.makeText(getApplicationContext(),"location",Toast.LENGTH_LONG).show();
                        break;
                    case R.id.itemSearch:
                        Toast.makeText(getApplicationContext(),"search",Toast.LENGTH_LONG).show();
                        break;
                }
                DrawerLayout d1=(DrawerLayout)findViewById(R.id.navDrawer);
                if(d1.isDrawerOpen(GravityCompat.START)){
                    d1.closeDrawer(GravityCompat.START);
                }
                return false;
            }
        });

    }

    @Override    public boolean onOptionsItemSelected(MenuItem item) {
        if (toggle.onOptionsItemSelected(item)){
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
Output:-


Friday, 16 September 2016

AutoCompleteTextview example in Android Studio | How to use of AutoCompleteTextview


Required Classes and Layouts
1.MainActivity.java | activity_java.xml
2.list_item_view.xml :- For list item view

Step1:-activity_main.xml
<?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="match_parent"
    android:layout_margin="16dp"
    android:gravity="center_vertical">
    <AutoCompleteTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/actv"
        android:hint="--Select--"
        android:ems="20"
        android:singleLine="true"
        android:completionThreshold="1"/>
</LinearLayout>
Step2:-MainActivity.java
package supriya.com.autocompletetextview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class MainActivity extends AppCompatActivity {
    AutoCompleteTextView actv;
    String[] item={"hyderabad","Andhra Pradesh","Amaravati","Arunachal Pradesh","Itanagar","Assam","Dispur","Bihar","Patna","Chhattisgarh","Raipur","West Bengal"};

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        actv=(AutoCompleteTextView)findViewById(R.id.actv);
        ArrayAdapter<String> adp=new ArrayAdapter<String>(getApplicationContext(),R.layout.list_item_view,item);
        actv.setAdapter(adp);

    }
}
Step3:-list_item_view.xml (hint:-go to app->right click on res->layouts->new->Layouts resource file->ok)
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:textColor="#FF0000"
    android:paddingLeft="16dp"
    android:minHeight="?android:attr/listPreferredItemHeightSmall" />
Output:-



Thursday, 15 September 2016

Context Menu Example in android studio | How to apply context menu in android application

Context Menu:-Android context menu appears when user press long click on the element. It is also known as floating menu.

Required classes and xml layouts:-
1.MainActivity.java | activity_main.xml

Create a new project and add following code to your project:-

Step1:- activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="supriya.com.contextmenu.MainActivity"
    android:background="#DF013A">
  <ListView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:id="@+id/list1"></ListView>
</RelativeLayout>
Step2:-MainActivity.java
package supriya.com.contextmenu;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    ListView listView;
    String[] list = {"Somya", "Supriya", "Pankaj", "Ankit", "Rajwari", "Honey", "Vikash", "Sunita", "Dipika", "Suman", "Akash", "Saurabh", "Mohan", "Ram"};

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.list1);
        ArrayAdapter<String> adp = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, list);
        listView.setAdapter(adp);
        registerForContextMenu(listView);


    }

    @Override    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.setHeaderTitle("Select Option");
        menu.add(0, v.getId(), 0, "Call");
        menu.add(0,v.getId(),0,"Message");
        menu.add(0,v.getId(),0,"Mail");
    }

    @Override    public boolean onContextItemSelected(MenuItem item) {
        if(item.getTitle()=="Call"){
            Toast.makeText(getApplicationContext(),"Call:Selected",Toast.LENGTH_LONG).show();
        }
        else if (item.getTitle()=="Message"){
            Toast.makeText(getApplicationContext(),"Message:Selected",Toast.LENGTH_LONG).show();
        }
        else if(item.getTitle()=="Mail"){
            Toast.makeText(getApplicationContext(),"Mail:Selected",Toast.LENGTH_LONG).show();
        }
        return super.onContextItemSelected(item);
    }
}
Output:-
         

Option Menu example in android studio| How to set option Menu in Actionbar

Option Menu:-Android Option Menus are the primary menus of android. They can be used for settings, search, delete item etc.
Here, we are inflating the menu by calling the inflate() method of MenuInflater class. To perform event handling on menu items, you need to override onOptionsItemSelected() method of Activity class.
We are going to see  example of option menus. We will see the simple option menus and second, options menus with images.

Required Classes and xml layouts:-
1.MainActivity.java | activity_main.xml
2.Home.java | home.xml
3.menu (create directory inside res with the name menu) | option_menu.xml(right click on menu->new->Menu resource file->give name option_menu.xml->ok. )
Step1:-Add 4 images inside res->drawable  with the name (u can add image according to your requirment)
1.home.jpg/png
2.profile.jpg/png
3.about.jpg/png
4.help.jpg/png
Step2:-option_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:optionmenu="http://schemas.android.com/apk/res-auto">
    <item
        android:title="Home"
        android:id="@+id/itemHome"
        android:icon="@drawable/home"
        optionmenu:showAsAction="ifRoom"></item>
    <item
        android:title="User"
        android:id="@+id/itemUser"
        android:icon="@drawable/user"
        optionmenu:showAsAction="ifRoom"></item>
    <item
        android:title="About"
        android:id="@+id/itemAbout"
        android:icon="@drawable/about"
        optionmenu:showAsAction="ifRoom"></item>
    <item
        android:title="Help"
        android:id="@+id/itemHelp"
        android:icon="@drawable/help"
        optionmenu:showAsAction="ifRoom"></item>
</menu>
Step 3:- activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_vertical"
    tools:context="supriya.com.optionmenuexample.MainActivity">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Example Of Option Menu"
        android:textSize="30sp"
        android:textAlignment="center"
        android:textColor="@color/colorAccent"/>
</LinearLayout>
Step 4:-MainActivity.java
package supriya.com.optionmenuexample;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

    @Override   
 public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.option_menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override   
 public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.itemHome:
                startActivity(new Intent(MainActivity.this,Home.class));
                Toast.makeText(getApplicationContext(),"Home selected",Toast.LENGTH_LONG).show();
                return true;
            case R.id.itemUser:
                Toast.makeText(getApplicationContext(),"User Selected",Toast.LENGTH_LONG).show();
                return true;
            case R.id.itemAbout:
                Toast.makeText(getApplicationContext(),"About Selected",Toast.LENGTH_LONG).show();
                return true;
            case R.id.itemHelp:
                Toast.makeText(getApplicationContext(),"Help Selected",Toast.LENGTH_LONG).show();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
Step5:-home.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Welcome to Home"
        android:textSize="30sp"
        android:textAlignment="center"/>
</LinearLayout>
Step 6:-Home.java
package supriya.com.optionmenuexample;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

/** * Created by Supriya on 9/15/2016. */
public class Home extends AppCompatActivity {
    @Override    
protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);
    }
}

Output:-
         

Wednesday, 14 September 2016

PopUp Menu Example in Android Studio

1. Android PopUp Menu?
Ans:-Android Popup Menu displays the menu below the anchor text if space is available otherwise above the anchor text. It disappears if you click outside the popup menu.PopupMenu is the direct subclass of java.lang.Object class.


Example of PopUp Menu:
Required class,file,&layouts:-
1.MainActivity.java | activity_main.xml
2.Login.java | login.xml
3.Make Folderinside res:- menu | popup_menu.xml
Step1:-Take new project and add new folder inside res.Right Click on  res->new->Directory->set name menu->ok.Now right click on menu folder and add new layouts for popup menu options. res-.menu->right click on menu->new->Menu resource file->give name popup_menu.xml->ok.

Step2:-popup_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:id="@+id/itemLogin"
            android:title="Login">
        </item>
    <item
        android:id="@+id/itemSignup"
        android:title="SignUp">
    </item>
    <item
        android:id="@+id/itemPfofile"
        android:title="Profile">
    </item>
    <item
        android:id="@+id/itemSetting"
        android:title="Settings">
    </item>
</menu>
Step3:-activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="16dp"
    tools:context="supriya.com.popupmenu.MainActivity">

   <Button
       android:layout_marginTop="30dp"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:id="@+id/btnPopup"
       android:text="Show PopUp Menu"
       android:background="#FE2E64"
       android:textColor="#fff"/>
</LinearLayout>
Step4:-MainActivity.java
package supriya.com.popupmenu;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.PopupMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    Button btnPopup;

    @Override    
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnPopup=(Button)findViewById(R.id.btnPopup);
        btnPopup.setOnClickListener(new View.OnClickListener() {
            @Override            
public void onClick(View view) {
                PopupMenu popUpMenu=new PopupMenu(MainActivity.this,view);
                popUpMenu.getMenuInflater().inflate(R.menu.popup_menu,popUpMenu.getMenu());
               popUpMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                   @Override  
                 public boolean onMenuItemClick(MenuItem item) {
                       switch (item.getItemId()){
                           case R.id.itemLogin:
                               startActivity(new Intent(MainActivity.this,Login.class));
                               Toast.makeText(getApplicationContext(),"Login",Toast.LENGTH_LONG).show();
                               return true;

                           case R.id.itemSignup:
                               Toast.makeText(getApplicationContext(),"Signup",Toast.LENGTH_LONG).show();
                               return true;

                           case R.id.itemPfofile:
                               Toast.makeText(getApplicationContext(),"Profile",Toast.LENGTH_LONG).show();
                               return true;

                           case R.id.itemSetting:
                               Toast.makeText(getApplicationContext(),"Settings",Toast.LENGTH_LONG).show();
                               return true;
                       }

                       return true;
                   }
               });
                popUpMenu.show();
            }
        });
    }
}
Step5:-login.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical" android:layout_width="match_parent"     android:layout_height="match_parent"     android:layout_margin="16dp"     android:gravity="center_vertical">     <EditText         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:hint="Enter Email"/>     <EditText         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:hint="Password"/> </LinearLayout> Step6:-Login.java
package supriya.com.popupmenu;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

/** * Created by Supriya on 9/14/2016. */public class Login extends AppCompatActivity {
    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
    }
}
Output:-



Saturday, 10 September 2016

View Pager Example in Android Studio

1. What is ViewPager?
Ans:-Layout manager that allows the user to flip left and right through pages of data. You supply an implementation of a PagerAdapter to generate the pages that the view shows. ViewPager is most often used in conjunction with Fragment , which is a convenient way to supply and manage the lifecycle of each page.

Here we will see how to implement ViewPager in Android Application.For this Example we will create 3 Layouts and their JAVA class and add these Layouts to MainActivity.java.Following are the site map.
1.MainActiviry.java  | activity_main.xml
2.ViewPager1.java  | view_pager1.xml
3.ViewPager2.java  | view_pager2.xml
4.ViewPager3.java  | view_pager3.xml

Step 1:- main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="supriya.com.viewpagerexample.MainActivity">

    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/vPager">
</android.support.v4.view.ViewPager>
</RelativeLayout>

Step 2:- MainActivity.java
package supriya.com.viewpagerexample;
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.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
    ViewPager vpager;

    @Override    
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        vpager=(ViewPager)findViewById(R.id.vPager);
        vpager.setAdapter(new setViewadapter(getSupportFragmentManager()));
    }
    public class setViewadapter extends FragmentPagerAdapter {
        public setViewadapter(FragmentManager fm) {
            super(fm);
        }
        @Override        
public Fragment getItem(int position) {
            if(position==0) {
                return new ViewPage1();
            }
            else if (position==1){
                return  new ViewPage2();
            }
            else                
                 return new ViewPage3();
        }

        @Override        
public int getCount() {
            return 3;
        }
    }
}
Step 3:- view_page1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:background="#3ADF00">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="ViewPage1"
        android:textSize="25sp"
        android:textAlignment="center"
        android:textColor="#ffffff"/>

</LinearLayout>
Step 4:- ViewPage1.java
package supriya.com.viewpagerexample;

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 Supriya on 9/11/2016. */
public class ViewPage1 extends Fragment {
    @Nullable    @Override    
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, 
@Nullable Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.view_page1,null);
        return v;
    }
}
Step 5:- view_page2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:background="#B4045F">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="ViewPage2"
        android:textSize="25sp"
        android:textAlignment="center"
        android:textColor="#ffffff"/>

</LinearLayout>
Step 6:- ViewPage2.java
package supriya.com.viewpagerexample;

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 Supriya on 9/11/2016. */
public class ViewPage2 extends Fragment {
    @Nullable    @Override   
 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.view_page2,null);
        return v;
    }
}
Step 7:- view_page3.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:background="#0174DF">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="ViewPage3"
        android:textSize="25sp"
        android:textAlignment="center"
        android:textColor="#ffffff"/>


</LinearLayout>
Step 8:- ViewPage3.java
package supriya.com.viewpagerexample;

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 Supriya on 9/11/2016. */
public class ViewPage3 extends Fragment {
    @Nullable    @Override    
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.view_page3,null);
        return v;
    }
}