1

My app has edittext and a button. Everytime I click a button a new textview is added to activity. Below is the code:

public void novVnos (View v){
    EditText eText = (EditText) findViewById(R.id.editText1);
    RelativeLayout mLayout = (RelativeLayout) findViewById(R.id.relativeLayout);

    mLayout.addView(createNewTextView(eText.getText().toString()));
}

private TextView createNewTextView (String text){
    final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    final TextView textView = new TextView(this);
    int counter = 0;

    counter += 1;
    textView.setLayoutParams(lparams);
    textView.setText(counter + ". " + text);
    return textView;
}

Now the thing is that whenever I add a new textview using the button, it is automatically placed in the upper left corner of the activity, but I want it somewhere else. I have already (manually) added a new textview to this activity and I want every new textview to be automatically placed UNDER this textview that already exists.

2 Answers 2

3

I'm not sure if you really need to have a RelativeLayout there, I strongly suggest using a LinearLayout instead and setting the orientation to vertical, which would make all views be added below each other.

If you really need to have a RelativeLayout, there is of course a way also. But you will have to specify the correct LayoutParams. Each of the default viewgroups have it's own LayoutParams class. Using RelativeLayout.LayoutParams - especially the method addRule(RelativeLayout.BELOW, idOfOtherView) - you can tell a view to be below another view.

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

1 Comment

yeah, that is what I call it thinking :)
1

Use a LinearLayout with vertical orientation on your XML and no a RelativeLayout.

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.