Click here to Skip to main content
Click here to Skip to main content

How to Create a shelfview in Android with TableLayout?

, 21 May 2012 Eclipse
Rate this:
Please Sign up or sign in to vote.
In this article, we try to make a shelf view show a list of books or newspapers, etc.

Introduction

In this article, we try to make a shelf view show a list of books or newspapers, etc.

Using the Code

  1. First, main.xml:

    In this file, you should use ScrollView and TableLayout for showing a shelf view.

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/sclView">
        <TableLayout
                android:id="@+id/tblLayout"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" 
               android:padding="0dp">
        </TableLayout>
    </ScrollView>
  2. showShelfView class: Inner TableLayout add several HorizontalScroll equals the number of rows. Also inner any TableRow add Image. Don't forget to set a shelf image for Row's background:

    Libary shelf view

    public class showShelfView extends Activity {
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
    
            int numRow = 4;
            int numCol = 8;
    
            TableLayout tblLayout = (TableLayout) findViewById(R.id.tblLayout);
    
            for(int i = 0; i < numRow; i++) {
                HorizontalScrollView HSV = new HorizontalScrollView(this);
                HSV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 
                		LayoutParams.FILL_PARENT));
    
                TableRow tblRow = new TableRow(this);
                tblRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 
                		LayoutParams.WRAP_CONTENT));
                tblRow.setBackgroundResource(R.drawable.bookshelf);
    
                for(int j = 0; j < numCol; j++) {
                ImageView imageView = new ImageView(this);
                    imageView.setImageResource(R.drawable.book1);
    
                    TextView textView = new TextView(this);
                    textView.setText("Java Tester");
                    textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
                    		LayoutParams.WRAP_CONTENT));
    
                    tblRow.addView(imageView,j);
                }
    
                HSV.addView(tblRow);
                tblLayout.addView(HSV, i);
            }
        }
    }

Points of Interest

Notice, we make TableRow and HorizontalScrollView equal the number of numRow variable. You can use a Dialog or Editview for input value, then create rows equal to that value.

License

This article, along with any associated source code and files, is licensed under The Eclipse Public License 1.0

Share

About the Author

omid.nazifi

Iran (Islamic Republic Of) Iran (Islamic Republic Of)
No Biography provided

Comments and Discussions

 
Questionplz help me Pinmembernourhan adel24-Dec-13 20:33 
Questionplz help me Pinmembernourhan adel24-Dec-13 20:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.141029.1 | Last Updated 21 May 2012
Article Copyright 2012 by omid.nazifi
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid