views:

46

answers:

1

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.

+1  A: 

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 Integer[] 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>
ShadowGod
This works perfectly. What is driving me crazy is that my 3 or 4 test projects essentially did this exactly the same way, but they would either forceclose or not work some other way. Thank you for saving me from tearing my hair out. The part I see different from mine is the onClickListener, could you why it has to be set to "this", just so I can try get a more complete understanding. Thanks again.
Hamid
Being new to Android, I really cannot explain why it has to be set to "this". I *think* the "this" just references your class, or main activity. Like in the above example, "this" could be replaced by "ImageViewExample.this" and it would work the same. I learned the above from reading http://developer.android.com/guide/topics/ui/ui-events.html
ShadowGod
Thanks again. Most helpful and kind.
Hamid