Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to inflate XML-Layout-File correctly inside Custom ViewGroup?
    text
    copied!<p>I want to inflate a XML-Layout-File in a custom ViewGroup Class, my Problem is that it produces just a empty screen. Doing the same in the Activity Class works fine. Here is my simple XML-Layout-File: </p> <pre><code>shownumberlayout.xml: &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#FFFFFF" android:id="@+id/layoutForNumber"&gt; &lt;TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tvNumber" android:layout_centerHorizontal="true" android:textColor="#000000" android:text="Test" android:layout_centerVertical="true" android:textSize="30dip"&gt; &lt;/TextView&gt; &lt;/RelativeLayout&gt; </code></pre> <p>Here is the working Version, inflating the <code>shownumberlayout.xml</code> in the Activity <code>ShowNumber</code>:</p> <pre><code>ShowNumber.class public class ShowNumber extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); ViewGroup vg = (ViewGroup) inflater.inflate(R.layout.shownumberlayout, null); setContentView(vg); } } </code></pre> <p>This shows a White Background with the black Text “Test” centered.</p> <p>Now the Version inflating the xml in the Custom <code>ViewGroup</code>-Class:</p> <pre><code>ViewGroup.class public class ViewNumber extends ViewGroup { private LayoutInflater inflater; public ViewNumber(Context context) { super(context); // TODO Auto-generated constructor stub initView(context); } public ViewNumber(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub initView(context); } public ViewNumber(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub initView(context); } private void initView(Context context){ inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); inflater.inflate(R.layout.shownumberlayout, null); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { // TODO Auto-generated method stub } } </code></pre> <p>ShowNumber.class public class ShowNumber extends Activity { /** Called when the activity is first created. */</p> <pre><code>@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ViewGroup vg = new ViewNumber(this); setContentView(vg); } </code></pre> <p>}</p> <p>Im doing it basically like in <a href="https://stackoverflow.com/questions/3368182/anyway-to-use-xml-layout-files-for-specific-view-viewgroups">this</a> Answer explained. This just produces a Empty Black Screen. What I am doing wrong?</p> <p><strong>UPDATE 1</strong></p> <p><strong>@Konstantin</strong> I applied your changes but still just a blank screen, i also made a log-ouput for getting the number of children. It remains always 1, even i add one more Textview to the XML-Layout-File. Before the Changes it remains always 0.</p> <pre><code>public class ViewNumber extends RelativeLayout { ... private void initView(Context context){ //inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); //inflater.inflate(R.layout.shownumberlayout, null); View.inflate(context, R.layout.shownumberlayout,this); Log.v("ViewNumber", "Number of Child: " + this.getChildCount());//output is 1,before it remains 0 } ... } </code></pre> <p><strong>@Sankar</strong> This is the Logcat, after the Changes from Konstantin:</p> <pre><code>12-16 09:24:23.606: DEBUG/AndroidRuntime(8951): &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; AndroidRuntime START &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; 12-16 09:24:23.606: DEBUG/AndroidRuntime(8951): CheckJNI is OFF 12-16 09:24:23.606: DEBUG/dalvikvm(8951): creating instr width table 12-16 09:24:23.656: DEBUG/AndroidRuntime(8951): --- registering native functions --- 12-16 09:24:23.916: DEBUG/AndroidRuntime(8951): Shutting down VM 12-16 09:24:23.916: DEBUG/dalvikvm(8951): Debugger has detached; object registry had 1 entries 12-16 09:24:23.916: INFO/AndroidRuntime(8951): NOTE: attach of thread 'Binder Thread #3' failed 12-16 09:24:24.076: DEBUG/AndroidRuntime(8960): &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; AndroidRuntime START &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; 12-16 09:24:24.076: DEBUG/AndroidRuntime(8960): CheckJNI is OFF 12-16 09:24:24.076: DEBUG/dalvikvm(8960): creating instr width table 12-16 09:24:24.126: DEBUG/AndroidRuntime(8960): --- registering native functions --- 12-16 09:24:24.376: INFO/ActivityManager(78): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=org.customview.harold/.ShowNumber } 12-16 09:24:24.426: DEBUG/AndroidRuntime(8960): Shutting down VM 12-16 09:24:24.426: DEBUG/jdwp(8960): Got wake-up signal, bailing out of select 12-16 09:24:24.426: DEBUG/dalvikvm(8960): Debugger has detached; object registry had 1 entries 12-16 09:24:24.456: INFO/AndroidRuntime(8960): NOTE: attach of thread 'Binder Thread #3' failed 12-16 09:24:24.456: VERBOSE/ViewNumber(8923): Number of Child: 1 12-16 09:24:24.496: VERBOSE/RenderScript_jni(164): surfaceDestroyed 12-16 09:24:24.526: INFO/ActivityManager(78): Displayed activity org.customview.harold/.ShowNumber: 104 ms (total 104 ms) 12-16 09:24:24.576: DEBUG/dalvikvm(158): GC_FOR_MALLOC freed 10631 objects / 526248 bytes in 52ms 12-16 09:24:34.606: DEBUG/dalvikvm(164): GC_EXPLICIT freed 1776 objects / 106960 bytes in 91ms </code></pre> <p><strong>UPDATE 2</strong></p> <p>The Content is finally showing correctly up. The missing thing was to override the Method <code>onLayout</code> (thanks to <strong>Franco</strong>) in the RelativeLayout-Sublcass:</p> <pre><code>public class ViewNumber extends RelativeLayout { ... @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { // TODO Auto-generated method stub for(int i = 0 ; i &lt; getChildCount() ; i++){ getChildAt(i).layout(l, t, r, b); } } ... } </code></pre> <p>Note: Later you should also override the Method <code>onMeasurement()</code>, but currently the content is also showing correctly without overriding it.</p> <p>Now the solution for the Method <code>initView</code> from <strong>Franco</strong> do not align the TextView in the Center, but puts it in the top left corner. The solution from <strong>Konstantin</strong> puts it correctly in the Center of the View:</p> <pre><code>public class ViewNumber extends RelativeLayout { ... private void initView(Context context){ View.inflate(context, R.layout.shownumberlayout,this); } ... } </code></pre>
 

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