title: How to add ListView and tab bar in android
body: <p>I want the list view and tab bar both at the same time and my code is below:</p>
  
  <p>Please check my Main Activity and Custom Adapter and tell me what changes I need to do.
  Because list is working but tabs are not being displayed.</p>
  
  <p>Two XML files I have included.
  1.tab_bar
  2.final_tab_item</p>
  
  <p>Please let me know where I am going wrong...</p>
  
  <pre><code> MainActivity.java
  
  
  
   package com.example.tabwithlist;
  
  
  
      import java.util.ArrayList;
  
      import com.example.customlist.CustomAdapter;
      import com.example.customlist.ListModel;
  
      import android.app.TabActivity;
      import android.content.Intent;
      import android.content.res.Resources;
      import android.os.Bundle;
      import android.util.Log;
      import android.view.Menu;
      import android.view.MenuItem;
      import android.widget.ListView;
      import android.widget.TabHost;
      import android.widget.TabHost.OnTabChangeListener;
  
      public class MainActivity extends TabActivity implements OnTabChangeListener 
  
      {
          ListView list;
          CustomAdapter adapter;
  
          public  ArrayList&lt;ListModel&gt; CustomListViewValuesArr = new ArrayList&lt;ListModel&gt;();
  
          /** Called when the activity is first created. */
            TabHost tabHost;
  
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.tab_bar);
             //   setContentView(R.layout.custom_list_view);
  
               setListData();
  
                  Resources res =getResources(); 
                  list=(ListView)findViewById(R.id.listnew);
  
                  //**************** Create Custom Adapter *********//*
                  adapter=new CustomAdapter(this, CustomListViewValuesArr,res);
                  list.setAdapter(adapter);
  
  
                  for (int i = 0; i &lt; 11; i++) {
  
                      final ListModel sched = new ListModel();
  
                       //******* Firstly take data in model object ******//*
                         sched.setCompanyName("Company "+i);
                         sched.setImage("image"+i);
                         sched.setUrl("http:\\\\www."+i+".com");
  
                      //******** Take Model Object in ArrayList **********//*
                      CustomListViewValuesArr.add(sched);
                  }
  
             //    Get TabHost Refference
                tabHost = getTabHost();
  
                // Set TabChangeListener called when tab changed
                tabHost.setOnTabChangedListener(this);
  
                TabHost.TabSpec spec;
                Intent intent;
                //************* TAB1 ************//*
                // Create  Intents to launch an Activity for the tab (to be reused)
                intent = new Intent().setClass(this, Tab1.class);
                spec = tabHost.newTabSpec("First").setIndicator("")
                              .setContent(intent);
  
                //Add intent to tab
                tabHost.addTab(spec);
  
                //************* TAB2 ************//*
                intent = new Intent().setClass(this, Tab2.class);
                spec = tabHost.newTabSpec("Second").setIndicator("")
                              .setContent(intent);  
                tabHost.addTab(spec);
  
                /************* TAB3 ************/
                intent = new Intent().setClass(this, Tab3.class);
                spec = tabHost.newTabSpec("Third").setIndicator("")
                              .setContent(intent);
                tabHost.addTab(spec);
  
                // Set drawable images to tab
                tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.tab2);
                tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.tab3);
  
                // Set Tab1 as Default tab and change image   
                tabHost.getTabWidget().setCurrentTab(0);
                tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tab1_over);
  
  
             }
  
        private void setListData() {
  
              for (int i = 0; i &lt; 11; i++) {
  
                  final ListModel sched = new ListModel();
  
                    /******* Firstly take data in model object ******/
                     sched.setCompanyName("Company "+i);
                     sched.setImage("image"+i);
                     sched.setUrl("http:\\\\www."+i+".com");
  
                  /******** Take Model Object in ArrayList **********/
                  CustomListViewValuesArr.add(sched);
              }
  
          }
  
  
      @Override
  
          public void onTabChanged(String tabId) {
  
          /************ Called when tab changed *************/
  
              //********* Check current selected tab and change according images *******/
  
              for(int i=0;i&lt;tabHost.getTabWidget().getChildCount();i++)
              {
                  if(i==0)
                      tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.tab1);
                  else if(i==1)
                      tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.tab2);
                  else if(i==2)
                      tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.tab3);
              }
  
  
              Log.i("tabs", "CurrentTab: "+tabHost.getCurrentTab());
  
              if(tabHost.getCurrentTab()==0)
                  tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.tab1_over);
              else if(tabHost.getCurrentTab()==1)
                  tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.tab2_over);
              else if(tabHost.getCurrentTab()==2)
                  tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.tab3_over);
  
          }
      }
  
  
      CustomAdapter.java(Here I am inflating my final_tab_item.xml file which displays how list view will look like.)
  
      package com.example.customlist;
  
      import java.util.ArrayList;
  
      import android.R;
      import android.app.Activity;
      import android.content.Context;
      import android.content.res.Resources;
      import android.view.LayoutInflater;
      //import android.view.LayoutInflater;
      import android.view.View;
      import android.view.ViewGroup;
      import android.widget.BaseAdapter;
      import android.widget.ImageView;
      import android.widget.TextView;
  
      public class CustomAdapter extends BaseAdapter {
  
  
  
          /*********** Declare Used Variables *********/
  
          private Activity activity;
          private ArrayList data;
          private static LayoutInflater inflater=null;
          public Resources res;
          ListModel tempValues=null;
          int i=0;
  
          /*************  CustomAdapter Constructor *****************/
          public CustomAdapter(Activity a, ArrayList d,Resources resLocal) {
  
              /********** Take passed values **********/
              activity = a;
              data=d;
              res = resLocal;
  
              /***********  Layout inflator to call external xml layout () **********************/
              inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  
          }
  
          /******** What is the size of Passed Arraylist Size ************/
          public int getCount() {
  
              if(data.size()&lt;=0)
                  return 1;
              return data.size();
          }
  
          public Object getItem(int position) {
              return position;
          }
  
          public long getItemId(int position) {
              return position;
          }
  
          /********* Create a holder to contain inflated xml file elements ***********/
          public static class ViewHolder{
  
              public TextView text;
              public TextView text1;
              public TextView textWide;
              public ImageView image;
  
          }
  
          /*********** Depends upon data size called for each row , Create each ListView row ***********/
          public View getView(int position, View convertView, ViewGroup parent) {
  
              View vi=convertView;
              ViewHolder holder;
  
              if(convertView==null){ 
  
                  /********** Inflate tabitem.xml file for each row ( Defined below ) ************/
                 //vi = inflater.inflate(R.layout., null); 
                  vi=inflater.inflate(com.example.tabwithlist.R.layout.final_tab_item,null);
  
                  /******** View Holder Object to contain tabitem.xml file elements ************/
  
                  holder=new ViewHolder();
                  holder.text=(TextView)vi.findViewById(com.example.tabwithlist.R.id.text);
                  holder.text1=(TextView)vi.findViewById(com.example.tabwithlist.R.id.text1);
                  holder.image=(ImageView)vi.findViewById(com.example.tabwithlist.R.id.image);
  
  
  
  
                 /************  Set holder with LayoutInflater ************/
                  vi.setTag(holder);
              }
              else  
                  holder=(ViewHolder)vi.getTag();
  
              if(data.size()&lt;=0)
              {
                  holder.text.setText("No Data");
  
              }
              else
              {
                  /***** Get each Model object from Arraylist ********/
                  tempValues=null;
                  tempValues = (ListModel) data.get(position);
  
                  /************  Set Model values in Holder elements ***********/
                   holder.text.setText(tempValues.getCompanyName());
                   holder.text1.setText(tempValues.getUrl());
                   holder.image.setImageResource(res.getIdentifier("com.example.tabwithlist:drawable/"+tempValues.getImage(),null,null));
  
                   /******** Set Item Click Listner for LayoutInflater for each row ***********/
                //   vi.setOnClickListener(new OnItemClickListener(position));
              }
              return vi;
          }
          }
  
  
      tab_bar.xml(In this file I am declaring list view and tab bar)
  
      &lt;?xml version="1.0" encoding="utf-8"?&gt;
      &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
              android:layout_height="fill_parent"&gt;
      &lt;TabHost 
          android:id="@android:id/tabhost"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"&gt;
          &lt;RelativeLayout
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"&gt;
              &lt;TabWidget
                  android:id="@android:id/tabs"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content" 
                  android:layout_alignParentBottom="true"/&gt;
              &lt;FrameLayout
                  android:id="@android:id/tabcontent"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"/&gt;
          &lt;/RelativeLayout&gt;
      &lt;/TabHost&gt;
      &lt;ListView
              android:id="@+id/listnew"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"  
              /&gt; 
  
      &lt;/RelativeLayout&gt;
  </code></pre>