0

I am new in android and tried to learn how to make an add button which add Views in a certain view dynamically when clicked. But i meet the problem that not sure how to set id for each item in the layout which i want to insert into another view. Here is the main_view:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="horizontal"
tools:context=".MainActivity">

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

<Button
    android:id="@+id/add_button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/container_layout"
    android:text="add"
    />



</RelativeLayout>

Here is my code:

public class MainActivity extends AppCompatActivity {
public int index_num;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button add_button = findViewById(R.id.add_button);
    final LayoutInflater layoutInflater = getLayoutInflater();
    final ViewGroup insertPoint = findViewById(R.id.container_layout);
    index_num = 0;

    add_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            View new_view = layoutInflater.inflate(R.layout.new_layout,insertPoint, false);
            insertPoint.addView(new_view);
            index_num++;
        }
    });
  }
}

And here is the layout which i want to insert the main view which includes 3 Edittext:

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

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:inputType="number"/>
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:inputType="number"/>
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:inputType="number"/>
</LinearLayout>

Can someone teach me how to set different id for the three edittext when i pressed the add button?

Harbour
  • 1
  • 1
  • please search first for an answer to your problem there is one https://stackoverflow.com/questions/1714297/android-view-setidint-id-programmatically-how-to-avoid-id-conflicts – PedroAGSantos Aug 15 '20 at 09:36

2 Answers2

0

you can use these ways

use View

view.setId(View.generateViewId());

use ViewCompat

ViewCompat.generateViewId()

use ConstraintLayout

ConstraintLayout.generateViewId()
Rasoul Miri
  • 5,433
  • 43
  • 52
0

As @Rasoul Miri said, you can use View.generateViewId() to set id for newly inserted views, and you should use a list to record them. And also you should set the id for those three items.

like these:

for the inserted view

<EditText
    android:id="@+id/one_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:inputType="number"/>
<EditText
    android:id="@+id/two_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:inputType="number"/>
<EditText
    android:id="@+id/three_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:inputType="number"/>

for the MainActivity

public ArrayList<InsertedView> list;

public void addView() {
    View new_view = layoutInflater.inflate(R.layout.new_layout,insertPoint, false);
    insertPoint.addView(new_view);
    index_num++;
    InsertedView insertedView = new InsertedView(View.generateViewId(), View.generateViewId(), View.generateViewId());
    new_view.findViewById(R.id.one_view).setId(insertedView.firstId);
    new_view.findViewById(R.id.two_view).setId(insertedView.secondId);
    new_view.findViewById(R.id.three_view).setId(insertedView.thirdId);
    list.add(insertedView);
}

class InsertedView {
    public int firstId;
    public int secondId;
    public int thirdId;
    public InsertedView(int one, int two, int three) {
        firstId = one;
        secondId = two;
        thirdId = three;
    }
}