Description:
This example will show how you can shelf view using table layout in android.
Algorithm:
1.) Create a new project by File-> New -> Android Project name it ShelfViewExample.
2.) Write following into main.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>
3.) Run for output.
Steps:
1.) Create a project named ShelfViewExample and set the information as stated in the image.
Build Target: Android 4.2
Application Name: ShelfViewExample
Package Name: com.example.ShelfViewExample
Activity Name: ShelfViewExampleActivity
Min SDK Version: 4.2

2.) Open ShelfViewExampleActivity.java file and write following code there:
package com.example.shelfviewexample;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.ViewGroup.LayoutParams;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class ShelfViewExampleActivity 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.shelf);
for(int j = 0; j < numCol; j++) {
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.book);
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);
}
}
}
3.) Compile and build the project.
Output
