Sunday, 9 May 2010

Galleries

Steps to write the Galleries


  • go to File->New->Android Application Project


  • Give the Application Name: (you can give any name) Ex:








  • click on Next button, then the we will get following:


  • copy the jpg or gif files (photos) and paste into "drawable-hdpi of res folder


  • Once you pasted into the drawable-hdpi folder, automatically, jpg (or picture) integer varaibles will be created into the R.java file, its reference also will be created in the form of hexa decimal format.
  • Go to "res" folder of left side, and choose "layout " , double click on "activity_mail.xml" file


  • Go to Layouts, select LinearLayout(Horizontal) (since i want to scroll my images Horizontally), and drag into the box:

  • Automatically generates the xml tags, Now you can see the xml tags by double clicking "activity_main.xml" file

  • Now we need to create the Gallery tags, to do that one, click on GraphicalLayout, click on Images & Media option from the list


  • now we can see the code, by double clicking activity_main.xml

  • Now we need to access the images so to do this one we need to create the derived class of BaseAdapter class,
  • Now im creating a child class of BaseAdapter class

  • extend the BaseAdapter class, and import that package
  • Now create the Integer class array in MyAdapter.java, and store the images in that array;
package com.example.gallerydemos;
import android.widget.BaseAdapter;

public class MyAdapter public class MyAdapter extends BaseAdapter
{
Integer myimages[]={R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e,R.drawable.f};
}

  • Now create Context class reference
package com.example.gallerydemos;
import android.widget.BaseAdapter;
import android.content.Context;

public class MyAdapter extends BaseAdapter
{
Integer myimages[]={R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e,R.drawable.f};
Context con;
}

  • Now we will get error derived class, so we need to right click the mouse at that class, choose "add unimplemented methods"


  •  Now we will get the followng window

  • create parameterized constructor:
package com.example.gallerydemos;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import android.widget.BaseAdapter;

public class MyAdapter extends BaseAdapter{
Integer myimages[]={R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e,R.drawable.f};
Context con;
//Now create a parameterized constructor
public MyAdapter(Context con)
{
         super();//call the super class constructor (BaseAdapter)
this.con=con;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
return null;
}
}

  • Rewrite the methods like below:
package com.example.gallerydemos;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import android.widget.BaseAdapter;
import android.widget.ImageView;

public class MyAdapter extends BaseAdapter{
Integer myimages[]={R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e,R.drawable.f};
Context con;
//Now create a parameterized constructor
public MyAdapter(Context con)
{
super();//super class constructor
this.con=con;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return myimages.length;//total images
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return myimages;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
//create ImageView object
ImageView iv=new ImageView(con);
//to display images
iv.setImageResource(myimages[arg0]);
//to give the space between the images
        iv.setPadding(52, 0, 52, 0);
//return the imageview object iv
return iv;
}
}

  • screen shot is like below:


  • Now we need to create the Gallery class object in MainActivity.java


  • Now create the MyAdapter class object (derived class of BaseAdapter) and pass the current class (MainActivity) to MyAdapter class: code looks like the below:
package com.example.gallerydemos;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Gallery;

public class MainActivity extends Activity {
//creating the reference variable for Gallery
Gallery gal;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//create the object for Gallery
gal=(Gallery)findViewById(R.id.gallery1);
//create the MyAdapter class object, and pass current class to that
MyAdapter ma=new MyAdapter(MainActivity.this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}











No comments:

Post a Comment