1

Can I add linearLayouts which is defined in XML in another linearLayout which has a parent containing many layouts. For example in this case I have a parent LinearLayout having to element: a scrollView and a LinearLayout. The scrollView has a LinearLayout which is initially defined empty in the XML. I want to add layouts to this LinearLayout so that if the user exceeds the bottom of the screen then the layouts can be in scrollView.

public class ActivityMain extends Activity implements OnFocusChangeListener{


EditText date;
LinearLayout row[]=new LinearLayout[30];
int rowNo=0;
LinearLayout itemForm=(LinearLayout)LinearLayout.inflate(this, R.id.item_form,null);
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    date=(EditText)findViewById(R.id.date);
    date.setOnFocusChangeListener(this);


}

@Override
public void onFocusChange(View arg0, boolean arg1) {
    // TODO Auto-generated method stub
    if(arg1==false){

        row[rowNo++]=(LinearLayout)findViewById(R.layout.row);
        itemForm.addView(row[rowNo]);

    }
}

 }

This is the XML layout containing the parent LinearLayout with its scrollView and LinearLayout

<LinearLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ActivityMain" 
android:orientation="vertical"
android:id="@+id/linearLayout"
android:weightSum="10">

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="8"
    android:orientation="vertical" >

<AutoCompleteTextView
    android:id="@+id/autoCompleteTextView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:text="AutoCompleteTextView" >

    <requestFocus />
</AutoCompleteTextView>

<EditText
    android:id="@+id/date"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="date" />

<TextView  android:id="@+id/code"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:text="voucher Code"
    android:textSize="30sp"/>

</LinearLayout>
<ScrollView android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:layout_weight="2">  
<LinearLayout android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:id="@+id/item_form"
              android:orientation="vertical">


</LinearLayout>
</ScrollView>

</LinearLayout>

LinearLayout that i want to inflate in the other linearLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="@+id/row" >


<EditText android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="Enter item name"
            android:id="@+id/item"/>

  <EditText
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:inputType="number"
  android:hint="Quantity"
  android:id="@+id/quant" />

  <EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:inputType="numberDecimal"
      android:hint="Rate"
      android:id="@+id/quant" />

 </LinearLayout>
kayveesin
  • 433
  • 1
  • 4
  • 17

1 Answers1

3

Try using following way to inflate xml :

View viewToLoad = LayoutInflater.from(
            getApplicationContext()).inflate(
            R.layout.yourlayoutfilename, null);
    ((LinearLayout) findViewById(R.id.item_form)).addView(viewToLoad);

And for Addview :

LinearLayout itemForm=(LinearLayout) viewToLoad.findViewById(R.id.item_form);
itemForm.addView(.......);
Hardik Joshi
  • 9,477
  • 12
  • 61
  • 113