How to Create and Delete the Dynamic View.
//Create the Main xml file for adding and delete the view.<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.intel.addedittext.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:id="@+id/linearPlus"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:orientation="vertical">
</LinearLayout>
<Button
android:id="@+id/btPlus"
android:layout_width="62dp"
android:layout_height="62dp"
android:text="+"
android:textColor="#000000" />
</LinearLayout>
<Button
android:id="@+id/btnClick"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CLick"
android:textColor="#000000" />
</LinearLayout>
//Create the raw item for creating the view.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/root"
android:layout_marginTop="8dp"
>
<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>
<Button
android:id="@+id/btnDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Delete"/>
</LinearLayout>
//Create the model class for creating the view
public class ModelBookUI { public EditText book_name; public EditText author; public Button delete; }
//Creating the main Java file for creating the view
public class MainActivity extends AppCompatActivity { List<ModelBookUI> UIArray = new ArrayList<>(); int position = 0; LinearLayout plusLinearLayout; Button plusButton; String name[]; String author[]; JSONArray jsonArray = new JSONArray(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); plusLinearLayout = (LinearLayout) findViewById(R.id.linearPlus); //Getting the Button ID plusButton = (Button) findViewById(R.id.btPlus); Button clickButton = (Button) findViewById(R.id.btnClick); clickButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { JSONArray jsonObject = getObject(); String mName = jsonObject.toString(); Log.d("Json::", "Json::" + mName); } }); plusButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { position = UIArray.size(); if (position < 5) { UIArray.add(AddView("lay" + position, position, plusLinearLayout)); } else { Toast.makeText(MainActivity.this, "Limit exeeds", Toast.LENGTH_LONG).show(); } } }); } private JSONArray getObject() { for (int j = 0; j < UIArray.size(); j++) { EditText bookName = (EditText) UIArray.get(j).book_name; EditText authorName = (EditText) UIArray.get(j).author; for (int i = j; i <= j; i++) { JSONObject object = new JSONObject(); try { object.put("surveyid", bookName.getText().toString()); object.put("question", authorName.getText().toString()); } catch (JSONException e) { e.printStackTrace(); } jsonArray.put(object); Log.i("Object::", "Object::" + jsonArray.put(object)); } } return jsonArray; } /**********************Add new view**************************/
private ModelBookUI AddView(String tag, int position, final 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); Button delete = (Button) child.findViewById(R.id.btnDelete); 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; model.delete = delete; parent.addView(child); delete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (UIArray.size() == 0) { Toast.makeText(MainActivity.this, "Nothing to remove", Toast.LENGTH_SHORT).show(); } else { parent.removeView((View) v.getParent()); UIArray.remove(UIArray.size() - 1); } } }); return model; } }
No comments:
Post a Comment