0

image

When click on add phone text I want to add view dynamically (view below add phone: where 'mobile' is textview and on right side there is edit text)

and this need to be for N level ..when user click on add phone view will be added..

How to do this.?

Yahya
  • 706
  • 8
  • 23
  • i tried to add view dynamic but not getting view like display in image and also i am able to add only one view..and also getting problem to fetch ID of that edittext added dynamically – Vivek Tilva Nov 27 '17 at 06:55
  • lol @KulsDroid wants to say that what code have you tried so far? – Abhishek Singh Nov 27 '17 at 06:56
  • Possible duplicate of [this](https://stackoverflow.com/questions/6216547/android-dynamically-add-views-into-view) – Hemant Parmar Nov 27 '17 at 07:00
  • try something similar to this https://stackoverflow.com/questions/40604016/android-how-to-display-more-views-after-clicking-a-button/40606522?noredirect=1#comment68514234_40606522 – Ramesh Kanuganti Nov 27 '17 at 07:07
  • by this: LinearLayout ll = (LinearLayout)findViewById(R.id.l_phone); // add edittext EditText et = new EditText(this); LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); et.setLayoutParams(p); et.setText("Text"); et.setTextColor(getResources().getColor(R.color.black)); et.setId(numberOfLines + 1); ll.addView(et); numberOfLines++; i added edittext but i am not able to achieve design similar to image i upload – Vivek Tilva Nov 27 '17 at 07:17
  • Create a child xml which contains view which you want to display again and again. Create layout in your existing xml and add child xml with the use of view inflater – Kuls Nov 27 '17 at 07:19
  • Create custom arraylist and store your edittext value into that arraylist so you can create dynamic views from the size of Arraylist – Kuls Nov 27 '17 at 07:20
  • by view inflater i created view dynamic for multiple times..thanks for help@KulsDroid...one more help i need .. i have submit button to save data, how will i get data from edittext that created dynamically ?? i mean getting ID would be problem – Vivek Tilva Nov 27 '17 at 07:33

2 Answers2

1

Try this code.This might help you.

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

<Button
    android:id="@+id/btn_Click"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:text="Click" />

<LinearLayout
    android:id="@+id/linearView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" />
</LinearLayout>

In your Activity class on button Click just use the below method.And make sure to declare an int variable (index) that will help you add new View to the end.

 int index=0;
 linearView = (LinearLayout) findViewById(R.id.linearView);

@OnClick(R.id.btn_Click)
public void click() {
    LinearLayout mainLinearLayout = new LinearLayout(this);
    mainLinearLayout.setOrientation(LinearLayout.VERTICAL);

    LinearLayout.LayoutParams mainParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    mainLinearLayout.setLayoutParams(mainParams);
    mainLinearLayout.setGravity(Gravity.CENTER);

    LinearLayout firstChildLinearLayout = new LinearLayout(this);
    firstChildLinearLayout.setOrientation(LinearLayout.HORIZONTAL);

    LinearLayout.LayoutParams firstChildParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
    firstChildLinearLayout.setLayoutParams(firstChildParams);

    TextView textView = new TextView(this);
    LinearLayout.LayoutParams txtParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
    txtParams.setMarginStart(5);
    txtParams.setMarginEnd(10);
    textView.setGravity(Gravity.CENTER_VERTICAL);
    textView.setLayoutParams(txtParams);

    textView.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.ic_foreground), null, null, null);
    textView.setText("mobile  >");

    EditText editText = new EditText(this);
    LinearLayout.LayoutParams etParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
    etParams.weight = 1;
    editText.setBackground(null);
    editText.setLayoutParams(etParams);


    firstChildLinearLayout.addView(textView, 0);
    firstChildLinearLayout.addView(editText, 1);

    LinearLayout secondChildLinearLayout = new LinearLayout(this);
    secondChildLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
    secondChildLinearLayout.setBackgroundColor(getResources().getColor(R.color.color_grey));

    LinearLayout.LayoutParams secondChildParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1);
    secondChildLinearLayout.setLayoutParams(secondChildParams);


    mainLinearLayout.addView(firstChildLinearLayout, 0);
    mainLinearLayout.addView(secondChildLinearLayout, 1);

    linearView.addView(mainLinearLayout, index);

    index++;
}

So to get values from EditText use following code. There I have displayed the value in Toast, you can use an array of String to store all dynamically created EditText values.

    public void getAllEditTextValues(View view) {
    View v = null;
    for (int i = 0; i < linearView.getChildCount(); i++) {
        v = linearView.getChildAt(i);
        if (v instanceof LinearLayout) {
            View tempView = ((LinearLayout) v).getChildAt(0);
            View et = ((LinearLayout) tempView).getChildAt(1);
            String etValue = null;
            if (et instanceof EditText) {
                etValue = ((EditText) et).getText().toString();
            }
            Toast.makeText(this, "" + etValue, Toast.LENGTH_SHORT).show();
            // Use Array to Store all values of EditText
        }
    }
}
Manish Singh Rana
  • 822
  • 1
  • 13
  • 26
  • thanks for this but i think u missed my comment before write this ans, i already get this by creating child xml and inflate view of that child xml..anyway thank you for this.. just one issue i am facing now how to get value of that dynamic created edittext? – Vivek Tilva Nov 27 '17 at 08:50
  • That we can get as we know the hierarchy of layout that we have created dynamically. There we just have to check whether the view has the instance of EditText or not. – Manish Singh Rana Nov 27 '17 at 10:04
  • As we know that EditText is second child of linear Layout, that's why I have used index 1 to get a view of EditText (i.e getChildAt(1)). This might help you. – Manish Singh Rana Nov 27 '17 at 10:14
  • thank you..i am getting value of edittext but UI is not proper as per image.. when i add view dynamic..it is added above my 'add phone' text and horizontal gray line is also missing.. can u help with this? really appreciate your help..thank you – Vivek Tilva Nov 28 '17 at 09:52
  • Alright, I will help you with that missing horizontal gray line. – Manish Singh Rana Nov 29 '17 at 04:55
  • To Add a missing horizontal gray line, we'll slightly change the code to inflate dynamic view, therefore code to get all value of edit text needs to be adjusted as per new inflated dynamic view. – Manish Singh Rana Nov 29 '17 at 05:40
  • @VivekTilva kindly try now as I have made some adjustments, this will surely help you. – Manish Singh Rana Nov 29 '17 at 05:48
0

Create XML as "custom_row.xml" with code:

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="horizontal"
          android:layout_width="match_parent"
          android:layout_height="wrap_content">
<ImageView
    android:layout_width="0dp"
    android:layout_weight="0.1"
    android:id="@+id/crossImage"
    android:layout_gravity="center_vertical"
    android:layout_height="wrap_content"
    android:src="@drawable/cross_image"/>

<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="0.45"
    android:id="@+id/columnSpinner"
    android:text="demo"

    android:layout_gravity="center"/>

<EditText
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="0.45"
    android:id="@+id/contentValueEditText"/>

</LinearLayout>

Now make you activity layout as:

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

<LinearLayout
android:id="@+id/linearView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
 <Button
android:id="@+id/btn_Click"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Click" />


</LinearLayout>

Now do code as:

  int index=0;
  linearView = (LinearLayout) findViewById(R.id.linearView);

 @OnClick(R.id.btn_Click)
 public void click() {
final LinearLayout singleInsertView1 = (LinearLayout) LayoutInflater.from(QueryRunner.this).inflate(R.layout.insert_single_item, null);

   EditText editText = (EditText)singleInsertView1.findViewById(R.id.contentValueEditText);
   editText.setTag("EditText"+index);

   singleInsertView1.setTag("LinearLayout"+index);
   contentViewAdder.addView(singleInsertView1);

   index++;
}

Now you can access the Edit Text by following code:

 LinearLayout singleLayout = (LinearLayout)linearView.findViewWithTag("LinearLayout"+index);
 EditText editText = singleLayout.findViewWithTag("EditText"+index);
halfer
  • 19,824
  • 17
  • 99
  • 186
Ankit Patidar
  • 2,731
  • 1
  • 14
  • 22