I am using LinearLayout.AddView to add simple views.
I can add things, like textViews and editTexts, but I need to add 4 elements at once in a block with some parameters.
This is the block I would add dynamically.
It's a block with 4 layouts, the big one horizontal, left one vertical, 2 right horizontals.
And here is the AXML for this
<?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:background="@android:color/holo_blue_light"
android:minWidth="25px"
android:minHeight="25px">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="88.0dp"
android:id="@+id/linearLayout1"
android:minWidth="25px"
android:minHeight="25px"
android:weightSum="100">
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="@+id/linearLayout2"
android:layout_weight="60">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText1"
android:hint="Servizio" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText2"
android:hint="Descrizione" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="@+id/linearLayout3"
android:layout_weight="20"
android:layout_gravity="center_vertical">
<EditText
android:inputType="number"
android:layout_width="32.5dp"
android:layout_height="wrap_content"
android:id="@+id/editText3"
android:layout_gravity="center_vertical" />
<TextView
android:text="€"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView1"
android:layout_gravity="center_vertical" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="@+id/linearLayout4"
android:weightSum="10"
android:layout_weight="20">
<EditText
android:inputType="number"
android:layout_width="32.5dp"
android:layout_height="wrap_content"
android:id="@+id/editText4"
android:layout_gravity="center_vertical" />
<TextView
android:text="€"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView1"
android:layout_gravity="center_vertical" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
And you can imagine I can't achieve something like this adding elements one by one. And every time user wants, he should be able to iterate and add more of these blocks.
I dunno if i'm doing it the correct way, because even if I can add dynamically this block, how am I supposed to retrieve information in every single field?
Thanks
To add the views dynamically, when the action is performed that causes the addition (say button click), inflate the layout and add it to the parent.
example:
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
View view = layoutInflater.inflate(R.layout.layout_file, rootView);
linearLayout.addView(view);
}
});
However, as you want to have access to the input in all of these text fields, I'd suggest adding a custom view that uses the layout you've provided. This can then provide access to your EditTexts input. Store them either in a List or iterate through the LinearLayout's children at a later date.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.