Q:

Import xml to java

when a button is pressed i want to create some elements, like a TextView and a CheckBox. I now it's possible to do this in my java code, but... Is it possible to have a xml file with only those elements inside and call it when the button is pressed?
something like this:
java:
myButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            //create whats in myxml

        }

and thats what i want to create (myxml.xml):
 <LinearLayout
    android:layout_width="fill_parent"
    android:orientation="horizontal"
    android:layout_height="wrap_content"
    android:background="@drawable/checkboxlayout"
    android:layout_marginTop="25px">

    <CheckBox
        android:layout_width="0dp"
        android:layout_weight=".7"
        android:layout_height="wrap_content"/>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_weight=".3"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:background="@drawable/datelayout"
        android:gravity="center"
        >
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            >
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            >

    </LinearLayout>

</LinearLayout>

hope someone can help

A:

One way could be to use the <include tag to include the component layout to the layout of your Activity/Fragment. The content of the component layout is retrievable with findViewById. Alternatively you could a LayoutInflater to convert your xml, in View.
E.g.
View view = getLayoutInflater().inflate(R.layout.name_of_layout, null);

where name_of_layout.xml, contains the component you want to transform. Be aware that the inflater returns a completely new View and it has to be added to the View's hierarchy of your Activity/Fragment if you want to deal with it

