Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A couple of things you can check in your code:</p> <ul> <li>On the <a href="http://developer.android.com/reference/android/view/View.html" rel="noreferrer">View</a> that is being added, check that you call its <a href="http://developer.android.com/reference/android/view/View.html#setLayoutParams(android.view.ViewGroup.LayoutParams)" rel="noreferrer">setLayoutParameter</a> method with an appropriate <a href="http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html" rel="noreferrer">ViewGroup.LayoutParameter</a>.</li> <li>When you adding the new <a href="http://developer.android.com/reference/android/view/View.html" rel="noreferrer">View</a>s, make sure you are doing it on the UI thread. To do this, you can use the parent <a href="http://developer.android.com/reference/android/view/View.html" rel="noreferrer">View</a>'s <a href="http://developer.android.com/reference/android/view/View.html#post(java.lang.Runnable)" rel="noreferrer">post</a> method.</li> </ul> <p>This self contained example adds a <a href="http://developer.android.com/reference/android/widget/TextView.html" rel="noreferrer">TextView</a> after a short delay when it starts:</p> <pre><code>import java.util.Timer; import java.util.TimerTask; import android.app.Activity; import android.os.Bundle; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.TextView; public class ProgrammticView extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); final LinearLayout layout = new LinearLayout(this); layout.setLayoutParams(new ViewGroup.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT)); setContentView(layout); // This is just going to programatically add a view after a short delay. Timer timing = new Timer(); timing.schedule(new TimerTask() { @Override public void run() { final TextView child = new TextView(ProgrammticView.this); child.setText("Hello World!"); child.setLayoutParams(new ViewGroup.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); // When adding another view, make sure you do it on the UI // thread. layout.post(new Runnable() { public void run() { layout.addView(child); } }); } }, 5000); } } </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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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