Matteo Codogno
Matteo Codogno

Reputation: 1579

Android dynamically add custom component to LinearLayout

i want add a custom component on a LinearLayout whenever i touch a button.

This is my code:

LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout root = (LinearLayout) findViewById(R.id.layout_cartelle_immagini);

View custom = vi.inflate(R.layout.custom_list_folder, null);
TextView textView = (TextView) custom.findViewById(R.id.label_pathFolder);
textView.setText(pathDirectory);

Spinner spinnerLevel = (Spinner) custom.findViewById(R.id.spinner_level);

try{

root.addView(custom);
}catch(Throwable e) {
    Log.e("BurgerClub", "MEX: " + e.getMessage());
    e.printStackTrace();
}

In this way, only first custom component is added, why?

Thanks

edit: I've modified my code:

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
((ViewGroup) root).addView(custom, myCount, params);

This is my custom component:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/border_bottom"
    android:layout_marginBottom="2dp"
    android:paddingRight="20dp"
    android:paddingLeft="20dp"
    style="@style/Theme.BurgerClubStyle" >

    <TextView
        android:id="@+id/label_pathFolder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:gravity="center"
        android:padding="20dp"
        android:textSize="25sp"
        style="@style/customText" />

    <Spinner
        android:id="@+id/spinner_level"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/btn_show_desc_img"
        android:entries="@array/n_level_array"
        android:padding="20dp"
        android:prompt="@string/n_level_prompt"
        android:textAlignment="3"
        style="@style/customText" />

    <ImageButton
        android:id="@+id/btn_show_desc_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/btn_remove_folder"
        android:layout_marginLeft="20dp"
        android:layout_centerVertical="true"
        android:background="@drawable/button_press"
        android:contentDescription="@string/showDescImg"
        android:src="@drawable/ic_desc" />

    <ImageButton
        android:id="@+id/btn_remove_folder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:background="@drawable/button_press"
        android:contentDescription="@string/showDescImg"
        android:src="@drawable/ic_delete" />

</RelativeLayout>

The first time that i press the button, custom component was added, but all other times NOT. The first custom element is not overwrite, it's the only visible!

Upvotes: 1

Views: 4392

Answers (2)

Marcos Vasconcelos
Marcos Vasconcelos

Reputation: 18276

Where's your layout_cartelle_immagini declared?

Probably it's a horizontal LinearLayout, just set orientation to vertical and your code should work (considering the code to add a custom view is inside a onClickListener).

Upvotes: 1

Ritesh Gune
Ritesh Gune

Reputation: 16729

Try this. It will help you.

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
root.addContentView(custom, params);

Upvotes: 1

Related Questions