8

I'm having a hard time getting two text views to appear on top of each other in my java code. Here's the code I'm experimenting with:

/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        layout = new RelativeLayout(this);
        text1 = new TextView(this);
        text1.setText("1");
        text2 = new TextView(this);
        text2.setText("2");

        RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams q = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);

        q.addRule(RelativeLayout.BELOW, layout.getId());
        text1.setLayoutParams(q);
        layout.addView(text1);


        p.addRule(RelativeLayout.BELOW,text1.getId());
        text2.setLayoutParams(p);
        layout.addView(text2);

        setContentView(layout);
    }

This stacks the two text views on the same line, but I want TextView text2, to appear below TextView text1, so in my app I want the following to appear as the output:

1
2

I've tried all sort of things with the "addRule" method, I'm not sure why this isn't working. I want to know how to do this without XML because I plan to build a library of methods that can build up a layout that is easily adjustable through editing an array.

1
  • 2
    accept some answers to your previous questions Commented Mar 16, 2011 at 15:05

3 Answers 3

8

Your TextViews don't have an id (by default the id is -1)... put this after their initialization:

text1.setId(1111); // 1111 is just an example,
text2.setId(2222); // just make sure the id are unique
Sign up to request clarification or add additional context in comments.

1 Comment

use View.generateViewId() to make sure the IDs won't collide with any other ID value.
3

I don't think you are looking to layout the text1 view below the RelativeLayout since you added all your views to it as children, right? Try removing the first rule; that rule is asking the text view to be below the same view it is in.

EDIT: Also a help is explicitly setting the id of the view you are laying out relative to.

So here:

text1.setId(2);
p.addRule(RelativeLayout.BELOW,2);

1 Comment

With or without that part removed it seemed to work. However, I think you're right, it would be better practice to not set it below the relative layout.
0

you can use xml layout for this :

in relative layout u set the first textview and assign it some id fot the next text view we can assign parameter android:layout_below="id of above text view" in this way we get 2nd text view below 1st text view

Comments

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.