8

I'm pretty new to Android and need a bit of help with some code.

Basically I have an array of coins and I want to dynamically display images for the coins in a ScrollView. Each time the user adds a coin the view should update appropriately.

Thank you!

Here is a screenshot of what I want to do - https://i.stack.imgur.com/ziCsu.png

Shmuel
  • 3,916
  • 2
  • 27
  • 45
  • hi, please check this link http://stackoverflow.com/questions/541966/how-do-i-do-a-lazy-load-of-images-in-listview – deadfish Mar 01 '13 at 10:05

3 Answers3

29

Try this sample

MainActivity

public void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    setContentView(R.layout.activity_main);
    LinearLayout linearLayout1 = (LinearLayout) findViewById(R.id.linearLayout1);
    for(int x=0;x<3;x++) {
        ImageView image = new ImageView(MainActivity.this);
        image.setBackgroundResource(R.drawable.ic_launcher);
        linearLayout1.addView(image);
    }
}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
        android:id="@+id/linearLayout1"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </LinearLayout>
    </ScrollView>
</LinearLayout>
NaviRamyle
  • 3,967
  • 1
  • 31
  • 49
  • yah. thanks. this is what i have now -http://i.imgur.com/eRzN3Sb.png (the color is just for debugging purposes.) one last thing, i want to be able to load the appropriate image from the array for each coin. basically i want to be able to say coinView.setBackgroundResource(coinArray[x].image) i assume i need to somehow use a drawable object and i was hoping its possible to include it in my enum class. the enum class looks like `code`(test) – Shmuel Mar 03 '13 at 03:00
  • I ran out of space in the comment above. I still need some help using drawables in my enum class. this is the link to my new question http://stackoverflow.com/questions/15182045/android-using-drawables-in-an-enum thank you – Shmuel Mar 03 '13 at 03:12
  • "attempt to invoke virtual method on a null object reference" any idea why? – Anitha May 21 '15 at 07:01
0

Look into using SurfaceView.

You will have a canvas object to draw your images on, and then aligning the coins is just a matter of mapping your pixels appropriately.

This is a good tutorial: http://www.mindfiresolutions.com/Using-Surface-View-for-Android-1659.php

TtT23
  • 6,876
  • 34
  • 103
  • 174
-3

Use a GridView instead and customize your own adapters. Gridview works like ListView