marcst
marcst

Reputation: 63

How do I simply add image views to a LinearLayout dynamically

I'm getting byte array from SQLiteDatabase and converting them to Bitmap then adding them to an image view. I want to print the images as a list of image views scrolling vertically. However when I try to add the view it will only print the first image added to the LinearLayout and not the rest.

In onCreate()

root = (LinearLayout) findViewById(R.id.photoFeedLayout);

Layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/photoFeedLayout"
    tools:context="com1032.cw2.ms01288.ms01288_assignment2.PhotoFeed">


</LinearLayout>

And in my PhotoFeed.java.

public void run() {
    do {
        byte[] imData = images.getBlob(images.getColumnIndex("IMAGEDATA"));
        Bitmap bm = BitmapFactory.decodeByteArray(imData, 0, imData.length);

        LinearLayout.LayoutParams imParams =
                                        new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        ImageView iv = (ImageView) new ImageView(getApplicationContext());

        iv.setImageBitmap(bm);

        root.addView(iv,imParams);

        } while (images.moveToNext());
    }

Is there a better way of printing all the ImageViews vertically by dynamically adding them to the root layout?

Upvotes: 0

Views: 113

Answers (1)

Harshad Pansuriya
Harshad Pansuriya

Reputation: 20910

Take one RecyclerView inside your layout.xml file

Then Inside the your OnCreate method Initialize it and set the Adpater class.

Now Create the Adapter and class and pass the Imagedata to inside the Adapter. and set it to the ImageView.

this will display the all the vertically..

For more detail visit this :

http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/

Upvotes: 1

Related Questions