Shelf View Example In Android

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

Leave a Comment:

3 comments
sv says July 10, 2013

Can you also share the resources, it will be very much helpful.

Reply
vijay says July 10, 2013

Hi team,
Ya this is nice and easy to understand …but i want if we put many images in drawable or sd card so how can we access and is it possible with these images in form of shelf view.
And One more Thing i am unable to perform with Google map can u help me for that.

Thanks & Regards
Vijay

Reply
Deepak Swami says July 25, 2013

nice tutorial!!
could you please attach source code with example..

Reply
Add Your Reply