Skip to main content

Simple View Pager using Fragment in Android Studio | Slide Navigation










  • In such case you have seen the slide in application while install it first time which will guide you how to use the particular application.


  • Here is the small example which can be useful to you as a start up application.

  • You just have to replace my Fragment Buttons to your image that's it.



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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/rl_pager"
    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"
    android:background="@android:color/holo_orange_dark"
    tools:context="com.hardik.viewpager.MainActivity">

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true"/>


</RelativeLayout>

fragment_one.xml


<FrameLayout 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:background="@android:color/holo_orange_dark"
    tools:context="com.hardik.viewpager.Fragment_One">

    <Button
        android:id="@+id/frag_rel_1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textSize="100dp"
        android:background="@android:color/holo_orange_dark"
        android:textColor="@android:color/white"
        android:text="1" />
</FrameLayout>

fragment_two.xml

<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:background="@android:color/holo_orange_dark"
    tools:context="com.hardik.viewpager.Fragment_Two">

    <Button
        android:id="@+id/frag_rel_2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textSize="100dp"
        android:background="@android:color/holo_orange_dark"
        android:textColor="@android:color/white"
        android:text="2" />

</RelativeLayout>

fragment_three.xml


<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:background="@android:color/holo_orange_dark"
    tools:context="com.hardik.viewpager.Fragment_Three">

    <Button
        android:id="@+id/frag_rel_3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="3"
        android:background="@android:color/holo_orange_dark"
        android:gravity="center"
        android:textSize="100dp"
        android:textColor="@android:color/white"
        />
</RelativeLayout>


MainActivity.java

package com.hardik.viewpager;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TableLayout;
import android.support.design.widget.TabLayout;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {
    private ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = (ViewPager) findViewById(R.id.pager);
        PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(pagerAdapter);


    }
}


PageAdapter.java


package com.hardik.viewpager;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.Log;


public class PagerAdapter extends FragmentPagerAdapter {

    FragmentManager fragmentManager;
    Fragment fragment;


    public PagerAdapter(FragmentManager fm) {
        super(fm);
        this.fragmentManager = fm;
        this.fragment = fragment;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new Fragment_One();
            case 1:
                return new Fragment_Two();
            case 2:
                return new Fragment_Three();
        }
        return null;
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    @Override
    public int getCount() {
        return 3;
    }
}









Comments

  1. A motivating discussion is worth comment. I believe that you need to publish more about this subject matter, it may not be a taboo matter but generally folks don't speak about such subjects. To the next! Cheers!!
    Tech news

    ReplyDelete

Post a Comment