Saturday 15 November 2014

Books ShelfView in Android

Hello Friends,

Today I am going to show you that how you can create Book Shelf View in android. I research soo many and finally i reached to one best and simple way to create shelf View in Android.

In this example i have implemented the Custom Shelf View which will create a shelf. The list of the books will come in the form of JSON response and by parsing it show the details of books in shelfview along with its thumbnails. I wrote a class that extends AdapterView to create a customized GridView that I called "shelf view".



Below is the code of ShelView:

BookShelfView.java

 public class BookshelfView extends GridView {  
  private Bitmap background;  
  public BookshelfView(Context context) {  
  super(context);  
  init();  
  }  
  public BookshelfView(Context context, AttributeSet attrs) {  
  super(context, attrs);  
  init();  
  }  
  public BookshelfView(Context context, AttributeSet attrs, int defStyle) {  
  super(context, attrs, defStyle);  
  init();  
  }  
  protected void init() {  
  //Set the background image of the ShelfView panel.  
  background = BitmapFactory.decodeResource(getResources(), R.drawable.shelf_panel_new);  
  }  
  //Draw a background in the screen and create multiple panels using height & width.  
  @Override  
  protected void dispatchDraw(Canvas canvas) {  
  int top = getChildCount() > 0 ? getChildAt(0).getTop() : 0;  
  for (int y = top; y < getHeight(); y += background.getHeight()) {  
   for (int x = 0; x < getWidth(); x += background.getWidth()) {  
   canvas.drawBitmap(background, x, y, null);  
   }  
  }  
  super.dispatchDraw(canvas);  
  }  
 }