1. Are you ready for the Galaxy S20? Here is everything we know so far!

Listview inside tabwidget

Discussion in 'Android Development' started by natarajan1986, Apr 16, 2012.

  1. natarajan1986

    natarajan1986 Lurker
    Thread Starter

    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    4. android:orientation="vertical" android:layout_width="fill_parent"
    5.     android:layout_height="fill_parent">
    6.     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    7. android:background="@color/red"    android:layout_width="fill_parent" android:layout_height="wrap_content">
    8.     <Button android:background="@drawable/homericon" android:layout_width="wrap_content"
    9.         android:layout_height="wrap_content" android:id="@+id/button2"></Button>
    10.        
    11.        
    12. </LinearLayout>
    13.    
    14.    
    15.    
    16.    
    17. <TabHost
    18.     android:id="@android:id/tabhost"
    19.     android:layout_width="wrap_content"
    20.     android:layout_height="wrap_content">
    21.     <LinearLayout
    22.         android:orientation="vertical"
    23.         android:layout_width="fill_parent"
    24.         android:layout_height="fill_parent">
    25.         <TabWidget
    26.             android:id="@android:id/tabs"
    27.             android:layout_width="fill_parent"
    28.             android:layout_height="wrap_content" />
    29.         <FrameLayout
    30.             android:id="@android:id/tabcontent"
    31.             android:layout_width="fill_parent"
    32.             android:layout_height="fill_parent"/>
    33.     </LinearLayout>
    34. </TabHost>
    35.    </LinearLayout>
    36.  
    37.  
    This is my layout xml for tabwidget and the code is
    Code (Text):
    1.  
    2. public class BookListTab extends TabActivity implements View.OnClickListener {
    3.  
    4.    
    5.    
    6.     Button homeButton;
    7.  
    8.     @Override
    9.     public void onClick(View paramView) {
    10.         if(paramView !=null){
    11.         Intent intent = new Intent();
    12.         intent.setClass(this, GridViewer.class);
    13.         startActivity(intent);
    14.         }
    15.     }
    16.    
    17.    
    18.     // This class is for list option in the main menu and it list all books read user with tabs
    19.     // like books,authors,tag etc and it also has sort option
    20.     /** Called when the activity is first created. */
    21.     @Override
    22.     public void onCreate(Bundle savedInstanceState) {
    23.         try{
    24.         super.onCreate(savedInstanceState);
    25.         setContentView(R.layout.booklistview);
    26.        
    27.         homeButton = (Button) findViewById(R.id.button2);
    28.         homeButton.setOnClickListener(this);
    29.        
    30.        
    31.         TabHost tabHost = getTabHost();
    32.         tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("TEXT").setContent(new Intent(this, BookActivity.class)));
    33.        
    34.         tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("LIST").setContent(new Intent(this, AuthorActivity.class)));
    35.         tabHost.setCurrentTab(1);
    36.  
    37.  
    38.  
    39.        
    40.  
    41.         }
    42.         catch(Exception e){
    43.             e.getLocalizedMessage();
    44.         }
    45.         }
    46.  
    47.  
    Inside BookActivity class , i have code for listview with icons

    Code (Text):
    1.  
    2. public class BookActivity extends ListActivity {
    3.  
    4. String[] title = new String[] {
    5.             "*New*Apple iPad Wi-Fi (16GB)",
    6.             "7 Touch Tablet -2GB Google Android",
    7.  
    8.             "Apple iPad Wi-Fi (16GB) Rarely Used ",
    9.             "Apple iPad Wi-Fi (16GB) AppleCase" };
    10.  
    11.     static final String[] detail = new String[] { "1h 37m Shipping: $10.00",
    12.             "1h 39m Shipping: Free", "58m 6s Shipping: $10.00",
    13.             "59m 30s Shipping: $10.95" };
    14.     private Integer[] imgid = {
    15.               R.drawable.boomerang,R.drawable.garden,R.drawable.index,
    16.               R.drawable.kaku
    17.             };
    18.     /** Called when the activity is first created. */
    19.     @Override
    20.     public void onCreate(Bundle savedInstanceState) {
    21.         super.onCreate(savedInstanceState);
    22.         setContentView(R.layout.bookactivity);
    23.        
    24.        
    25.        
    26.  
    27.             setListAdapter(new MyCustomAdapter(this, R.layout.booklistrow,
    28.                      title));
    29. getListView().setTextFilterEnabled(true);
    30.  
    31.     }
    32.     public class MyCustomAdapter extends ArrayAdapter<String> {
    33.  
    34.         public MyCustomAdapter(Context context, int textViewResourceId,
    35.         String[] objects) {
    36.         super(context, textViewResourceId, objects);
    37.         // TODO Auto-generated constructor stub
    38.         }
    39.  
    40.         @Override
    41.         public View getView(int position, View convertView, ViewGroup parent) {
    42.  
    43.             View row=null;;
    44.         try{
    45.         LayoutInflater inflater=getLayoutInflater();
    46.         row=inflater.inflate(R.layout.booklistrow, parent, false);
    47.         TextView label=(TextView)row.findViewById(R.id.title);
    48.         label.setText(title[position]);
    49.         ImageView icon=(ImageView)row.findViewById(R.id.img);
    50.        
    51.         // if it is root folder we wont set icon or else we place approprate icons i.e file & folder
    52.    
    53.  
    54.         }
    55.  
    56.  
    57.    
    58.         catch(Exception e){
    59.             e.getLocalizedMessage();
    60.         }
    61.         return row;
    62.         }
    63.        
    64.    
    65.    
    66.    
    67.     }
    68. }
    69.  
    70.  
    and the layouts used for listview

    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <LinearLayout
    4.   xmlns:android="http://schemas.android.com/apk/res/android"
    5.   android:layout_width="fill_parent"
    6.   android:layout_height="fill_parent">
    7.   <TextView android:text="book content"
    8.               android:padding="15dip"
    9.               android:textSize="18dip"
    10.               android:layout_width="fill_parent"
    11.               android:layout_height="wrap_content"/>
    12.      
    13.     <TextView android:id="@+id/path" android:layout_width="fill_parent"
    14.         android:layout_height="wrap_content" />
    15.     <ListView android:id="@android:id/list" android:layout_width="fill_parent"
    16.         android:layout_height="wrap_content" />
    17.  
    18.     <TextView android:id="@android:id/empty" android:layout_width="fill_parent"
    19.         android:layout_height="wrap_content" android:text="No Data" />
    20. </LinearLayout>
    21.  
    22.  
    and this layout to inflate listview
    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    4. android:orientation="horizontal"
    5. android:layout_width="fill_parent"
    6. android:layout_height="wrap_content">
    7. <ImageView
    8. android:id="@+id/img"
    9. android:scaleType="centerCrop"
    10. android:layout_width="70dp"
    11. android:layout_height="70dp"/>
    12.  
    13. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    14. android:orientation="vertical"
    15. android:paddingLeft="10dp"
    16. android:layout_width="fill_parent"
    17. android:layout_height="wrap_content"
    18. >
    19. <TextView
    20. android:layout_width="fill_parent"
    21. android:id="@+id/title"
    22. android:layout_height="wrap_content"
    23. android:textStyle="bold"
    24. android:textColor="#000000"
    25. android:textSize="16sp" />
    26.  
    27.  
    28. </LinearLayout>
    29.  
    30. </LinearLayout>
    31.  
    My problem is tabs are forming correctly but listview is not formed inside tabhost.

    Any idea why list view is not showing,any clarification on the code ,you can ask

    Thanks
     



    1. Download the Forums for Android™ app!


      Download

       
Loading...
Similar Threads - Listview inside tabwidget
  1. hbtousa
    Replies:
    1
    Views:
    571
  2. TiagoPT
    Replies:
    1
    Views:
    549
  3. Rgarner
    Replies:
    19
    Views:
    1,009
  4. behrprofl
    Replies:
    1
    Views:
    2,875
  5. Titan1
    Replies:
    9
    Views:
    2,603
  6. Achal Mehra
    Replies:
    0
    Views:
    678
  7. frappydan
    Replies:
    6
    Views:
    2,048
  8. raylight
    Replies:
    1
    Views:
    3,621
  9. SANJISH KUMAR
    Replies:
    0
    Views:
    1,879
  10. Naomi969
    Replies:
    13
    Views:
    16,068

Share This Page

Loading...