In Previous ViewPager tutorial I created the tab indicator with ViewPager. Today I am going to show you how to crate the ViewPager Circle style.
Get full code from "https://github.com/manishkpr/AndroidViewPagerStyle2"
crete file LayoutOne.java
===============
import android.content.Context;
public class LayoutOne extends Fragment {
public static Fragment newInstance(Context context) {
LayoutOne f = new LayoutOne();
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.layout_one, null);
return root;
}
}
create file LayoutTwo.java
================
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class LayoutTwo extends Fragment {
public static Fragment newInstance(Context context) {
LayoutTwo f = new LayoutTwo();
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.layout_two, null);
return root;
}
}
create file ViewPagerAdapter.java
=======================
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class ViewPagerAdapter extends FragmentPagerAdapter {
private Context _context;
public static int totalPage=2;
public ViewPagerAdapter(Context context, FragmentManager fm) {
super(fm);
_context=context;
}
@Override
public Fragment getItem(int position) {
Fragment f = new Fragment();
switch(position){
case 0:
f=LayoutOne.newInstance(_context);
break;
case 1:
f=LayoutTwo.newInstance(_context);
break;
}
return f;
}
@Override
public int getCount() {
return totalPage;
}
}
create file ViewPagerStyle1Activity.java
==========================
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.widget.Button;
public class ViewPagerStyle1Activity extends FragmentActivity {
private ViewPager _mViewPager;
private ViewPagerAdapter _adapter;
private Button _btn1,_btn2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setUpView();
setTab();
}
private void setUpView(){
_mViewPager = (ViewPager) findViewById(R.id.viewPager);
_adapter = new ViewPagerAdapter(getApplicationContext(),getSupportFragmentManager());
_mViewPager.setAdapter(_adapter);
_mViewPager.setCurrentItem(0);
initButton();
}
private void setTab(){
_mViewPager.setOnPageChangeListener(new OnPageChangeListener(){
@Override
public void onPageScrollStateChanged(int position) {}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {}
@Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
btnAction(position);
}
});
}
private void btnAction(int action){
switch(action){
case 0: setButton(_btn1,"1",40,40); setButton(_btn2,"",20,20);break;
case 1: setButton(_btn2,"2",40,40); setButton(_btn1,"",20,20); break;
}
}
private void initButton(){
_btn1=(Button)findViewById(R.id.btn1);
_btn2=(Button)findViewById(R.id.btn2);
setButton(_btn1,"1",40,40);
setButton(_btn2,"",20,20);
}
private void setButton(Button btn,String text,int h, int w){
btn.setWidth(w);
btn.setHeight(h);
btn.setText(text);
}
}
create xml file named footer.xml
=====================
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="15dip"
android:background="@color/d_blue"
android:layout_gravity="bottom"
android:orientation="vertical" >
<Button android:layout_height="wrap_content" android:layout_width="wrap_content"
android:id="@+id/btn1"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/rounded_celll" />
<Button android:layout_height="wrap_content" android:layout_width="wrap_content"
android:id="@+id/btn2"
android:layout_toRightOf="@id/btn1"
android:layout_marginLeft="5dip"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/rounded_celll" />
</RelativeLayout>
create new xml file name layout_one.xml
==========================
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
android:gravity="center|center"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Layout" />
</LinearLayout>
create another file named layout_two.xml
==========================
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
android:gravity="center|center"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second Layout" />
</LinearLayout>
create xml named main.xml
==================
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
android:orientation="vertical" >
<!-- ViewPager -->
<android.support.v4.view.ViewPager
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:id="@+id/viewPager" />
<!-- Footer -->
<include layout="@layout/footer" />
</FrameLayout>
manifest.xml
==========
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.manishkpr.viewpager"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" android:theme="@style/Theme">
<activity
android:name=".ViewPagerStyle1Activity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Get full code from "https://github.com/manishkpr/AndroidViewPagerStyle2"
crete file LayoutOne.java
===============
import android.content.Context;
public class LayoutOne extends Fragment {
public static Fragment newInstance(Context context) {
LayoutOne f = new LayoutOne();
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.layout_one, null);
return root;
}
}
create file LayoutTwo.java
================
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class LayoutTwo extends Fragment {
public static Fragment newInstance(Context context) {
LayoutTwo f = new LayoutTwo();
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.layout_two, null);
return root;
}
}
create file ViewPagerAdapter.java
=======================
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class ViewPagerAdapter extends FragmentPagerAdapter {
private Context _context;
public static int totalPage=2;
public ViewPagerAdapter(Context context, FragmentManager fm) {
super(fm);
_context=context;
}
@Override
public Fragment getItem(int position) {
Fragment f = new Fragment();
switch(position){
case 0:
f=LayoutOne.newInstance(_context);
break;
case 1:
f=LayoutTwo.newInstance(_context);
break;
}
return f;
}
@Override
public int getCount() {
return totalPage;
}
}
create file ViewPagerStyle1Activity.java
==========================
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.widget.Button;
public class ViewPagerStyle1Activity extends FragmentActivity {
private ViewPager _mViewPager;
private ViewPagerAdapter _adapter;
private Button _btn1,_btn2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setUpView();
setTab();
}
private void setUpView(){
_mViewPager = (ViewPager) findViewById(R.id.viewPager);
_adapter = new ViewPagerAdapter(getApplicationContext(),getSupportFragmentManager());
_mViewPager.setAdapter(_adapter);
_mViewPager.setCurrentItem(0);
initButton();
}
private void setTab(){
_mViewPager.setOnPageChangeListener(new OnPageChangeListener(){
@Override
public void onPageScrollStateChanged(int position) {}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {}
@Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
btnAction(position);
}
});
}
private void btnAction(int action){
switch(action){
case 0: setButton(_btn1,"1",40,40); setButton(_btn2,"",20,20);break;
case 1: setButton(_btn2,"2",40,40); setButton(_btn1,"",20,20); break;
}
}
private void initButton(){
_btn1=(Button)findViewById(R.id.btn1);
_btn2=(Button)findViewById(R.id.btn2);
setButton(_btn1,"1",40,40);
setButton(_btn2,"",20,20);
}
private void setButton(Button btn,String text,int h, int w){
btn.setWidth(w);
btn.setHeight(h);
btn.setText(text);
}
}
create xml file named footer.xml
=====================
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="15dip"
android:background="@color/d_blue"
android:layout_gravity="bottom"
android:orientation="vertical" >
<Button android:layout_height="wrap_content" android:layout_width="wrap_content"
android:id="@+id/btn1"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/rounded_celll" />
<Button android:layout_height="wrap_content" android:layout_width="wrap_content"
android:id="@+id/btn2"
android:layout_toRightOf="@id/btn1"
android:layout_marginLeft="5dip"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/rounded_celll" />
</RelativeLayout>
create new xml file name layout_one.xml
==========================
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
android:gravity="center|center"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Layout" />
</LinearLayout>
create another file named layout_two.xml
==========================
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
android:gravity="center|center"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second Layout" />
</LinearLayout>
create xml named main.xml
==================
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
android:orientation="vertical" >
<!-- ViewPager -->
<android.support.v4.view.ViewPager
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:id="@+id/viewPager" />
<!-- Footer -->
<include layout="@layout/footer" />
</FrameLayout>
manifest.xml
==========
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.manishkpr.viewpager"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" android:theme="@style/Theme">
<activity
android:name=".ViewPagerStyle1Activity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
how can i change the color of the circles?
ReplyDeletei tried this: btn2.setBackgroundColor(getResources().getColor(R.color.dark_blue));
but now there is a square instead of a circle.
Can you help me?
Que linda mami!!!!!
Deletevery nice example ....i liked it
ReplyDelete