2

I have a 2-dim. array of ImageViews. They are set to a RelativeLayout programmatically. But when I start the App, they simply don't appear. This is my code:

private void setMap(ImageView[][] fields){
    RelativeLayout relLay = (RelativeLayout) findViewById(R.id.screen);
    OnClickListener listener = new MyOnClickListener();
    for (int i = 0; i < numFieldsHeight; i++){
        for (int j = 0; j < numFieldsWidth; j++){
            ImageView img = new ImageView(MapActivity.this);
            img.setImageResource(R.drawable.map);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            if (j != 0)
                params.addRule(RelativeLayout.RIGHT_OF, fields[i][j-1].getId());
            else
                params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
            if (i != 0)
                params.addRule(RelativeLayout.BELOW, fields[i-1][j].getId());
            else
                params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
            params.height = fieldLength;
            params.width = fieldLength;
            img.setVisibility(View.VISIBLE);
            img.setId(getFieldId(j,i));
            img.setOnClickListener(listener);
            fields[i][j] = img;
            relLay.addView(fields[i][j], params);
        }
    }
}

and

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000" >

    <RelativeLayout 
        android:id="@+id/relLayDiffSeekBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        ...

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/screen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:background="#222222" >

    </RelativeLayout>

</RelativeLayout>

Further attributes of the RelativeLayout "screen" are set in code. But this works, I can see the RelativeLayout, just the ImageViews are missing.

There are no Exceptions thrown. Please help me!

------! Edit !------

I am very sorry, this code is actually working. The problem was that I forgot to set the variable fieldLength to something different from 0. Thank you very much for your help anyway! :)

Garrarufa
  • 1,145
  • 1
  • 12
  • 29

2 Answers2

0
Drawable d = getResources().getDrawable(R.drawable.map);
d.setBounds(0, 0, 50, 50);
img.setImageDrawable(d);
Autocrab
  • 3,474
  • 1
  • 15
  • 15
0
public void horizontalScrollGalleryLayout () {
    sv=new HorizontalScrollView(this);
    LinearLayout llh = new LinearLayout(this);
    llh.setOrientation(LinearLayout.HORIZONTAL);
    LinearLayout.LayoutParams layoutParamsTV = new LinearLayout.LayoutParams(90,90);
    LinearLayout.LayoutParams layoutParamsLL = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    LinearLayout.LayoutParams layoutParamsLLD = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    layoutParamsTV.setMargins(10, 5, 5, 5);
    for(int k=0;k<6;k++)
    {   
        LinearLayout llv = new LinearLayout(this);
        for (int i=0; i<4; i++) {
            llv.setOrientation(LinearLayout.VERTICAL);
            img=new ImageButton(this);
            try {
                img.setBackgroundResource(integerIDs[total]);
            } catch (Exception e) {

                e.printStackTrace();
            }
            llv.addView(img, layoutParamsTV);
        }
        llh.addView(llv, layoutParamsLL);
        llh.setBackgroundColor(Color.TRANSPARENT);
    }
    sv.setBackgroundColor(Color.GRAY);
    sv.addView(llh, layoutParamsLLD);
    setContentView(sv);

}

This could be one of the Solution...for creating a grid of Matrix for you problem .try it...any problem let me know

Nitesh Tiwari
  • 4,742
  • 3
  • 28
  • 46
  • I tried this, but still same problem. I changed all ImageViews to Buttons and tried your code with it, but I still cannot see them. – Garrarufa Sep 26 '13 at 12:43
  • check out the edit one this will create a Dynamic Scrolable matrix of grid 6*4 – Nitesh Tiwari Sep 26 '13 at 13:29
  • actually I do not want to use a GridView. This is quite complicated for what I want. I need a field of a non changing number of quadratic buttons that are placed next to each other without spaces between them. This field may not be scrollable, every button must be visible and must stay in the same place all the time. I want it to look like a single picture, but every button must be colored and clicked independently. – Garrarufa Sep 27 '13 at 08:38