Maria Niaz
Maria Niaz

Reputation: 107

How to dynamically create a large number of imageView in Android

Hello I am making a jigsaw puzzle application and for that i have 16 made smaller bitmaps out of a large scaled bitmap with height = width = 240. Now I have to dynamically display these bitmaps in a single activity at one time. The code I tried is not working properly as it is displaying all images in a single row and size of image is too small. Here is the code:

public void display()
    {
        float x =0;
        float y = 0;
        LinearLayout llMain = new LinearLayout(this);
        for(int i=0;i<3;i++)
        {
            LinearLayout llRow = new LinearLayout(this);
            for(int j=0;j<tiles.length/4;j++)
            {
                ImageView iv = new ImageView(this);
                iv.setImageBitmap(tiles[j]);// get bitmap from image path
                iv.setX(x);
                iv.setY(y);
                x = y = x + 60;
                llRow.addView(iv);
            }
            llMain.addView(llRow);
        }
        setContentView(llMain);
       }
}

Here is the LogCat:

01-05 00:32:35.820: D/dalvikvm(11346): GC_EXTERNAL_ALLOC freed 47K, 52% free 2599K/5379K, external 1990K/2108K, paused 135ms
01-05 00:32:44.183: D/dalvikvm(11346): GC_EXTERNAL_ALLOC freed 43K, 50% free 2739K/5379K, external 2477K/2527K, paused 18ms
01-05 00:32:45.156: I/dalvikvm(11346): Could not find method android.widget.ImageView.setX, referenced from method com.maju.jigsawpuzzle.PlayBoard.display
01-05 00:32:45.156: W/dalvikvm(11346): VFY: unable to resolve virtual method 3236: Landroid/widget/ImageView;.setX (F)V
01-05 00:32:45.156: D/dalvikvm(11346): VFY: replacing opcode 0x6e at 0x002e
01-05 00:32:45.156: D/dalvikvm(11346): VFY: dead code 0x0031-003e in Lcom/maju/jigsawpuzzle/PlayBoard;.display ()V
01-05 00:32:45.179: D/AndroidRuntime(11346): Shutting down VM
01-05 00:32:45.179: W/dalvikvm(11346): threadid=1: thread exiting with uncaught exception (group=0x40015578)
01-05 00:32:45.187: E/AndroidRuntime(11346): FATAL EXCEPTION: main
01-05 00:32:45.187: E/AndroidRuntime(11346): java.lang.NoSuchMethodError: android.widget.ImageView.setX
01-05 00:32:45.187: E/AndroidRuntime(11346):    at com.maju.jigsawpuzzle.PlayBoard.display(PlayBoard.java:164)
01-05 00:32:45.187: E/AndroidRuntime(11346):    at com.maju.jigsawpuzzle.PlayBoard.shuffleArray(PlayBoard.java:143)
01-05 00:32:45.187: E/AndroidRuntime(11346):    at com.maju.jigsawpuzzle.PlayBoard.breakImage(PlayBoard.java:123)
01-05 00:32:45.187: E/AndroidRuntime(11346):    at com.maju.jigsawpuzzle.PlayBoard.onCreate(PlayBoard.java:40)
01-05 00:32:45.187: E/AndroidRuntime(11346):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-05 00:32:45.187: E/AndroidRuntime(11346):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
01-05 00:32:45.187: E/AndroidRuntime(11346):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
01-05 00:32:45.187: E/AndroidRuntime(11346):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-05 00:32:45.187: E/AndroidRuntime(11346):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
01-05 00:32:45.187: E/AndroidRuntime(11346):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-05 00:32:45.187: E/AndroidRuntime(11346):    at android.os.Looper.loop(Looper.java:123)
01-05 00:32:45.187: E/AndroidRuntime(11346):    at android.app.ActivityThread.main(ActivityThread.java:3687)
01-05 00:32:45.187: E/AndroidRuntime(11346):    at java.lang.reflect.Method.invokeNative(Native Method)
01-05 00:32:45.187: E/AndroidRuntime(11346):    at java.lang.reflect.Method.invoke(Method.java:507)
01-05 00:32:45.187: E/AndroidRuntime(11346):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
01-05 00:32:45.187: E/AndroidRuntime(11346):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
01-05 00:32:45.187: E/AndroidRuntime(11346):    at dalvik.system.NativeStart.main(Native Method)

Line 164 is the line:

iv.setX(x);

Does anybody know the solution? Moreover I have to register onClick with them...

Thanks in advancce

Upvotes: 0

Views: 1431

Answers (2)

Leonidos
Leonidos

Reputation: 10518

Do not use setX/setY if you want to support old android versions. Instead set LayoutParams (LinearLayout.LayoutParams) to specify ImageView's x/y params. Read this post: Android - Use of view.setX() and setY in api 8

Upvotes: 1

Waza_Be
Waza_Be

Reputation: 39558

According to the documentation

the setX method is only available in API level 11.

That means that your app will crash if you are on Android 2.x.

I guess that you are getting some Lint warning in your code, isn't it?

I have never used the setX method and cannot help, but I hope you can find some alternatives on pre-Honeycomb devices.

When you will have find it you can use

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){}

Upvotes: 0

Related Questions