Android Swipe View with Tab layout – Example

You may also like...

  • Karthik Ramesh

    above code can you make it extends ListFragment

    • http://128.199.224.11/ Raj Amal

      yes

      • 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?

    • http://128.199.224.11/ 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: http://www.learn2crack.com/2013/11/android-timepicker-datepicker-example.html (but this time for Fragment) and check my this issue: http://stackoverflow.com/questions/21821267/how-to-set-selected-dialog-values-to-textviews-in-fragment

  • tgrclw

    How to Theme.NoTitleBar?

    • http://128.199.224.11/ 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.app.Activity;

    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();

    @Override

    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.activity_caja);

    /*

    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.activity_login);

    */

    /**

    * 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,

    userArray);

    userList = (ListView) findViewById(R.id.listView);

    userList.setItemsCanFocus(false);

    userList.setAdapter(userAdapter);

    /**

    * get on item click listener

    */

    userList.setOnItemClickListener(new OnItemClickListener() {

    @Override

    public void onItemClick(AdapterView parent, View v,

    final int position, long id) {

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

    Toast.makeText(CajaActivity.this,

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

    .show();

    }

    });

    }

    }

    //////////////////////////////////

  • Mohamed

    thank you.

  • Vikas

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

    • http://www.learn2crack.com/ 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?

    • http://www.learn2crack.com/ Raj Amal

      Tab.setCurrentItem(int);

      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 ?

    • http://www.learn2crack.com/ Raj Amal

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

      • 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();

    android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
    android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
    ft.add(R.id.pager, nc);
    ft.addToBackStack(null);
    ft.commit();

    }

    • ChaturaM

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

      getActivity().getActionBar().setSelectedNavigationItem(1);

  • 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…..now if all tutorials were like this, it would making learn so fun :-) thank you…

  • http://lifeissimplela.blogspot.com/ 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

  • http://sm-giml.blogspot.com/ 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(R.menu.main, menu);
    return true;
    }
    But the menu tabs are on the actionbar and my menu is on overflow

    Thanks