Nikhil
Nikhil

Reputation: 167

How to add below layout programatically

I have below layout in xml. But I want to add few more similar rows(RelativeLayouts one below another) dynamically with same layout. I've tried the below code, but all the values are overlapping.

Also I don't know how to add textview one below another programmatically. Any help in solving this would be helpful. Thanks in advance.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ccc">

    <LinearLayout
        android:id="@+id/searchResultsLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:background="#fff"
            android:layout_marginStart="20dp"
            android:layout_marginEnd="20dp"
            android:layout_height="100dp"
            android:padding="6dip" >

            <TextView
                android:id="@+id/firstLine"
                android:layout_width="fill_parent"
                android:layout_height="26dp"
                android:ellipsize="marquee"
                android:text="Ali Connors"
                android:textSize="16sp" />

            <TextView
                android:id="@+id/secondLine"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/firstLine"
                android:gravity="center_vertical"
                android:text="Brunch this weekend?"
                android:textSize="12sp" />

            <TextView
                android:id="@+id/rightTime"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="right"
                android:text="15m"
                android:textSize="16sp" />

        </RelativeLayout>
    </LinearLayout>

</LinearLayout>

Code:

LinearLayout ll = (LinearLayout) findViewById(R.id.searchResultsLayout);

        for (int i = 0; i <1; i++) {

            RelativeLayout row= new RelativeLayout(this);
            RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
            relativeParams.setMargins(20, 5, 20, 5);

            row.setBackgroundColor(Color.parseColor("#ffffff"));
            ll.setLayoutParams(relativeParams);
            TextView name = new TextView(this);
            TextView time = new TextView(this);
            TextView dest = new TextView(this);

            name.setText("Name");
            time.setText("9hrs");
            dest.setText("Destination");
            row.addView(name);
            row.addView(time);
            row.addView(dest);
            ll.addView(row,i);
        }

Upvotes: 0

Views: 1791

Answers (3)

Jins Lukose
Jins Lukose

Reputation: 707

in order to get relative Layout one below other just use listview and adapter. it will automatically generate when you get data

Upvotes: 0

vishnu R
vishnu R

Reputation: 37

 LinearLayout ll = (LinearLayout) findViewById(R.id.searchResultsLayout);
    ll.setOrientation(LinearLayout.VERTICAL);

    for (int i = 0; i <1; i++) {

        RelativeLayout row= new RelativeLayout(this);
        RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        relativeParams.setMargins(20, 5, 20, 5);

        row.setBackgroundColor(Color.parseColor("#ffffff"));
        ll.setLayoutParams(relativeParams);
        TextView name = new TextView(this);
        TextView time = new TextView(this);
        TextView dest = new TextView(this);
        final RelativeLayout.LayoutParams paramsval =
                new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams
                (RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);

        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        name.setText("Name");
        name.setId(View.generateViewId());

        time.setText("9hrs");
        dest.setText("Destination");

        time.setLayoutParams(layoutParams);
        row.addView(time);
        row.addView(name);
        paramsval.addRule(RelativeLayout.BELOW,name.getId());
        dest.setLayoutParams(paramsval);
        row.addView(dest);
        ll.addView(row,i);
    }

Upvotes: 1

Ankit
Ankit

Reputation: 1068

just replace your code with below code:

here is java code

    setContentView(R.layout.myview_activity);

    LinearLayout ll = (LinearLayout) findViewById(R.id.ll);

    for (int i = 0; i <5; i++) {

        LinearLayout row= new LinearLayout(this);

        LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        layoutParams.setMargins(20, 5, 20, 5);
        row.setLayoutParams(layoutParams);
        row.setBackgroundColor(Color.parseColor("#000000"));

        LinearLayout.LayoutParams layoutParams1=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams1.setMargins(0,0,20,0);

        TextView name = new TextView(this);
        name.setLayoutParams(layoutParams1);

        TextView time = new TextView(this);
        time.setLayoutParams(layoutParams1);

        TextView dest = new TextView(this);
        dest.setLayoutParams(layoutParams1);


        name.setText("Name");
        time.setText("9hrs");
        dest.setText("Destination");

        row.addView(name);
        row.addView(time);
        row.addView(dest);

        ll.addView(row);
    }

}

Upvotes: 1

Related Questions