main.xml layout
<?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>
create new layout name footer.xml
<?xml version="1.0" encoding="utf-8"?>
<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 layout under drawable folder name rounded_celll.xml
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android">
<shape android:shape="rectangle">
<gradient
android:startColor="@color/white"
android:endColor="@color/white"
android:angle="0"/>
<corners android:bottomLeftRadius="10dp" android:topLeftRadius="10dp" android:topRightRadius="10dp" android:bottomRightRadius="10dp"/>
<stroke android:color="@color/l_blue" android:width="1dp" />
</shape>
</inset>
create layout 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 layout name 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>
put below code in cololr.xml file
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="white">#FFF</color>
<color name="l_blue">#909BAA</color>
<color name="d_blue">#6D7B92</color>
<color name="gray">#CCCCCC</color>
<color name="d_gray">#666</color>
<color name="black">#000</color>
</resources>
put below code in strings.xml
<string name="app_name">ViewPagerStyle1</string>
<string name="indicator_height">3dip</string>
put below code in styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme" parent="android:Theme">
<item name="android:background">@null</item>
<item name="android:windowNoTitle">true</item>
</style>
<style name="text_title">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">@color/d_gray</item>
<item name="android:gravity">center</item>
</style>
<style name="text_option">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">@color/black</item>
</style>
<style name="layout_wrap">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
<style name="layout_fill">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">fill_parent</item>
</style>
<style name="layout_f_w">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
</style>
<style name="h_line">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">1.5dip</item>
<item name="android:background">@color/l_blue</item>
</style>
<style name="indicator_style">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">4dip</item>
<item name="android:background">@color/l_blue</item>
<item name="android:orientation">vertical</item>
</style>
</resources>
create class name 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 adapter name 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 class name 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 class name LayoutOne.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 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;
}
}
<?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>
create new layout name footer.xml
<?xml version="1.0" encoding="utf-8"?>
<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 layout under drawable folder name rounded_celll.xml
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android">
<shape android:shape="rectangle">
<gradient
android:startColor="@color/white"
android:endColor="@color/white"
android:angle="0"/>
<corners android:bottomLeftRadius="10dp" android:topLeftRadius="10dp" android:topRightRadius="10dp" android:bottomRightRadius="10dp"/>
<stroke android:color="@color/l_blue" android:width="1dp" />
</shape>
</inset>
create layout 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 layout name 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>
put below code in cololr.xml file
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="white">#FFF</color>
<color name="l_blue">#909BAA</color>
<color name="d_blue">#6D7B92</color>
<color name="gray">#CCCCCC</color>
<color name="d_gray">#666</color>
<color name="black">#000</color>
</resources>
put below code in strings.xml
<string name="app_name">ViewPagerStyle1</string>
<string name="indicator_height">3dip</string>
put below code in styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme" parent="android:Theme">
<item name="android:background">@null</item>
<item name="android:windowNoTitle">true</item>
</style>
<style name="text_title">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">@color/d_gray</item>
<item name="android:gravity">center</item>
</style>
<style name="text_option">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">@color/black</item>
</style>
<style name="layout_wrap">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
<style name="layout_fill">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">fill_parent</item>
</style>
<style name="layout_f_w">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
</style>
<style name="h_line">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">1.5dip</item>
<item name="android:background">@color/l_blue</item>
</style>
<style name="indicator_style">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">4dip</item>
<item name="android:background">@color/l_blue</item>
<item name="android:orientation">vertical</item>
</style>
</resources>
create class name 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 adapter name 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 class name 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 class name LayoutOne.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 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;
}
}
No comments:
Post a Comment
Comments