Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid dynamically add custom component to LinearLayout
    text
    copied!<p>i want add a custom component on a LinearLayout whenever i touch a button.</p> <p>This is my code:</p> <pre><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(); } </code></pre> <p>In this way, only first custom component is added, why?</p> <p>Thanks</p> <p><strong>edit:</strong> I've modified my code:</p> <pre><code>LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); ((ViewGroup) root).addView(custom, myCount, params); </code></pre> <p>This is my custom component:</p> <pre><code>&lt;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" &gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>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!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload