Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to dynamically add EditText to LinearLayout
    primarykey
    data
    text
    <p>I have read many post similar to this, but none of them resolved my problem. Another funny thing is, on the emulator the place for the edittext is held (meaning other widgets are shifted down), but is not shown, on the other hand the same implementation on the real phone doesn't even show a place for the widget. I have the following <code>LinearLayout</code> inside which I need to dynamically add an <code>EditText</code> widget when the <code>TextView</code> <strong>Add a Team Member</strong> is clicked.</p> <pre><code>&lt;LinearLayout android:id="@+id/layout_add_task" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/separator" android:layout_margin="5dp" android:background="@color/yellow_background" android:orientation="vertical" android:padding="5dp"&gt; &lt;LinearLayout android:id="@+id/layout_task_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" &gt; &lt;ImageView android:id="@+id/iv_check_research_tasks" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/icon_check_empty" /&gt; &lt;EditText android:id="@+id/et_task_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:selectAllOnFocus="true" android:text="Task Name" android:textSize="20dp" android:textStyle="bold" /&gt; &lt;/LinearLayout&gt; &lt;TextView android:id="@+id/tv_add_team_member" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:layout_marginTop="5dp" android:text="Add a team member" android:textColor="@color/text_green" android:textSize="15dp" android:textStyle="bold" /&gt; &lt;LinearLayout android:id="@+id/ll_add_task" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/tv_add_team_member" android:orientation="horizontal" android:weightSum="100" &gt; &lt;ImageView android:id="@+id/iv_create_assignment_attach" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_weight="10" android:src="@drawable/icon_flag_alt" /&gt; &lt;TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="5dp" android:layout_weight="15" android:text="Due" android:textSize="15dp" /&gt; &lt;EditText android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="65" android:hint="None" android:selectAllOnFocus="true" /&gt; &lt;ImageView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_weight="10" android:src="@drawable/icon_calendar_empty" /&gt; &lt;/LinearLayout&gt; &lt;LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="horizontal" &gt; &lt;Button android:id="@+id/btn_create_assignment_trash" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="30dp" android:layout_marginTop="5dp" android:text="Trash" /&gt; &lt;Button android:id="@+id/btn_create_assignment_done" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="30dp" android:layout_marginTop="5dp" android:text="Done" /&gt; &lt;/LinearLayout&gt; &lt;/LinearLayout&gt; </code></pre> <p>Here is the Java code, the <code>onClickListener</code> implementation of the TextView.</p> <pre><code>tvAddTeamMember.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { LinearLayout ll = (LinearLayout) getActivity().findViewById(R.id.layout_task_name); EditText et = new EditText(getActivity()); LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); et.setLayoutParams(p); et.setText("Text"); ll.addView(et); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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