0

I have got a problem by implementing relative layout from code. What I want, that TextView1 is located to the left, and TextView2 to the right of the screen at one line.

It works fine from XML, but doing this from code do not... what could be the reason?

This work just fine:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="@string/hello_world" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="TextView" />

</RelativeLayout>

This doesn't work(giving an overlapping of elements):

RelativeLayout rel = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,
        LayoutParams.WRAP_CONTENT);
rel.setLayoutParams(params);

TextView t1 = new TextView(this);   
t1.setText("balbla_1");
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.MATCH_PARENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
lp1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
t1.setLayoutParams(lp1);

TextView t2 = new TextView(this);
t2.setText("balbla_2");
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.MATCH_PARENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
lp2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
t2.setLayoutParams(lp2);        

rel.addView(t1);
rel.addView(t2);

setContentView(rel);

but this would work fine: (replace MATCH_PARENT with WRAP_CONTENT):

RelativeLayout rel = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,
        LayoutParams.WRAP_CONTENT);
rel.setLayoutParams(params);

TextView t1 = new TextView(this);   
t1.setText("balbla_1");
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
lp1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
t1.setLayoutParams(lp1);

TextView t2 = new TextView(this);
t2.setText("balbla_2");
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
lp2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
t2.setLayoutParams(lp2);        

rel.addView(t1);
rel.addView(t2);

setContentView(rel);
spaaarky21
  • 6,524
  • 7
  • 52
  • 65
user1908375
  • 1,069
  • 1
  • 14
  • 33

1 Answers1

1

The Java code is not an exact match to the XML, the overlapping is because you have forgotten:

lp2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

You have also neglected:

lp1.addRule(RelativeLayout.CENTER_VERTICAL);

And you are using MATCH_PARENT for the widths instead of WRAP_CONTENT.

Sam
  • 86,580
  • 20
  • 181
  • 179
  • sorry.. I just checked the xml code. it works also fine without ..BOTTOM.. tags.. so like: – user1908375 Dec 16 '12 at 20:14
  • I changed the xml.. it works also having there just: layout_alignParentLeft, layout_alignParentRight – user1908375 Dec 16 '12 at 20:17
  • adding lp1.addRule(RelativeLayout.CENTER_VERTICAL); and lp2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); locate elements one in center and one in the bottom.. but what I want is to locate them inline, one to the right and one to the left.. that is also done in xml layout. – user1908375 Dec 16 '12 at 20:23
  • 1
    OK. WRAP_CONTENT did the thing! – user1908375 Dec 16 '12 at 20:24
  • thanks Sam.. whith WRAP_CONTENT it works... I also updated my question. – user1908375 Dec 16 '12 at 20:28
  • Glad I could help, now your XML and Java match! Please click the checkmark to accept this answer. – Sam Dec 16 '12 at 20:29