Thursday, 2 April 2015

Adding Dynamic Views in Linear Layout


How to add dynamic views in a parent view (Can be linear or relative etc.)


Here are the steps :


1. MainActivity.java

public class MainActivity extends ActionBarActivity {


LinearLayout mDynamicLinearLayout;
LayoutInflater __inflater;
Button mAddRow;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initiViews();
}

private void initiViews() {
mDynamicLinearLayout=(LinearLayout)findViewById(R.id.mDynamicLinearLayout);
mAddRow=(Button)findViewById(R.id.mAddRow);
mAddRow.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
addDynamicViews();
}
});
}

private void addDynamicViews() {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View _itemRow = inflater.inflate(R.layout.dynamic_row, null);
final EditText name=(EditText)_itemRow.findViewById(R.id.name);
final EditText password=(EditText)_itemRow.findViewById(R.id.password);
Button mShow=(Button)_itemRow.findViewById(R.id.mShow);
mShow.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, name.getText().toString()+ "pwd "+password.getText().toString(), Toast.LENGTH_LONG).show();
}
});
mDynamicLinearLayout.addView(_itemRow);
}

}



XML layouts goes here : 


1. activity_main.xml 

<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:orientation="vertical" >

    <Button
        android:id="@+id/mAddRow"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Add" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

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

</LinearLayout>


2. dynamic_row.xml

<?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="vertical" >

    <EditText
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:hint="username" />

    <EditText
        android:id="@+id/password"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:hint="password" />

    <Button
        android:id="@+id/mShow"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Show Values" />

</LinearLayout>


Output :







No comments:

Post a Comment