There is a template of this type:

<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="true"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:id="@+id/ololo"> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="0.01" android:padding="10dip" android:text="Текст" android:textSize="18dp" android:textColor="@android:color/black"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:padding="5dp" android:background="@android:color/white"> <EditText android:layout_width="0dip" android:layout_weight="1.0" android:layout_height="wrap_content" android:id="@+id/editText"/> <Button android:layout_width="0dip" android:layout_weight="0.3" android:layout_height="wrap_content" android:text="Send"/> </LinearLayout> </LinearLayout> 

I'm trying to add another textview, but do it programmatically. I do it like this:

 runOnUiThread(new Runnable() { @Override public void run() { LinearLayout layout = (LinearLayout) findViewById(R.id.ololo); TextView txt = new TextView(getApplicationContext()); txt.setText("123123123"); LinearLayout newLayout = new LinearLayout(getApplicationContext()); newLayout.addView(txt,0); } 

The code is compiled, but is not added when textview is emulated. What am I doing wrong?

  • one
    you add txt to newLayout , which you don’t add anywhere. - Vladyslav Matviienko

1 answer 1

Perhaps your mistake in this method

 runOnUiThread(new Runnable() { @Override public void run() { LinearLayout layout = (LinearLayout) findViewById(R.id.ololo); TextView txt = new TextView(getApplicationContext()); txt.setText("123123123"); LinearLayout newLayout = new LinearLayout(getApplicationContext()); newLayout.addView(txt,0); } 

First you inflate LinearLayout with the markup:

 LinearLayout layout = (LinearLayout) findViewById(R.id.ololo); 

Then create a TexView

 TextView txt = new TextView(getApplicationContext()); 

And then you create an incomprehensible LinearLayout

 LinearLayout newLayout = new LinearLayout(getApplicationContext()); 

and put a TexView in it

 newLayout.addView(txt,0); 

In this case, if after that you nowhere add newLayout to your markup - it turns out that you simply add your TextView - nowhere.

Try replacing the code with this one, it can help:

 @Override public void run() { LinearLayout layout = (LinearLayout) findViewById(R.id.ololo); TextView txt = new TextView(getApplicationContext()); txt.setText("123123123"); layout.addView(txt,0); } 

UPD:

 public class TestActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_activity); LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ololo); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); params.weight = 0.01f; TextView textView = new TextView(this); textView.setText("Custom text"); linearLayout.addView(textView,params); } } 

Here in this form, using your markup, add a new `TextView ', how to make it appear so that you normally need to think, I would change the layout.

enter image description here

  • Did not help unfortunately - Nikola Krivosheya
  • I added the code, so add, but with the layout you need to think Shoto! - Kirill Stoianov