Thursday, 8 March 2018

ANDROID TAB LAYOUT Swipe Views WITH FRAGMENT USE TAB ADAPTER ARRAY LIST & COUNTER


TAB LAYOUT IN ANDROID 

EXAMPLE YOU TUBE , AND MORE APPLICATION USE TAB VIEW



Swipe Views. You can create swipe views in your app using the ViewPager widget, available in the STUDIO. The ViewPager is a layout widget in which each child view is a separate page (a separate tab) in the layout ALSO  known as tab layout.




for make tab layout us adapter class and view pager

each and every tab could be define different activity load or fragment also use in tab 



now we started with create a new activity 
  1.  Main Activity.xml
  2. Main Activity.java
  3. fst_fregment_layout.xml
  4. fst_fregment_layout.java
  5. snd_fragment_layout.xml
  6. snd_fragment_layout.java
  7. tab_adapter.java


1) Main Activity.xml



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.store.tab_layout_app.MainActivity">




    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        >

    </android.support.design.widget.TabLayout>
    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>


2) MainActivity.java




package com.example.store.tab_layout_app;

import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TableLayout;

public class MainActivity extends AppCompatActivity {

    TabLayout tab_layout1 ;
    ViewPager view_pager ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tab_layout1 = (TabLayout)findViewById(R.id.tab_layout1);
        view_pager =(ViewPager)findViewById(R.id.view_pager);


        //for counter fregment
        //view_pager.setAdapter(new tab_adapter(MainActivity.this,getSupportFragmentManager()));
        //tab_layout1.setupWithViewPager(view_pager);


        tab_adapter tb = new tab_adapter (MainActivity.this,getSupportFragmentManager());
        tb.addFragment(new fst_fregment_layout(),"tab one");
        tb.addFragment(new snd_fragment_layout(),"tab two");
        tb.addFragment(new fst_fregment_layout(),"tab three");

        view_pager.setAdapter(tb);
        tab_layout1.setupWithViewPager(view_pager);

    }
}


3) fst_fregment_layout.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:gravity="center"
    android:orientation="vertical"
    android:descendantFocusability="blocksDescendants">

    <TextView
        android:id="@+id/tv3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="TextView" />


</LinearLayout>


4)fst_fregment_layout.java



package com.example.store.tab_layout_app;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * Created by store on 28-02-2018.
 */

public class fst_fregment_layout extends Fragment {

    String data ="";

    TextView tv3;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fst_fregment_layout,container,false);
        tv3 = (TextView)rootView.findViewById(R.id.tv3);

        //data=getArguments().getString("name");
        tv3.setText("fst Fragment");

        return rootView;
    }
}




5)snd_fragment_layout.xml



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:gravity="center"
    android:orientation="vertical"
    android:descendantFocusability="blocksDescendants">

    <TextView
        android:id="@+id/tv3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="TextView" />


</LinearLayout>


6)snd_fragment_layout.java



package com.example.store.tab_layout_app;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * Created by store on 28-02-2018.
 */

public class snd_fragment_layout extends Fragment {

    String data ="";

    TextView tv3;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.snd_fragment_layout,container,false);
        tv3 = (TextView)rootView.findViewById(R.id.tv3);

        //data=getArguments().getString("name");
        tv3.setText("snd Fragment");

        return rootView;
    }
}



7)tab_adapter.java

package com.example.store.tab_layout_app;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by store on 07-03-2018.
 */

public class tab_adapter extends FragmentPagerAdapter {

    private Context mContext;

    List<Fragment> fragment_list = new ArrayList<>();
    List<String> title_list = new ArrayList<>();

    public tab_adapter(Context context, FragmentManager fm) {
        super(fm);
        mContext = context;


    }
    public void  addFragment ( Fragment fragment , String title)
    {
        fragment_list.add(fragment);
        title_list.add(title);

    }


    @Override
    public Fragment getItem(int position) {

        return fragment_list.get(position);
        // This determines the fragment for each tab

       /* if (position == 0) {
            return new fst_fregment_layout();
        } else if (position == 1){
            return new snd_fragment_layout();
        } else {
            return new fst_fregment_layout();
        }*/
    }

    // This determines the number of tabs
    @Override
    public int getCount() {


        return fragment_list.size();
    }

    // This determines the title for each tab
    @Override
    public CharSequence getPageTitle(int position) {
        // Generate title based on item position

        return title_list.get(position);
        /* switch (position) {
            case 0:
                return "tab1";
            case 1:
                return "tab2";
            default:
                return null;
        }*/
    }

}


video preview for that activity:-





thank you

No comments:

Post a Comment

B Remi Calculator

Loan Calculator Loan Calculator Loan Amount PF Cross Sell (LI % GI) No Yes EMI CARD No Ye...