Click here to Skip to main content
15,885,366 members
Articles / Mobile Apps / Android
Tip/Trick

How to Create a shelfview in Android with TableLayout?

Rate me:
Please Sign up or sign in to vote.
3.40/5 (4 votes)
21 May 2012Eclipse 26.2K   1K   4   2
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
    <?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

    Java
    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


Written By
Iran (Islamic Republic of) Iran (Islamic Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionplz help me Pin
nourhan adel24-Dec-13 20:33
nourhan adel24-Dec-13 20:33 
Questionplz help me Pin
nourhan adel24-Dec-13 20:26
nourhan adel24-Dec-13 20:26 

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

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