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

Popular posts from this blog

Sticky Header Smooth Recycler view in Android

> Here is the example of Sticky Header view in recycler view with smooth scroll > Sticky header is a View over recycler view that will Stick on header of recycler view and shows some information > In this example i have filtered out the alphabets that will stick on header of recycler view. > Follow the following Steps to make a Recycler View with Sticky Header. > Here arrays.xml contains some dummy names so we can show up a demo. > Run this application and see the sticky header in your screen. > You can see the output in following video

Tri-States | Indeterminate Checkbox Android

Sometimes it is required to implement checkbox with 3 states Unchecked Checked Indeterminate So here is a simple example of how to implement Tri-States or you can say Indeterminate Checkbox Android First of all, make a custom class which is extend Checkbox import android.content.Context import android.util.AttributeSet import androidx.appcompat.widget.AppCompatCheckBox import com.android.tristatescheckbox.R class TriStatesCheckBox : AppCompatCheckBox { private var state = 0 constructor ( context: Context ?) : super ( context ) { init () } constructor ( context: Context ?, attrs: AttributeSet ?) : super ( context , attrs ) { init () } constructor ( context: Context ?, attrs: AttributeSet ?, defStyleAttr: Int ) : super ( context , attrs , defStyleAttr ) { init () } private fun init () { state = UNCHECKED...

API Level in Android

Android Version Released API Level Name Build Version Code Android 7.1 December 2016 25 Nougat Android.OS.BuildVersionCodes.NMr1 Android 7.0 August 2016 24 Nougat Android.OS.BuildVersionCodes.N Android 6.0 August 2015 23 Marshmallow Android.OS.BuildVersionCodes.M Android 5.1 March 2015 22 Lollipop Android.OS.BuildVersionCodes.LollipopMr1 Android 5.0 November 2014 21 Lollipop Android.OS.BuildVersionCodes.Lollipop Android 4.4W June 2014 20 Kitkat Watch Android.OS.Bui...