0

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.

Here is the block

It's a block with 4 layouts, the big one horizontal, left one vertical, 2 right horizontals.

Layouts

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Beier, Thanks a lot, I can now add my AXML layout, but only one time. By the way, i'm interested in the second part. I will try adding a custom view (even if now I don't know what this means ^^ I'm going to study). And how could I store them after?
really there is NO documentation in c# about custom views? I can't fine anything...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.