1

i have in xml tis code:

<RelativeLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/mainLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    <Button
        android:id="@+id/examp1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/examp2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/examp1" />
</RelativeLayout>

I want to create these buttons programmatically and i use:

RelativeLayout mainLayout;
mainLayout = (RelativeLayout) findViewById(R.id.mainLayout);

for (int i=0; i<1; i++)
{
     Button new_button = new Button(this);
     new_button.setText("blabla");
     new_button.setId(i);
     new_button.setLayoutParams(new LayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT,         
                        ViewGroup.LayoutParams.WRAP_CONTENT));

     mainLayout.addView(new_button);
}

How can I translate this command android:layout_below="@id/examp1" programmatically?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Nikitas
  • 649
  • 4
  • 9
  • 18
  • I meant that when you ask a question and when someone answers your question and if that is apt for your question, you will have to accept that answer. So that the users will be interested to answer your question. – Andro Selva Jun 10 '11 at 12:50
  • Thnaks Andro..i will follow your advice – Nikitas Jun 10 '11 at 12:52

1 Answers1

3

Use RelativeLayout.LayoutParams. Use addRule(), for your example use it with RelativeLayout.BELOW and the id (R.id.examp1)

aromero
  • 25,681
  • 6
  • 57
  • 79
  • So, i have to put each button on seperate layout, then use the addRule and then add each button to the layout? – Nikitas Jun 10 '11 at 12:52
  • What do you want to do? If you want to layout buttons one below another then use a LinearLayout with vertical orientation. If you still want to do this with a RelativeLayout you need to do it with the addRule() method over the same RelativeLayout, no need to put each button on a separate layout. – aromero Jun 10 '11 at 12:58