Monday 18 September 2017

Tab layout




---------------------------tab.xml------------------------------------------



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Tab 1"
        android:textAppearance="?android:attr/textAppearanceLarge"/>
</RelativeLayout>









--------same as add tab1,tab2 xml create




--------------------Tab.java----------------

package com.httpabcdefapp1123.newdesignapp.Agencyclass;

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 com.httpabcdefapp1123.newdesignapp.R;

/** * Created by meenakshi on 9/18/2017. */
public class Active_user_caregiver extends Fragment {

    //Overriden method onCreateView  
  @Nullable 
   @Override   
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        //Returning the layout file after inflating        //Change R.layout.tab1 in you classes        return inflater.inflate(R.layout.active_user_caregiver, container, false);
    }
}





--------same as add tab1,tab2 java create



--------------pager adapter-----------------------------------



package com.httpabcdefapp1123.newdesignapp.AgencyAdapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

import com.httpabcdefapp1123.newdesignapp.Agencyclass.Active_user_caregiver;
import com.httpabcdefapp1123.newdesignapp.Agencyclass.Payment_history;
import com.httpabcdefapp1123.newdesignapp.Agencyclass.Cancelled;
import com.httpabcdefapp1123.newdesignapp.Agencyclass.Inactive;
import com.httpabcdefapp1123.newdesignapp.Agencyclass.Working;

/** * Created by meenakshi on 9/18/2017. */
public class Pager extends FragmentStatePagerAdapter {
    //integer to count number of tabs  
  int tabCount;

    //Constructor to the class   
 public Pager(FragmentManager fm, int tabCount) {
        super(fm);
        //Initializing tab count  
      this.tabCount= tabCount;
    }

    //Overriding method getItem  
  @Override
    public Fragment getItem(int position) {
        //Returning the current tabs   
     switch (position) {
            case 0:
                Tab tab = new Tab ();
                return tab1;
            case 1:
                Cancelled tab2 = new Cancelled();
                return tab2;
            case 2:
                Inactive tab3 = new Inactive();
                return tab3;
          
            default:
                return null;
        }
    }

    //Overriden method getCount to get the number of tabs   
 @Override   
 public int getCount() {
        return tabCount;
    }
}


------------activity_main.xml-------------------------------

<RelativeLayout 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:id="@+id/main_layout"   
 android:layout_width="match_parent" 
   android:layout_height="match_parent">

    <include layout="@layout/tool_bar" />
    <!-- our toolbar -->

    <!-- our tablayout to display tabs  -->   
 <android.support.design.widget.TabLayout   
     android:id="@+id/tabLayout"      
  android:layout_below="@+id/tool_bar"  
      android:layout_width="match_parent"
       android:layout_height="wrap_content" />

    <!-- View pager to swipe views -->  
  <android.support.v4.view.ViewPager      
  android:id="@+id/pager"     
   android:layout_below="@+id/tabLayout"
        android:layout_width="match_parent" 
       android:layout_height="fill_parent"    
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

</RelativeLayout>





-----AgencyfragmentActivity.java-----------


package com.httpabcdefapp1123.newdesignapp.Activity.AgencySide;

import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.os.Bundle;

import com.httpabcdefapp1123.newdesignapp.Activity.BaseActivity;
import com.httpabcdefapp1123.newdesignapp.AgencyAdapter.Pager;
import com.httpabcdefapp1123.newdesignapp.R;

public class AgencyfragmentActivity extends BaseActivity implements TabLayout.OnTabSelectedListener {

     TabLayout tabLayout;

     ViewPager viewPager;

    @Override 
   protected int getContentResId() {
        return R.layout.activity_agencyfragment;
    }

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setToolbarWithBackButton("SYZYGY");

        viewPager = (ViewPager) findViewById(R.id.pager);

        tabLayout = (TabLayout) findViewById(R.id.tabLayout);



        //Adding the tabs using addTab() method   
     tabLayout.addTab(tabLayout.newTab().setText("Payment History"));
        tabLayout.addTab(tabLayout.newTab().setText("Cancelled"));
        tabLayout.addTab(tabLayout.newTab().setText("Inactive"));
        tabLayout.addTab(tabLayout.newTab().setText("Working"));
        tabLayout.addTab(tabLayout.newTab().setText("Active User Caregiver"));
        tabLayout.setTabGravity(TabLayout.GRAVITY_CENTER);
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);


        Pager adapter = new Pager(getSupportFragmentManager(), tabLayout.getTabCount());
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

        //Adding adapter to pager   
     viewPager.setAdapter(adapter);

        //Adding onTabSelectedListener to swipe views 
       tabLayout.setOnTabSelectedListener(this);
    }

    @Override    public void onTabSelected(TabLayout.Tab tab) {

        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override    public void onTabReselected(TabLayout.Tab tab) {

    }
}




----------build.gradle(depenecy)-----------


compile 'com.android.support:design:25.3.1'


No comments:

Post a Comment

AutoComplete Address (Updated)

-------------------------------------Activity---------------------------------- package placeautocomplete.iteritory.com; import androi...