Create Dynamic views in Andorid Studio.
Creating New Project.
Open android studio and create a new project.
File => New => New Project => Configure your new project => Select the form factor yours app will run on => Add an Activity to Mobile => Customize the Activity => Finish.
Create Xml file in project.
Open => app => res => layout - activity_main.xml.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="desktop.laptop.com.customdemo.MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/linearPlus"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:layout_marginTop="10dp"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginTop="16dp"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/btPlus"
android:layout_width="32dp"
android:layout_height="36dp"
android:text="+"
android:textColor="#000000" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
Create Add View file in project.
Open => app => res => layout - add_view.xml.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:background="#cccccc"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="18dp"
android:orientation="horizontal">
<TextView
android:id="@+id/txtBook"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Guide(s)"
android:textColor="#000000" />
<EditText
android:id="@+id/etBook"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="68dp"
android:ems="10"
android:inputType="textEmailAddress"
android:paddingLeft="8dp"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="18dp"
android:layout_marginTop="12dp"
android:orientation="horizontal">
<TextView
android:id="@+id/txtAuthor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Author/Publisher"
android:textColor="#000000" />
<EditText
android:id="@+id/etAuthor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:ems="10"
android:inputType="textEmailAddress"
android:paddingLeft="8dp"
android:textColor="#000000" />
</LinearLayout>
</LinearLayout>
Create the Java file in project.
Open app => main => src = MainActivity.java.
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
List<ModelBook> dataArray = new ArrayList<>();
List<ModelBookUI> UIArray = new ArrayList<>();
int position = 0;
Button plusButton;
LinearLayout plusLinearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
plusLinearLayout = (LinearLayout) findViewById(R.id.linearPlus);
plusButton = (Button) findViewById(R.id.btPlus);
plusButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
position = UIArray.size();
UIArray.add(AddView("lay" + position, position, plusLinearLayout));
}
});
}
/**********************
* Add new view
**************************/
private ModelBookUI AddView(String tag, int position, LinearLayout parent) {
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
View child = inflater.inflate(R.layout.add_book, parent, false);
EditText book = (EditText) child.findViewById(R.id.etBook);
EditText author = (EditText) child.findViewById(R.id.etAuthor);
LinearLayout root = (LinearLayout) child.findViewById(R.id.root);
root.setTag(tag);
ModelBookUI model = new ModelBookUI();
model.book_name = book;
model.author = author;
parent.addView(child);
return model;
}
}
Create the Java file in project.
Open app => main => src = ModelBook.java.
public class ModelBook {
public String book;
public String author;
}
Create the Java file in project.
Open app => main => src = ModelBookUI.java.
import android.widget.EditText;
public class ModelBookUI {
public EditText book_name;
public EditText author;
}
Screen Shot:-
May this code help you. Thanks!!!!!!
Creating New Project.
Open android studio and create a new project.
File => New => New Project => Configure your new project => Select the form factor yours app will run on => Add an Activity to Mobile => Customize the Activity => Finish.
Create Xml file in project.
Open => app => res => layout - activity_main.xml.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="desktop.laptop.com.customdemo.MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/linearPlus"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:layout_marginTop="10dp"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginTop="16dp"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/btPlus"
android:layout_width="32dp"
android:layout_height="36dp"
android:text="+"
android:textColor="#000000" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
Create Add View file in project.
Open => app => res => layout - add_view.xml.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:background="#cccccc"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="18dp"
android:orientation="horizontal">
<TextView
android:id="@+id/txtBook"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Guide(s)"
android:textColor="#000000" />
<EditText
android:id="@+id/etBook"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="68dp"
android:ems="10"
android:inputType="textEmailAddress"
android:paddingLeft="8dp"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="18dp"
android:layout_marginTop="12dp"
android:orientation="horizontal">
<TextView
android:id="@+id/txtAuthor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Author/Publisher"
android:textColor="#000000" />
<EditText
android:id="@+id/etAuthor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:ems="10"
android:inputType="textEmailAddress"
android:paddingLeft="8dp"
android:textColor="#000000" />
</LinearLayout>
</LinearLayout>
Create the Java file in project.
Open app => main => src = MainActivity.java.
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
List<ModelBook> dataArray = new ArrayList<>();
List<ModelBookUI> UIArray = new ArrayList<>();
int position = 0;
Button plusButton;
LinearLayout plusLinearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
plusLinearLayout = (LinearLayout) findViewById(R.id.linearPlus);
plusButton = (Button) findViewById(R.id.btPlus);
plusButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
position = UIArray.size();
UIArray.add(AddView("lay" + position, position, plusLinearLayout));
}
});
}
/**********************
* Add new view
**************************/
private ModelBookUI AddView(String tag, int position, LinearLayout parent) {
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
View child = inflater.inflate(R.layout.add_book, parent, false);
EditText book = (EditText) child.findViewById(R.id.etBook);
EditText author = (EditText) child.findViewById(R.id.etAuthor);
LinearLayout root = (LinearLayout) child.findViewById(R.id.root);
root.setTag(tag);
ModelBookUI model = new ModelBookUI();
model.book_name = book;
model.author = author;
parent.addView(child);
return model;
}
}
Create the Java file in project.
Open app => main => src = ModelBook.java.
public class ModelBook {
public String book;
public String author;
}
Create the Java file in project.
Open app => main => src = ModelBookUI.java.
import android.widget.EditText;
public class ModelBookUI {
public EditText book_name;
public EditText author;
}
Screen Shot:-
Button Click |
Add View |
May this code help you. Thanks!!!!!!
No comments:
Post a Comment