0

I am trying to add some buttons to a Relative Layout programatically. I used some examples seen on StackOverflow, but for some reason I cannot understand, my code is not working: I want to have my buttons one above the other, but they end up being one on top of the other. Here is my layout file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mainLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.android.myApp.MainActivity" >

</RelativeLayout>

And here is my code:

    mLayout = (RelativeLayout) findViewById(R.id.mainLayout);
    RelativeLayout.LayoutParams lprams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                                                                         RelativeLayout.LayoutParams.WRAP_CONTENT);

    Button btnTag = new Button(this);
    btnTag.setText("Button0");
    btnTag.setId(0);
    btnTag.setOnClickListener(mGlobalOnCLickListener);
    mLayout.addView(btnTag, lprams);
    lprams.addRule(RelativeLayout.BELOW, 0);

    btnTag = new Button(this);
    btnTag.setText("Button2");
    btnTag.setId(1);
    btnTag.setOnClickListener(mGlobalOnCLickListener);
    mLayout.addView(btnTag, lprams);
5
  • Use linear layout instead relative layout Commented Sep 23, 2014 at 6:38
  • OK I could try that, but I still do not understand what is wrong with my code... Commented Sep 23, 2014 at 6:50
  • The problem is in this line lprams.addRule(RelativeLayout.BELOW, 0); Commented Sep 23, 2014 at 6:56
  • More something like this: Commented Sep 23, 2014 at 6:59
  • p.addRule(RelativeLayout.ALIGN_BOTTOM, tv.getId()); Commented Sep 23, 2014 at 7:00

1 Answer 1

1

Checkout this code....Hope this will help

mLayout = (RelativeLayout) findViewById(R.id.mainLayout); RelativeLayout.LayoutParams lprams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

Button btnTag = new Button(this);
btnTag.setText("Button0");
btnTag.setId(10);
btnTag.setOnClickListener(mGlobalOnCLickListener);
mLayout.addView(btnTag, lprams);


lprams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                         RelativeLayout.LayoutParams.WRAP_CONTENT);

lprams.addRule(RelativeLayout.BELOW, 10);
btnTag = new Button(this);
btnTag.setText("Button2");
btnTag.setId(1);
btnTag.setOnClickListener(mGlobalOnCLickListener);
mLayout.addView(btnTag, lprams);                                        
Sign up to request clarification or add additional context in comments.

3 Comments

Nope, I still have the same issue.
Can you check once with different ID for first button other that "0"?
Asmita, with the new version you posted It works fine. Seems the problem was indeed in the values. Do not know why 0 and 1 or 1 and 2 do not work. Thanks a lot!!

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.