Android Swipe View with Tab layout – Example

You may also like...

  • Karthik Ramesh

    above code can you make it extends ListFragment

    • Raj Amal


      • Danny Moses

        Thanks for this. I been trying to learn android and java little by little trying to create an app. This example is awesome. I want to add a list of text to each page and change the page titles to match the contents but I’m stuck. Can you post an example of an array of text.

  • Ľuboš Mudrák

    I wonder. Is there way how to prevent constant calling of constructor of each fragments while swipping ? Only create them once and call that instance ?

  • Sylvina

    Hi, i’m working on tabsswipe project right now. But, i still don’t know how to put an expendables list view inside of my specific tab, or a button inside the tab which can go to other activity after i’ve clicked . Can you help me please?

    • Raj Amal

      It is similar to normal expendable listview, but you need to implement it in a fragment.

  • Cdn

    Hi there and thanks for this tutorial, do you know if it is possible to compile your project, so as to generate an.apk, without going through an IDE like Eclipse ?

  • Abrahim Neil

    Hey Raj nice code, much needed, please add one more functionality in existing code, to pick date and time, like this: (but this time for Fragment) and check my this issue:

  • tgrclw

    How to Theme.NoTitleBar?

    • Raj Amal

      Add this to your styles.xml file

  • Bek

    Hi Raj!
    How to start certain tab from another activity using simple button in your project? for example, to start with second tab.

  • Masha

    Heey. Great Tutorial. I have a gridview with images, when i click on one i use an adapter to pop it out onto an entire activity for viewing (like if i was to choose one). How do i swipe through the ‘popped out’ images?

  • Alex

    I need to make an activity with swipe tabs on a new window after I choose it on the menu. Can i use this code but change the names of activities and layouts?

  • Kaos1337

    There is a bug, at the launch if you log (Log.i(…)) the switch case of the tab adapter you will see that it will go from case 0 to 1 doing nothing.

  • caspecor

    how to insert this code in android tab for example?


    package com.example.multitouch;

    import java.util.ArrayList;

    import android.os.Bundle;


    import android.util.Log;

    import android.view.View;

    import android.view.Window;

    import android.view.WindowManager;

    import android.widget.AdapterView;

    import android.widget.AdapterView.OnItemClickListener;

    import android.widget.ListView;

    import android.widget.Toast;

    public class CajaActivity extends Activity {

    ListView userList;

    UserCustomAdapter userAdapter;

    ArrayList userArray = new ArrayList();


    protected void onCreate(Bundle savedInstanceState) {












    * add item in arraylist


    userArray.add(new Mesa(“Mesa 1″, “Total:”, “45.36 €”));

    userArray.add(new Mesa(“Mesa 2″, “Total:”, “22.68 €”));

    userArray.add(new Mesa(“Mesa 3″, “Total:”, “98.44 €”));

    userArray.add(new Mesa(“Mesa 4″, “Total:”, “12.14 €”));

    userArray.add(new Mesa(“Mesa 5″, “Total:”, “45.75 €”));

    userArray.add(new Mesa(“Mesa 6″, “Total:”, “56.85 €”));

    userArray.add(new Mesa(“Mesa 7″, “Total:”, “55.69 €”));


    * set item into adapter


    userAdapter = new UserCustomAdapter(CajaActivity.this, R.layout.row,


    userList = (ListView) findViewById(;




    * get on item click listener


    userList.setOnItemClickListener(new OnItemClickListener() {


    public void onItemClick(AdapterView parent, View v,

    final int position, long id) {

    Log.i(“List View Clickeado”, “**********”);


    “List View Clickeado:” + position, Toast.LENGTH_LONG)







  • Mohamed

    thank you.

  • Vikas

    Hi Raj
    Can you let me know how to display list of data where you are displaying Windows/IOS/Android

    • Raj Amal

      You can display list by extending the class to ListFragment.

      • Vikas

        Could you please add some example in your code.

  • Francesco

    Very helpful! Thank you very much!

  • Luis Ruiz Figueroa

    how can I choose which tab starts first?

    • Raj Amal


      Replace int with the tab number.

      • Luis Ruiz Figueroa

        pudiste hacerlo con fragment?

  • David

    Thank you, it was very helpful..

  • Hitesh

    how should i add spinners in these tab layout ?

    • Raj Amal

      For windows fragment you should add spinner like this.
      Spinner spinner =(Spinner)windows.findViewById(;

      • Hitesh

        Done thanks a lot !!

  • Reinder Rae

    Thanks for your code it works really good but I have a quetion if you could help me with it, I wrote a code to call a method from tab1 to open a “fragment2″ but for somereason I get a white screen, could you tell me why if you know the cause?
    this code is at MainActivity

    public void openNewsCont() {

    fragment2 nc = new fragment2(); fm = getSupportFragmentManager(); ft = fm.beginTransaction();
    ft.add(, nc);


    • ChaturaM

      if you want to go from tab 1 to tab two use following code


  • Steve

    same coding in android hive.

    • dan

      slightly different implementation

  • Hit Chikani

    very nic example :)

  • dipak

    awesome it works!!, simple, easy and straightforward to follow… if all tutorials were like this, it would making learn so fun :-) thank you…

  • Ibn Shajahan

    how to call this fragment activity from navigation slider?

  • Mounzer Orabi Agha

    Good Tutorial.. but how i can change the indicator color ( the blue color ) to custom color ? thanks you

  • Samad Moiz

    How can i add more tabs

  • Maitray Shah

    How can I implement an expandable listview in one of those tabs instead of just simple text..??

  • Venkat Ramarao Potlapalli

    Hey raj… Thats a very good tutorial. But i want to add button setOnClickListener in this tabs which i’m unable to do so, can you help me out of it. Thank you in advance :) :)

  • jmz3

    Really good tutorial, helped me understand the workflow. I tried this in another way by using the ‘SectionsPagerAdapter

  • Fahad

    Great work dude

  • Guest

    Am unable to use getSupportFragmentManager()

    Error =The constructor TabPagerAdapter(FragmentManager) is undefined;

  • Syarif Ibnu Miftahudin Al-Dami

    Can i hide or remove the actionBar and just display the tabs?

  • Moises Alexander Salazar Vila

    Great tutorial! This helps me a lot !

  • Pandit

    hey Raj,
    There is problem with fragment, When I am opening first tab then second tab’s operations are performed in background but UI is of First Tab.

  • Lucas

    quality code, puts everything else to shame

  • زبیر احمد

    how set tab to bottom plz help

  • shreya

    Im really stuck at adding support libraries and updating build.gradle with dependencies for v4 libs. Do you have a tut for that? or a step by step guide. Im using Android Studio

  • Avashiva

    Hi really nice tutorial works really good, but one question how can I do for seeing my ActionBar and under it the tabs?
    I added:
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(, menu);
    return true;
    But the menu tabs are on the actionbar and my menu is on overflow


  • Puneet

    How can I start another activity inside the same tab? e.g.from I want to start another activity under Windows tab. Is it possible to do so? If yes, how can I do that? I am able to start another activity but it does not come within same tab but it removes the tabs layout.

  • Jaydeep Goyani

    nice work

  • Tim

    How about creating this project for API’s 11 and above ? What will be the difference ?? And how to get the ViewPager element ?

  • Linear Agressor

    You are a fuc*ing life save man !!!

  • Duy Kien Ngo

    Nice Tutorial, however what should I do if I want to put tab layout in the bottom of screen ?

  • Luba

    I keep on getting this error java.lang.RuntimeException: Unable to start activity ComponentInfo{}: java.lang.NullPointerException: Attempt to invoke virtual method ‘void’ on a null object reference

  • Ramal

    It looks like you are missing the TextView declaration in each of your fragments you have:

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    View android = inflater.inflate(R.layout.android_frag, container, false);
    return android;

    It should say:

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    View android = inflater.inflate(R.layout.android_frag, container, false);
    TextView androidTextView = (TextView)android.findViewById(“Android”);
    return android;

    ((TextView)android.findViewById(“Android”); is an assignment without a declaration.

  • Vipin

    Raj Amal,
    I wanted different tabs. And in each tab i have some text fields and button. But button click wont fire inside tab. Works fine without tab. Any idea whats wrong or could you direct me to some examples of using onclicklistener inside tab. Couldnt find one. Thanks in Advance

  • parniaznet

    how can i change tabs height?

  • Bruno Nogueira Silva

    how to send bundle to fragment

  • sky

    how to intent method for any button in android ios and widows pages.??

    • sky


  • BB

    If i place a button in windows and need to access new fragment called Blackberry by clicking it , what would be the way.