Grid of Clickable Views problem

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Grid of Clickable Views problem

Postby nbadal » Fri Jul 10, 2009 11:23 pm

Hi, I'm trying to create a grid of clickable views from an array, and
the following code places all of the views on top of each other. What am
I doing wrong?
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. for(int ct = 0; ct<max; ct++) {
  3.  
  4. final int pos = ct;
  5.  
  6. LayoutParams layout = new LayoutParams(24,24);
  7.  
  8. viewArray[ct].setBackgroundColor(colorArray[ct]);
  9.  
  10. viewArray[ct].setMinimumHeight(24);
  11.  
  12. viewArray[ct].setMinimumWidth(24);
  13.  
  14. viewArray[ct].setOnClickListener(new View.OnClickListener() {
  15.  
  16.            @Override
  17.  
  18.            public void onClick(View arg0) {
  19.  
  20.                      setColor(pos);
  21.  
  22.            }
  23.  
  24. });
  25.  
  26. //Positioning
  27.  
  28.  if(ct % 6 == 0 ) { //If we need to make a new row
  29.  
  30.            if(rowCt != 0) { //If it's not the first row
  31.  
  32.                       layout.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
  33.  
  34.                       ++rowCt;
  35.  
  36.            } else { //If its the first box
  37.  
  38.                       layout.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
  39.  
  40.            }
  41.  
  42. } else {
  43.  
  44.            if(rowCt!=0) {
  45.  
  46.                       layout.addRule(RelativeLayout.BELOW, viewArray[ct-6].getId());
  47.  
  48.                       layout.addRule(RelativeLayout.RIGHT_OF, viewArray[ct-1].getId());
  49.  
  50.            } else {
  51.  
  52.                       layout.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
  53.  
  54.                       layout.addRule(RelativeLayout.RIGHT_OF, viewArray[ct-1].getId());
  55.  
  56.            }
  57.  
  58. }
  59.  
  60. viewArray[ct].setLayoutParams(layout);
  61.  
  62. colorPicker.addView(viewArray[ct]);
  63.  
  64. }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
nbadal
Developer
Developer
 
Posts: 30
Joined: Tue May 26, 2009 3:55 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Majestic-12 [Bot] and 6 guests