Android Combining Tab Layout and List View

In Android Tab Layout Tutorial i explained how to implement a tab view. This tutorial is about implementing list view inside a tab layout. Everything is same except tabs will have listview inside it. List data is displayed by fetching json by making http request. I took an example of simple mailbox which will contains two list views for inbox, outbox messages and a profile tab.

In this example i am displaying list view in each tab where the list data is fetched from a JSON.

6. Each and every tab needs an icon so design icons for each tab. We need three dimensions of each icon. Design each icon in 48 x 48 px, 32 x 32 px and 24 x 24 px and place them in drawable-hdpi, drawable-mdpi and drawable-ldpi respectively. See following diagram for your guidance

7. Android icon states will be define in xml files with default and hover state configurations. For three icons we need the icon state configuration files. So create three 3 xml files under drawable-hdpi directory. Type the following code for icon states.

9. Now open your project make sure that you an entry of new activity name in AndroidManifest.xml file. Open you AndroidManifest.xml file and modify the code as below. Also don’t forgot to add INTERNET Permissions as we are getting JSON by making http request.

10. If you run your project now you will see tabs running without listviews. So all we need to do is implement listview in each tab activity. So open you individual tab activity classes and try to implement listview. If you are new to ListView i suggest you to go through Android ListView Tutorial once.

Hi there! I am Founder at androidhive and programming enthusiast. My skills includes Android, iOS, PHP, Ruby on Rails and lot more. If you have any idea that you would want me to develop? Let’s talk: ravi@androidhive.info

Sir, i want to know that if i click on any item from the list view and one child activity opens but tabs remain on the same position. how can we handle this problem?

Labidi4

Nice Tut, but when i add OnItemClickListener to start a new activity so i can show some details about the clicked item i loose the TabHost , is there a way i can start a new activity within the tabhost ?

gandos

you can’t start a new activity inside a tabview. instead you could replace the content by calling setContentView. i’ve tried this and work

Guest

Can you give an example. I tried setContentView with an intent but it did nothing. I was able to do as the person said above or switch the tab but not stay on the same tab and switch out the content section.

Zeus

can u give an eg for setContentView

netX

your tutorial works great but is there anyway i can link this project with “Android Login and Registration with PHP, MySQL and SQLite” project. i will like to use the registration info to create the profile data for profile.json

James

Nice write up!

Andrei B.

Actually, no. You can use the later SDKs (which include fragments) and still compile for 2.3. I’ve done it, there’s no trick to it.

I think it also works for 2.2. I remember I compiled for that version also a while back.

anon

And the fragment way totally blows. Way more work, more files and not nearly as concise. I can see why so many developers have a gripe with android development.

Thorsten

Ever worked with fragments and the support library? Import the support library in Eclipse (from the ADT project menu) or just start a new Android project in Eclipse/Android Studio (IntelliJ) and start coding. Works with Android 2.1 (SDK) and higher (if you still want to support SDK 7 or 8).http://developer.android.com/training/basics/fragments/creating.html

Hi all. I am a novice in developing app and thanks to
Ravi for all good work that is doing . I’m working at a simple app and I get
stuck . if you have knowledge of
building android apps, and make team
with me please replay to george.celsie@gmail.com . Thanks ang
good luck for all your projects..

Cuong Rio

Many thanks

Praveen

Thanks for this good tutorial. It really helped me.
I have a question:
Can we add swipe feature to this app? If yes please explain that…

Puneet

How do I start another activity from say ProfileActivity.java within same tab (Profile in my example)?

The app is gr8..the logic and everything…however in my case its not going beyond “Loading Inbox” n then it force closes…i think may be its about internet proxy settings…can u elaborate on setting proxy settings here…

ak

please How can I add onclick listener to the Listadapter. pls and pls. respond. I know this project is old. need that ans like air.

meme

please if u know how apply onclick listener tell me 🙁

reza

hi i need to combine grideviw combining with swipe tabhost layout can you help me.? tx reza

Gusti Bimo

What if i want to show listVIew in tab layout using fragment not tabActivity, how to implement listview in fragment?

Aditya Mehta

I also want to know the same if you find the answer please let me know too

Aditya Mehta

I have implement tabs using fragement. How I can handle listview event in that?

how to generate this model of array by selecting values from mysql database using select statement and send it in JSON response??

kavya shree

hi ravi sir, i m doing android project and its “Greeting sending app”… can i get the source code for customizing greetind card..

andhy fauzi

thx bro.. this is so help me

Kranthi kumar

Thank you so much and it is very helpful for me.

Sanyog Nishandar

hi sir , i want to create tabs in fragments and then attach it another main project as my work is to design front end.. i have 2 different layouts , can i use recycler for it ?

Aditya

Hi Ravi, i have created Gmail and Facebook Login and i fetched data like profile image and email and name to another activity using shared preferences now i need show that data into Tab Activity , i’m confused now where to use shared preferences to display both in Fragments, plzzzzzzz Help soon…