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);
    }
}
4
  • Is there a reason you use relative layout instead of linear layout Commented Nov 22, 2013 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? Commented Nov 22, 2013 at 19:24
  • @JoshEngelsma no there is no real reason. Just chose it to get started. Not committed to it at all. Commented Nov 22, 2013 at 19:30
  • @nexus_2006 nothing is different with those two that I know of. That is why I was so confused. Commented Nov 22, 2013 at 19:31

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

2 Comments

That worked like a charm. Not sure why either. Maybe you are right about the default NO_ID. Regardless, thank you!
Just found public static final int View.NO_ID = 0xffffffff; (-1), so I really don't know, but glad it worked!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.