Home Ask Login Register

Developers Planet

Your answer is one click away!

shailesh kumar February 2016

How to add Relative layout in Relative layout dynamically

My project file has XML like root xml. In root xml i define a Relative layout like below

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

I have another xml file first is one.xml and another is two.xml like below

one.xml

     <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone"
        android:id="@+id/two_video_layout">

             <TextView
                    android:id="@+id/two_video_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:singleLine="true"
                    android:text="Video Title" />


</RelativeLayout>

and another layout is

two.xml

<?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="gone"
            android:id="@+id/two_video_layout">

                 <Imageview
                        android:id="@+id/image"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                         />


    </RelativeLayout>

How can we add one.xml or two.xml dynamically

Answers


AKSiddique February 2016

You have to add the view using LayoutParams.

LinearLayout linearLayout = new LinearLayout(this);

RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
        LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
relativeParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);

    parentView.addView(linearLayout, relativeParams);

To relatively position your items programmatically you have to assign ids to them.

TextView tv1 = new TextView(this);
tv1.setId(1);


Amit Vaghela February 2016

Try something like this, where rootLayout is for root.xml

RelativeLayout two_video_layout=new RelativeLayout(this);
rootLayout.addView(two_video_layout);


Kapil Rajput February 2016

You can use LayoutInflater like this :

LayoutInflater inflater=(LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.viewYou_want_to_inflate, null);
// to inflate one.xml call 
View v = inflater.inflate(R.layout.one, null);
TextView textView=(TextView)v.findViewById(R.id.two_video_title); // to get textview object from one.xml
// to inflate two.xml call 
View v = inflater.inflate(R.layout.two, null);

and to add a view in a root view call addView function like this :

RelativeLayout root_layout=(RelativeLayout)findViewById(R.id.root_view);
layout.addView(v);

Post Status

Asked in February 2016
Viewed 2,592 times
Voted 8
Answered 3 times

Search




Leave an answer


Quote of the day: live life