[Java for Android App Development] :
4)Implement ViewPager using fragments
​

Blue = Add

Red = Delete

Now go to src > com.example.viewpagertest and edit MainActivity.java

​

Delete the content marked in red:

[COLOR="Blue"]package com.example.viewpagertest;[/COLOR]

[COLOR="Red"]import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

[MENTION=439709]override[/MENTION]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

[MENTION=439709]override[/MENTION]
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}[/COLOR]

and add the following code:

[COLOR="blue"]import com.viewpagerindicator.PageIndicator;
import com.viewpagerindicator.TitlePageIndicator;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import com.example.viewpagertest.R;

public class MainActivity extends FragmentActivity {

FragmentAdapter mAdapter;
ViewPager mPager;
PageIndicator mIndicator;
int Number = 0;

[MENTION=439709]override[/MENTION]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mAdapter = new FragmentAdapter(getSupportFragmentManager());

mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter);

mIndicator = (TitlePageIndicator)findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);

}

[MENTION=439709]override[/MENTION]
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}[/COLOR]

The result:

package com.example.viewpagertest;

import com.viewpagerindicator.PageIndicator;
import com.viewpagerindicator.TitlePageIndicator;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import com.example.viewpagertest.R;

public class MainActivity extends FragmentActivity {

FragmentAdapter mAdapter;
ViewPager mPager;
PageIndicator mIndicator;
int Number = 0;

[MENTION=439709]override[/MENTION]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mAdapter = new FragmentAdapter(getSupportFragmentManager());

mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter);

mIndicator = (TitlePageIndicator)findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);

}

[MENTION=439709]override[/MENTION]
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

Now you have to create three fragments into src > com.example.viewpagertest folder:

- go to new > other:

​

and create three classes named "FirstActivity", "SecondActivity" and "ThirdActivity" like the picture below:

​

Now edit FirstActivity.java in src > com.example.viewpagertest folder and delete the red code:

[COLOR="Blue"]package com.example.viewpagertest;[/COLOR]

[COLOR="Red"]public class FragmentAdapter {

}[/COLOR]

and add the following code:

package com.example.viewpagertest;

[COLOR="Blue"]import com.example.viewpagertest.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FirstActivity extends Fragment {

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.layout1, null);

return v;

}
}
[/COLOR]

For the SecondActivity class:

package com.example.viewpagertest;

import com.example.viewpagertest.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class SecondActivity extends Fragment {

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.layout2, null);

return v;

}
}

For the ThirdActivity class:

package com.example.viewpagertest;

import com.example.viewpagertest.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ThirdActivity extends Fragment {

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.layout3, null);

return v;

}
}

Now we need to create a new class called "FragmentAdapter.java" into src > com.example.viewpagertest folder:

- go to new > other:

​

and create a new class called "FragmentAdapter" like the pictures:

​

click finish.

Now edit the FragmentAdapter.java in src > com.example.viewpagertest folder and delete the red code:

[COLOR="Blue"]package com.example.viewpagertest;[/COLOR]

[COLOR="Red"]public class FragmentAdapter {

}[/COLOR]

and add the following code:

package com.example.viewpagertest;

[COLOR="blue"]import com.viewpagerindicator.IconPagerAdapter;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatPagerAdapter;

public class FragmentAdapter extends FragmentStatPagerAdapter implements IconPagerAdapter{

public FragmentAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}

[MENTION=439709]override[/MENTION]
public int getIconResId(int index) {
// TODO Auto-generated method stub
return 0;
}

[MENTION=439709]override[/MENTION]
public Fragment getItem(int position)
{
// TODO Auto-generated method stub
Fragment fragment = new FirstActivity();
switch(position){
case 0:
fragment = new FirstActivity();
break;
case 1:
fragment = new SecondActivity();
break;
case 2:
fragment = new ThirdActivity();
break;
}
return fragment;
}

[MENTION=439709]override[/MENTION]
public int getCount() {
// TODO Auto-generated method stub
return 3;
}

[MENTION=439709]override[/MENTION]
public CharSequence getPageTitle(int position){
String title = "";
switch(position){
case 0:
title = "First";
break;
case 1:
title = "Second";
break;
case 2:
title = "Third";
break;
}
return title;
}

}[/COLOR]

Save the entire project! Enjoy your App!!!