Sunday, 28 February 2016

Gallery Example in android?

Step 1>  Create a Project with the name of GalleryTest and create a xml file in layout with the name of activity.xml and paste below code

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

</LinearLayout>


Step 2> create a Activity with the name of  MainActivity which extends Actrivity and write bellow code

package com.example.gallerytest;
import android.view.ViewGroup;
import android.widget.Gallery;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);
     
        Gallery glr=(Gallery)findViewById(R.id.glr);
     
        glr.setAdapter(new GalleryAdapter(this));
     
    }

}





Step3 > Crea antoher xml file with the name of adapter.xml and paste below code




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

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="34dp"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="28dp"
        android:layout_marginTop="59dp"
        android:layout_toRightOf="@+id/imageView1"
        android:textSize="30sp"
        android:text="TextView" />

</RelativeLayout>



Step 4> Create a Adapter class with the name of  GalleryAdapter.java and paste below code



package com.example.gallerytest;

import java.io.File;

import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class GalleryAdapter  extends BaseAdapter
{
          MainActivity mainActivity;
          String path="/storage/sdcard0/DCIM/Camera/";
          File f=new File(path);
          
          
          String[] file=f.list();
          LayoutInflater inflate;
          
public GalleryAdapter(MainActivity mainActivity)
{
this.mainActivity=mainActivity;
         inflate=LayoutInflater.from(mainActivity);
 
}

@Override
public int getCount() {
return file.length;
  }
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup arg2) {
convertView=inflate.inflate(R.layout.adpater,null);
TextView tv=(TextView)convertView.findViewById(R.id.textView1);
ImageView img=(ImageView)convertView.findViewById(R.id.imageView1);
tv.setText(file[position]); 
    File f=new File(path+file[position]);
    img.setImageURI(Uri.fromFile(f));  
    
return convertView;
}
}




Step 5> Write below permission in AndroidManifest.xml



    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    

AndroidManifest.xml

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />
 
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
 
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name="com.example.gallerytest.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>



OutPut:











No comments:

Post a Comment