Sunday, 25 August 2013

Add images dynamically based on number selected

Add images dynamically based on number selected

I have the following layout which displays an image and a text string:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center" >
<ImageView
android:id="@+id/ivIcon"
android:layout_width="@dimen/number_icon"
android:layout_height="@dimen/number_icon"
android:scaleType="fitXY"
android:layout_marginTop="@dimen/letter_icon_margin_top" />
<Button
android:id="@+id/btnNumber"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="5dp"
android:layout_weight="3"
android:text=""
android:gravity="center"
android:textSize="@dimen/word_size_text"
android:textColor="#000000"
android:textStyle="bold"
android:background="@drawable/borderaction" />
</LinearLayout>
The ivIcon is displaying the images based on user input. It worked great
when displaying one image per letter. But how do i fix the above layout so
it displays the number of images based on user selection.
For example this is what is looking like when I use it for letter:

For the number, if the user chooses 5 the display should be:

If the user chooses 1 the display should be:

The code that is setting the image for the letter is:
ivLetterIcon = (ImageView) findViewById(R.id.ivIcon);
ivLetterIcon.setImageResource(R.drawable.apple);
How would I accomplish the number images?
Would it be best to have a blank layout without the images and add the
ImageView at runtim depending on the number and have it centered? This way
the images(s) are centered to the parent view layout?

No comments:

Post a Comment