0

I need to create this layout purely in Java and not in XML. This means I have no XML in Eclipse for this project. Right now my layout looks like this:

enter image description here

You might not be able to tell from the picture but blocks 0, 1 and 3 are all on top of each other. I need the 5 blocks to look like this:

 0  1  2
 3  4

This is my first time not using an XML file to make Activity layouts so I am inching along with figuring this stuff out. And if you notice me doing something entirely the "long" way when I could write the code more efficiently, let me know please.

But my question is, why aren't the blocks laying the order I need them? I feel like I am messing something up that is simple.

public class Puzzle extends Activity {

    int z1to5 = 50;
    int z6to10 = 50;

    @Override
    protected void onCreate(Bundle savedInstanceState) {        
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.XXXXX);

        RelativeLayout relativelayout = new RelativeLayout(getApplicationContext());

        Button bList[] = new Button[5];

        for (int i = 0; i < 5; i++) {
            Button button = new Button(getApplicationContext());
            button = new Button(getApplicationContext());
            button.setId(i);
            button.setText("" + i);
            relativelayout.addView(button);
            bList[i] = button;
        }

        RelativeLayout.LayoutParams params0 = new RelativeLayout.LayoutParams(150, 150);
            bList[0].setLayoutParams(params0);

        RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(150, 150);
            params1.addRule(RelativeLayout.RIGHT_OF, bList[0].getId());
            bList[1].setLayoutParams(params1);

        RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(150, 150);
            params2.addRule(RelativeLayout.RIGHT_OF, bList[1].getId());
            bList[2].setLayoutParams(params2);

        RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(150, 150);
            params3.addRule(RelativeLayout.BELOW, bList[0].getId());
            bList[3].setLayoutParams(params3);

        RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(150, 150);
            params4.addRule(RelativeLayout.RIGHT_OF, bList[3].getId());
            params4.addRule(RelativeLayout.BELOW, bList[1].getId());
            bList[4].setLayoutParams(params4);

        setContentView(relativelayout);
    }
}
Matt
  • 3,882
  • 14
  • 46
  • 89
  • Is there a reason you use relative layout instead of linear layout – Josh Engelsma Nov 22 '13 at 19:14
  • I have no idea what the answer is, but 1 or 2 of your buttons went to the correct relative place. (2 right of 1) (4 right of 3 and below 1). Whats different about those two? – nexus_2006 Nov 22 '13 at 19:24
  • @JoshEngelsma no there is no real reason. Just chose it to get started. Not committed to it at all. – Matt Nov 22 '13 at 19:30
  • @nexus_2006 nothing is different with those two that I know of. That is why I was so confused. – Matt Nov 22 '13 at 19:31

1 Answers1

1

I got it working with this change:

for (int i = 0; i < 5; i++) {
        Button button = new Button(getApplicationContext());
        button = new Button(getApplicationContext());
        button.setId(i+1);
        button.setText("" + i);
        relativelayout.addView(button);
        bList[i] = button;
    }

button.setId(i+1);

I can't find it in the docs, but maybe when you make a new View programmatically, the default NO_ID equals 0?

nexus_2006
  • 744
  • 2
  • 14
  • 29
  • That worked like a charm. Not sure why either. Maybe you are right about the default NO_ID. Regardless, thank you! – Matt Nov 22 '13 at 19:39
  • Just found `public static final int View.NO_ID = 0xffffffff;` (-1), so I really don't know, but glad it worked! – nexus_2006 Nov 22 '13 at 19:43