Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamically adding views to mixed xml/code compound layout
    primarykey
    data
    text
    <p>Sorry if this redundant with the ton of questions/answers on inflate, but I could not get a solution to my problem.</p> <p>I have a compound view (LinearLayout) that has a fixed part defined in XML and additional functionalities in code. I want to dynamically add views to it.</p> <p>Here is the XML part (compound.xml):</p> <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/compoundView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" &gt; &lt;TextView android:id="@+id/myTextView" android:layout_width="110dp" android:layout_height="wrap_content" android:text="000" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>I have defined in code a LinearLayout to refer to the XML:</p> <pre><code>public class CompoundControlClass extends LinearLayout { public CompoundControlClass (Context context) { super(context); LayoutInflater li; li = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); li.inflate(R.layout.compound_xml,*ROOT*, *ATTACH*); } public void addAView(){ Button dynBut = new Button(); // buttoin def+layout info stripped for brevity addView(dynBut); } } </code></pre> <p>I tried to programmatically add a view with addAView.</p> <p>If ROOT is null and ATTACH is false, I have the following hierarchy (per HierarchyViewer):</p> <ul> <li>CompoundControlClass>dynBut </li> </ul> <p>The original TextView in the XML is gone.</p> <p>If ROOT is this and ATTACH is true, I have the following hierarchy:</p> <ul> <li>CompoundControlClass>compoundView>myTextView</li> <li>CompoundControlClass>dynBut</li> </ul> <p>I would like to have </p> <ul> <li>CompoundControlClass>myTextView</li> <li>CompoundControlClass>dynBut</li> </ul> <p>where basically the code and XML are only one unique View. What have I grossly missed?</p> <h1>ANSWER BASED on feedback from D Yao ----------------------</h1> <p>The trick is to INCLUDE the compound component in the main layout instead of referencing it directly.</p> <p>activity_main.xml</p> <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"&gt; &lt;include layout="@layout/comound" android:id="@+id/compoundView" android:layout_alignParentBottom="true" android:layout_width="fill_parent" android:layout_height="wrap_content"/&gt; &lt;/RelativeLayout&gt; </code></pre> <p>mainActivity.java</p> <pre><code>public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); CompoundControlClass c = (CompoundControlClass) this.findViewById(R.id.compoundView); c.addAView(this); } } </code></pre> <p>CompoundControlClass.java</p> <pre><code>public class CompoundControlClass extends LinearLayout { public CompoundControlClass(Context context) { super(context); } public CompoundControlClass(Context context, AttributeSet attrs) { super(context, attrs); } public CompoundControlClass(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public void addAView(Context context){ ImageView iv = new ImageView(context); iv.setImageResource(R.drawable.airhorn); addView(iv); } } </code></pre> <p>compound.xml</p> <pre><code>&lt;com.sounddisplaymodule.CompoundControlClass xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/compoundView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" &gt; &lt;TextView android:layout_width="110dp" android:layout_height="wrap_content" android:gravity="right" android:textSize="40sp" android:textStyle="bold" android:text="0:00" /&gt; &lt;/com.sounddisplaymodule.CompoundControlClass&gt; </code></pre>
    singulars
    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.
    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