Badrinath
Badrinath

Reputation: 355

Textview not getting displayed to rightOf another textView programatically in android

I am trying to add textview dynamically to the right of the textView , but textView is not getting displayed.

The statement "params2.addRule(RelativeLayout.RIGHT_OF, 1);" in the below code is creating a problem if i remove that line the textview is getting displayed at random position.

Need to implement this xml dynamically

<TextView
            android:id="@+id/stationnameVal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="KANYAKUMARI (CAPE)"
            android:textColor="@color/black"
            android:textSize="12sp" />

        <TextView
            android:id="@+id/arrivaltimeVal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="45dp"
            android:layout_toRightOf="@+id/stationnameVal"
            android:text="SRC"
            android:textColor="@color/black"
            android:textSize="12sp" />

Code is :

    RelativeLayout rel = new RelativeLayout(this);
    // first TextView
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

        TextView stateName = new TextView(DisplayActivity.this);
        stateName.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));
        params.setMargins(0, 5, 0, 0);
        stateName.setText("KANYAKUMARI (CAPE)");
        stateName.setId(1);
        stateName.setTextColor(R.color.black);
        stateName.setTextSize(12);
        rel.addView(stateName, params);

    // Adding Second TextView
       RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

       TextView arr = new TextView(DisplayActivity.this);
       arr.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));
       arr.setText("SRC");
       arr.setTextColor(R.color.white);
       arr.setTextSize(12);
       params2.addRule(RelativeLayout.ALIGN_PARENT_TOP, stateName.getId());
       params2.addRule(RelativeLayout.RIGHT_OF, 1);
       params2.setMargins(45, 0, 0, 0);
       rel.addView(arr, params2);

Upvotes: 0

Views: 385

Answers (2)

Archie.bpgc
Archie.bpgc

Reputation: 24012

change this:

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

or this:

rel.addView(stateName, params);

to

rel.addView(stateName);

and also change this:

params2.addRule(RelativeLayout.ALIGN_PARENT_TOP, stateName.getId());

to

params2.addRule(RelativeLayout.ALIGN_PARENT_TOP);

and do the same for param:

params.addRule(RelativeLayout.ALIGN_PARENT_TOP);

otherwise remove it, because Kanyakumari and src are not aligned properly.

actually do this to align them:

params2.addRule(RelativeLayout.ALIGN_TOP, 1);

Upvotes: 1

Ziteng Chen
Ziteng Chen

Reputation: 1969

How about changing the "LayoutParams.FILL_PARENT" to "LayoutParams.WRAP_CONTENT"?

Regards

Ziteng Chen

Upvotes: 1

Related Questions