How can i do the same thing with Gallery in Android

Posted by Maxood on Stack Overflow See other posts from Stack Overflow or by Maxood
Published on 2010-05-06T11:05:17Z Indexed on 2010/05/06 11:08 UTC
Read the original article Hit count: 253

Filed under:

I am navigating images with the clicks of next and previous buttons.Here is my code:

package com.myapps.imagegallery;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class ImageGallery extends Activity {
    /** Called when the activity is first created. */

    int imgs[] = {R.drawable.bluehills,R.drawable.lilies,R.drawable.sunset,R.drawable.winter};
    String desc[] = {"Blue Hills", "Lillies", "Sunset", "Winter" };
    int counter=0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        final ImageView imgView = (ImageView)findViewById(R.id.ImageView01);
        imgView.setImageResource(imgs[counter]);

        final TextView tvDesc = (TextView)findViewById(R.id.tvDesc);

        Button btnNext = (Button)findViewById(R.id.btnNext);
        btnNext.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View arg0) {
                try{
                // TODO Auto-generated method stub
                if (counter < desc.length -1)   
                counter++;
                imgView.setImageResource(imgs[counter]);
                tvDesc.setText(desc[counter]);
                }catch(Exception e)
                {
                    Toast.makeText(ImageGallery.this, e.toString(), Toast.LENGTH_SHORT).show();
                }

            }


        });


        Button btnPrev = (Button)findViewById(R.id.btnPre);
        btnPrev.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                try{
                    // TODO Auto-generated method stub
                    if (counter > 0)
                    counter--;
                    imgView.setImageResource(imgs[counter]);
                    tvDesc.setText(desc[counter]);
                    }catch(Exception e)
                    {
                        Toast.makeText(ImageGallery.this, e.toString(), Toast.LENGTH_SHORT).show();
                    }           

            }});

    }
}

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

<ImageView 
android:id="@+id/ImageView01"
android:layout_x="70dip" 
android:layout_width="200px" 
android:layout_height="200px" 
android:layout_y="90dip"

>
</ImageView>    


<TextView 
    android:id="@+id/tvDesc" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    android:layout_x="90px"
    android:layout_y="300px"
    />



<Button 
android:layout_x="47dip" 
android:layout_height="wrap_content" 
android:text="Previous" 
android:layout_width="100px" 
android:id="@+id/btnPre" 
android:layout_y="341dip">
</Button>

<Button 
android:layout_x="190dip" 
android:layout_height="wrap_content" 
android:text="Next" 
android:layout_width="100px" 
android:id="@+id/btnNext" 
android:layout_y="341dip">
</Button>

</AbsoluteLayout>

© Stack Overflow or respective owner

Related posts about gallery