Creating a Matrix of ImageView at runtime

0 Usman Mohy ud Din · October 23, 2014
Hy Everyone...

I need to creat a matrix of imageView at run time.Is there anyway to do so.??
Like in below image. I,23 representing position of imageview.

https://buckysroom.org/images/forum/upload/2014-10-23/3b40943e7e2a5311b550cf168a626215.png

Post a Reply

Replies

Oldest  Newest  Rating
+1 Alex Sweps · October 23, 2014
Your not creating it with a multi dimensional array. I dont have much time to go through it check but watch this and try and setup your matrix like how bucky makes this table. 

https://buckysroom.org/videos.php?cat=31&video=17999
0 Alex Sweps · October 23, 2014
Yeah just add the code in your constructor. Use a multidimensional array for the grid. 
0 Usman Mohy ud Din · October 23, 2014
@Alex
I am using this code, but it only shows two horizontal bars in center of screen instead of showing matrix.
Which part of code i have to improve/change...?

Code
  re.setGravity(Gravity.CENTER);
          int index=1;
          int rows=4;
          int cols=4;
        for (int i = 0; i < rows; i++) {
          
        for (int j = 0; j < cols; j++) {
                final  ImageView img =  new ImageView(this);
                  //do some operations with image
                    RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(40,40);
                if (j == 0) {
                            rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT,
                                  RelativeLayout.TRUE);
                    }
                    else {
                            rlp.addRule(RelativeLayout.RIGHT_OF, index - 1);
                        }
                 if (i == 0) {
                           rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP,
                                   RelativeLayout.TRUE);
                        }
                      else {
                            rlp.addRule(RelativeLayout.BELOW, index - cols);
                        }                                             

                          img.setLayoutParams(rlp);            
                      re.addView(img);
                }
        
        }

main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/imageLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:background="#FFAA66CC"
    android:gravity="center"
    android:textColor="#000000" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center" >

        <TextView
            android:id="@+id/bottom"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_alignParentBottom="true"
            android:background="#FFFF4444"
            android:gravity="center"
            android:textColor="#000000"
            android:textSize="30sp" />

        <TextView
            android:id="@+id/above"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_above="@id/bottom"
            android:background="#FF33B5E5"
            android:gravity="center"
            android:paddingBottom="3dp"
            android:textColor="#000000"
            android:textSize="30sp" />
    </RelativeLayout>

</RelativeLayout>
  • 1

Java / Android Development

107,255 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator