//
you're reading...
Tech

Five steps to load a set of images in Andoird applications

This post introduces a simple and quick way to load a set of images into an Android application.

1. Place the images somewhere

Get these images here. Place it in the project folder under “res/drawable/” (create one if none);

2. A set of images is an array of local references (i.e., where they are in the project folder)

    // class fields
    private Integer[] images = {
            R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7,
    };

3. Create some layout (here we use linear layout )

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
    //...

4. Place the images in the layout

        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        
        int numImages = images.length;
        for(int i = 0; i < numImages; i++)
        {
        	ImageView tmpIV = new ImageView(this);
        	tmpIV.setImageResource(images[i]);
        	linearLayout.addView(tmpIV, new LinearLayout.LayoutParams(
    				LayoutParams.FILL_PARENT,
    				LayoutParams.WRAP_CONTENT));
        }

5. Make the screen of images scrollable

        ScrollView scrollView = new ScrollView(this);
        scrollView.addView(linearLayout);
        setContentView(scrollView);
Advertisements

About Xiang 'Anthony' Chen

Making an Impact in Your Life

Discussion

Trackbacks/Pingbacks

  1. Pingback: Four Layouts in Android Applications « A Designer with Dirty Hands - November 27, 2011

  2. Pingback: Four Layouts in Android Applications | Think Hard, Build Harder - June 9, 2013

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Twitter Updates

%d bloggers like this: