Monday, July 16, 2012

Store Data in Mobile Memory using SharedPreference



Click Here to download source code

Package Name   :   selva.shared

Project Name     :   SharedPreferences

Version              :   1.5 ( Support 1.5 and above versions)


main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

 
    <Button
        android:id="@+id/button1"
        android:layout_width="98dp"
        android:layout_height="wrap_content"
        android:text="Save" />

</LinearLayout>



m1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearlayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <Button
        android:id="@+id/button"
        android:layout_width="140dp"
        android:layout_height="wrap_content"
        android:text="View Saved Data" />
   
</LinearLayout>


SharedPreferencesActivity.java


package selva.shared;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class SharedPreferencesActivity extends Activity
{
     public SharedPreferences prefs;
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        Button btn=(Button) findViewById(R.id.button1);
       
      btn.setOnClickListener(new View.OnClickListener()
      {
       
        @Override
        public void onClick(View v)
        {
            // TODO Auto-generated method stub
       
               String str="www.androidprogramz.in";
                int no=234; 
                float fno=234.234f;
               
                prefs = getSharedPreferences("detail", MODE_PRIVATE);
                      // detail is sharedpreference name
                SharedPreferences.Editor editor = prefs.edit();
               
                editor.putString("Urlname",str);
                // To save str value in Urlname
                editor.putInt("Intvalue", no);
               // To save no value in Intvalue
                editor.putFloat("Floatvalue", fno);
                
// To save fno value in Floatvalue
                editor.commit();
           
           
            Intent i=new Intent(SharedPreferencesActivity.this,Anotheractivity.class);
            startActivity(i);
           
        }
    });
       
       
    }
}



Anotheractivity.java



package selva.shared;

import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Anotheractivity extends Activity
{
    public SharedPreferences prefs;
   
      public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.m1);
       
            Button btn=(Button) findViewById(R.id.button);
   
             prefs = getSharedPreferences("detail", MODE_PRIVATE);
            
                final String name=prefs.getString("Urlname", "novalue");
                     // To get name value from Urlname
               final int val=prefs.getInt("Intvalue", 0);
                       // To get val value from Intvlue
               final float fval=prefs.getFloat("Floatvalue", 0);
                     
// To get fval value from Floatvalue
            btn.setOnClickListener(new View.OnClickListener()
            {
               
                @Override
                public void onClick(View v)
                {
                    // TODO Auto-generated method stub
                LinearLayout l=(LinearLayout) findViewById(R.id.linearlayout);
               
                TextView tv=new TextView(Anotheractivity.this);
                tv.setText(name);
                tv.setTextColor(Color.GREEN);
                l.addView(tv);
               
                TextView tv1=new TextView(Anotheractivity.this);
                tv1.setText(Integer.toString(val));
                tv1.setTextColor(Color.GREEN);
                l.addView(tv1);
               
                TextView tv2=new TextView(Anotheractivity.this);
                tv2.setText(Float.toString(fval));
                tv2.setTextColor(Color.GREEN);
                l.addView(tv2);
               
           
                }
            });
             
             
        }

}

   note : to clear sharedpreference 

               prefs = getSharedPreferences("detail", MODE_PRIVATE);
                    
                SharedPreferences.Editor editor = prefs.edit();
              
                editor.clear(); // to clear all values in detail                

                editor.commit();

   note 1: to remove one particular value

              prefs = getSharedPreferences("detail", MODE_PRIVATE);
                    
                SharedPreferences.Editor editor = prefs.edit();
              
                editor.remove("Urlname"); // to remove paricular value from detail                

                editor.commit();



  AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="selva.shared"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="3" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".SharedPreferencesActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
             android:label="@string/app_name"
             android:name=".Anotheractivity">
            <intent-filter>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

OUTPUT:





















click save button and View Saved Data











































Click Here to download source code


Sunday, July 15, 2012

Create Html file and Displaying in Android



Click Here to Download Source Code

Package Name   :  selva.web

Project Name     :  WebView2

Version               : 1.5 ( Supports 1.5 and above versions)

main.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <WebView android:id="@+id/webview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>


 Create ex.html under assets folder

<body>
<h1> Hi This is Simple Website </h1>
<a href="http://www.androidprogramz.in/"> Click Here </a>

</body>






 
 WebView2Activity.java

 package selva.web;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class WebView2Activity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
      
        WebView wv = (WebView) findViewById(R.id.webview1);
        wv.loadUrl("file:///android_asset/ex.html");
 
    }
}


OUTPUT: 



 




















click On Click Here













































Click Here to Download Source Code


Display HTML content in Android using WebView



Click Here to download Source Code

Package Name  :  selva.web

Project Name   :  WebView

Version              :  1.5 ( Supports 1.5 and above versions)

main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <WebView
     android:id="@+id/webview1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" />
</LinearLayout>


WebViewActivity.java

package selva.web;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;


public class WebViewActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
                      
              
               WebView wv = (WebView) findViewById(R.id.webview1);
               final String mimeType = "text/html";
               final String encoding = "UTF-8";
         
               String html = "<H1>A simple HTML page</H1><body>" + "<p>Android tutorial  programs present in www.androidprogramz.in</p>";
               wv.loadDataWithBaseURL("", html, mimeType, encoding, "");
    }
  
}



OUTPUT :






























Click Here to download Source Code


Display Images in GridView



Click Here to download Source Code


Package name  :  selva.image

Project name    :  ImageInGridView

Version             :  1.5 ( Supports 1.5 and above versions)
 

main.xml


 <?xml version="1.0" encoding="utf-8"?>
 <GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:columnWidth="90dp"
    android:stretchMode="columnWidth"
    android:gravity="center"/>




ImageInGridViewActivity.java


package selva.image;



import android.app.Activity;
import android.os.Bundle;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
public class ImageInGridViewActivity extends Activity
{

//---the images to display---
Integer[] imageIDs = {
        R.drawable.image1,
        R.drawable.image2,
        R.drawable.image3,
        R.drawable.image4,
        R.drawable.image5,
        R.drawable.image6,
};
/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        GridView gridView = (GridView) findViewById(R.id.gridview);
        gridView.setAdapter(new ImageAdapter(this));
        gridView.setOnItemClickListener(new OnItemClickListener()
            {
                public void onItemClick(AdapterView<?> parent,View v, int position, long id)
                    {
                        Toast.makeText(getBaseContext(),"pic" + (position + 1) + "selected",Toast.LENGTH_SHORT).show();
                    }
            });
    }
    public class ImageAdapter extends BaseAdapter
    {
        private Context context;
        public ImageAdapter(Context c)
        {
            context = c;
        }
        //---returns the number of images---
        public int getCount()
        {
            return imageIDs.length;
        }
        //---returns the ID of an item---
        public Object getItem(int position)
        {
            return position;
        }
    //---returns the ID of an item---
        public long getItemId(int position)
        {
            return position;
        }
        //---returns an ImageView view---
        public View getView(int position, View convertView,ViewGroup parent)
        {
            ImageView imageView;
                if (convertView == null)
                {
                    imageView = new ImageView(context);
                    imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
                    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    imageView.setPadding(5, 5, 5, 5);
                }
                else
                {

                imageView = (ImageView) convertView;
                }
                imageView.setImageResource(imageIDs[position]);
                return imageView;
        }
    }
}


OUTPUT :































click on image5









































Click Here to download Source Code


Creating ImageSwitcher in Android



Click here To download Source Code

Package Name   :  selva.image

Project Name     :  ImageSwitcher

Version              :  1.5 ( Supports 1.5 and above versions) 


main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ff000000" >
    <Gallery
    android:id="@+id/gallery1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
    <ImageSwitcher
    android:id="@+id/switcher1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true" />
</RelativeLayout>


 note : res --> values --> img.xml

img.xml 

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Gallery1">
<attr name="android:galleryItemBackground" />
</declare-styleable>
</resources>



 ImageSwitcherActivity.java


package selva.image;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery;
import android.widget.ViewSwitcher.ViewFactory;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
public class ImageSwitcherActivity extends Activity implements ViewFactory {
//---the images to display---
Integer[] imageIDs = {
        R.drawable.image1,
        R.drawable.image2,
        R.drawable.image3,
        R.drawable.image4,
        R.drawable.image5,
        R.drawable.image6
};
private ImageSwitcher imageSwitcher;
/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher1);
            imageSwitcher.setFactory(this);
            imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));
            imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));
            Gallery gallery = (Gallery) findViewById(R.id.gallery1);
            gallery.setAdapter(new ImageAdapter(this));
            gallery.setOnItemClickListener(new OnItemClickListener()
            {
                public void onItemClick(AdapterView<?> parent,View v, int position, long id)
                    {
                        imageSwitcher.setImageResource(imageIDs[position]);

                    }
            });
        }
    public View makeView()
    {
        ImageView imageView = new ImageView(this);
        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
        return imageView;
    }
    public class ImageAdapter extends BaseAdapter
    {
            private Context context;
            private int itemBackground;
            public ImageAdapter(Context c)
            {
                context = c;
                //---setting the style---
                TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
                itemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
                a.recycle();
            }
            //---returns the number of images---
            public int getCount()
            {
                return imageIDs.length;
            }
            //---returns the ID of an item---
            public Object getItem(int position)
            {
                return position;
            }
            public long getItemId(int position)
            {
                    return position;
            }
            //---returns an ImageView view---
            public View getView(int position, View convertView, ViewGroup parent)
            {
                ImageView imageView = new ImageView(context);
                imageView.setImageResource(imageIDs[position]);

                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));
                imageView.setBackgroundResource(itemBackground);
                return imageView;
            }
    }
}


OUTPUT: 


horizontal scroll and click on image






























Click here To download Source Code


Saturday, July 14, 2012

Creating Context Menu in Android



Click Here to download source code

Package Name   :   selva.contextmenu

Project Name     :    ContextMenu

Version              :    1.5 (Supports 1.5 and above versions)

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>



 ContextMenuActivity.java



package selva.contextmenu;

import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class ContextMenuActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button btn = (Button) findViewById(R.id.btn);
        btn.setOnCreateContextMenuListener(this);
    }
  
   
   
    @Override
    public void onCreateContextMenu(ContextMenu menu, View view,ContextMenuInfo menuInfo)
    {
    super.onCreateContextMenu(menu, view, menuInfo);
    CreateMenu(menu);
    }
    @Override
  
    public boolean onContextItemSelected(MenuItem item)
    {
    return MenuChoice(item);
    }
   
    private void CreateMenu(Menu menu)
    {
            MenuItem mnu1 = menu.add(0, 0, 0, "Item 1");
            {
                    mnu1.setAlphabeticShortcut('a');
                    mnu1.setIcon(R.drawable.image1);
            }
            MenuItem mnu2 = menu.add(0, 1, 1, "Item 2");
            {
                    mnu2.setAlphabeticShortcut('b');
                    mnu2.setIcon(R.drawable.image2);
            }
            MenuItem mnu3 = menu.add(0, 2, 2, "Item 3");
            {
                    mnu3.setAlphabeticShortcut('c');
                    mnu3.setIcon(R.drawable.image3);
            }
            MenuItem mnu4 = menu.add(0, 3, 3, "Item 4");
            {
                    mnu4.setAlphabeticShortcut('d');
            }
            menu.add(0, 4,4, "Item 5");
            menu.add(0, 5,5, "Item 6");
            menu.add(0, 6,6, "Item 7");
    }
    private boolean MenuChoice(MenuItem item)
    {
            switch (item.getItemId())
            {
            case 0:
                Toast.makeText(this, "You clicked on Item 1",Toast.LENGTH_LONG).show();
                return true;
            case 1:
                Toast.makeText(this, "You clicked on Item 2",Toast.LENGTH_LONG).show();
                return true;
            case 2:
                Toast.makeText(this, "You clicked on Item 3",Toast.LENGTH_LONG).show();
                return true;
            case 3:
                Toast.makeText(this, "You clicked on Item 4",Toast.LENGTH_LONG).show();
                return true;
            case 4:
                Toast.makeText(this, "You clicked on Item 5",Toast.LENGTH_LONG).show();
                return true;
            case 5:
                Toast.makeText(this, "You clicked on Item 6",Toast.LENGTH_LONG).show();
                return true;

            case 6:
                Toast.makeText(this, "You clicked on Item 7",Toast.LENGTH_LONG).show();
                return true;
            }
            return false;
    }

}



OUTPUT:






 Long Click Button













































 click on item1




































Click Here to download source code



Creating menu in Android



Click Here to download source code

Package Name  :   selva.menu

Project Name    :   Menu

Version             :   1.5 (Supports 1.5 and above versions) 

main.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

</LinearLayout>


MenuActivity.java


package selva.menu;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class  MenuActivity  extends Activity
{

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    }
   
   
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    CreateMenu(menu);
    return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
    return MenuChoice(item);
    }

    private void CreateMenu(Menu menu)
    {
            MenuItem mnu1 = menu.add(0, 0, 0, "Item 1");
            {
                    mnu1.setAlphabeticShortcut('a');
                    mnu1.setIcon(R.drawable.image1);
            }
            MenuItem mnu2 = menu.add(0, 1, 1, "Item 2");
            {
                    mnu2.setAlphabeticShortcut('b');
                    mnu2.setIcon(R.drawable.image2);
            }
            MenuItem mnu3 = menu.add(0, 2, 2, "Item 3");
            {
                    mnu3.setAlphabeticShortcut('c');
                    mnu3.setIcon(R.drawable.image3);
            }
            MenuItem mnu4 = menu.add(0, 3, 3, "Item 4");
            {
                    mnu4.setAlphabeticShortcut('d');
            }
            menu.add(0, 4,4, "Item 5");
            menu.add(0, 5,5, "Item 6");
            menu.add(0, 6,6, "Item 7");
    }
    private boolean MenuChoice(MenuItem item)
    {
            switch (item.getItemId())
            {
            case 0:
                Toast.makeText(this, "You clicked on Item 1",Toast.LENGTH_LONG).show();
                return true;
            case 1:
                Toast.makeText(this, "You clicked on Item 2",Toast.LENGTH_LONG).show();
                return true;
            case 2:
                Toast.makeText(this, "You clicked on Item 3",Toast.LENGTH_LONG).show();
                return true;
            case 3:
                Toast.makeText(this, "You clicked on Item 4",Toast.LENGTH_LONG).show();
                return true;
            case 4:
                Toast.makeText(this, "You clicked on Item 5",Toast.LENGTH_LONG).show();
                return true;
            case 5:
                Toast.makeText(this, "You clicked on Item 6",Toast.LENGTH_LONG).show();
                return true;

            case 6:
                Toast.makeText(this, "You clicked on Item 7",Toast.LENGTH_LONG).show();
                return true;
            }
            return false;
    }

}


OUTPUT:














































Click home button



















































Click Item2











































Click home button again














































Click Item6












































Click Here to download source code


Friday, July 13, 2012

Gallery View in Android



Click Here to Download  Source Code

Package Name  :  selva.gallery

Project Name   :  Gallery

Version Name   :  1.5 (Supports 1.5 and above versions)

main.xml

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Images" />
    <Gallery
        android:id="@+id/gallery1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <ImageView
        android:id="@+id/image1"
        android:layout_width="320px"
        android:layout_height="250px"
        android:scaleType="fitXY" />
</LinearLayout>


 note: res/values/img.xml

img.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Gallery1">
<attr name="android:galleryItemBackground"/>
</declare-styleable>
</resources>



GalleryActivity.java


package selva.gallery;

import android.app.Activity;
import android.os.Bundle;
import android.content.Context;
import android.content.res.TypedArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;

public class GalleryActivity extends Activity {
   
    //---the images to display---
    Integer[] imageIDs = {
    R.drawable.image1,
    R.drawable.image2,
    R.drawable.image3,
    R.drawable.image4,
    R.drawable.image5,
    R.drawable.image6,
        };
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Gallery gallery = (Gallery) findViewById(R.id.gallery1);
        gallery.setAdapter(new ImageAdapter(this));
        gallery.setOnItemClickListener(new OnItemClickListener()
        {
        public void onItemClick(AdapterView<?> parent, View v,
        int position, long id)
        {
        Toast.makeText(getBaseContext(),
        "pic" + (position + 1) + "selected",
        Toast.LENGTH_SHORT).show();
        }
        });
    }
   
    public class ImageAdapter extends BaseAdapter
    {
      private Context context;
      private int itemBackground;
       public ImageAdapter(Context c)
       {
          context = c;
          //---setting the style---
          TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
          itemBackground = a.getResourceId(
          R.styleable.Gallery1_android_galleryItemBackground, 0);
          a.recycle();
       }
       //---returns the number of images---
       public int getCount()
       {
           return imageIDs.length;
       }
           //---returns the ID of an item---
       public Object getItem(int position)
       {
           return position;
       }
       //---returns the ID of an item---
       public long getItemId(int position)
       {
           return position;
       }
       //---returns an ImageView view---
    public View getView(int position, View convertView, ViewGroup parent)
    {
    ImageView imageView = new ImageView(context);
    imageView.setImageResource(imageIDs[position]);
    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
    imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));
    imageView.setBackgroundResource(itemBackground);
 
    return imageView;
    }
    } 
}


OUTPUT:














































Click Here to Download  Source Code