android image "viewer" app

Hamid picture Hamid · Oct 3, 2010 · Viewed 24.5k times · Source

I am trying to create an android app that will let me display images fullscreen with next and previous buttons on top to change between them.

Can anybody point me to some tutorials where i can find instructions on something similar?

If not, what is the best method to use to get the images into the app? I have tried several ways from creating object classes for the images and instantiating it with a drawable in each using the Bitmap Factory to return the image but that won't work.

I am a beginner to android and could really use reference material but can't find anything useful that covers this subject.

Answer

ShadowGod picture ShadowGod · Oct 3, 2010

As a newbie myself I've been working with this and it's very simple. Here is some code (maybe there is a better way but this is the way I figured out how to do it):

package com.imageviewexample;

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

public class ImageViewExample extends Activity implements OnClickListener {

    /** Called when the activity is first created. */

    int image_index = 0;
    private static final int MAX_IMAGE_COUNT = 3;

    private int[] mImageIds = {
            R.raw.image1,
            R.raw.image2,
            R.raw.image3
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button btnPrevious = (Button)findViewById(R.id.previous_btn);
        btnPrevious.setOnClickListener(this);       
        Button btnNext = (Button)findViewById(R.id.next_btn);
        btnNext.setOnClickListener(this);

        showImage();        

    }

    private void showImage() {

        ImageView imgView = (ImageView) findViewById(R.id.myimage);             
        imgView.setImageResource(mImageIds[image_index]);       

    }

    public void onClick(View v) {

        switch (v.getId()) {

            case (R.id.previous_btn):

                image_index--;

                if (image_index == -1) {                    
                    image_index = MAX_IMAGE_COUNT - 1;                  
                }

                showImage();

            break;

            case (R.id.next_btn):

                image_index++;

                if (image_index == MAX_IMAGE_COUNT) {               
                image_index = 0;                
            }

                showImage();

            break;      

        }

    }
}

And this is the 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"
    >

<Button
    android:id="@+id/previous_btn"
    android:layout_width="124dip" 
    android:layout_height="wrap_content" 
    android:text="Previous"
    />

<Button
    android:id="@+id/next_btn"
    android:layout_width="124dip" 
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/previous_btn" 
    android:text="Next"
    />

<ImageView
    android:id="@+id/myimage"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:layout_below="@+id/previous_btn"
    />         

</RelativeLayout>