jason
jason

Reputation: 7164

When I programmatically generate textviews, static ones don't appear

I have this layout :

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ScrollView01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
        android:orientation="vertical"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_goster" tools:context="com.example.GosterActivity">


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/text_line1"
    android:layout_centerHorizontal="true"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text_line2"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/text_line1"/>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/rl_root"
        android:layout_below="@+id/text_line2">

    </LinearLayout>

</LinearLayout>
</ScrollView>

In Java, I set texts of this layout like this :

tvline1.setText("asdf1");
tvline1.setText("asdf2");

And I programmatically generate TextViews in rl_root like this :

LinearLayout relativeLayout2 = (LinearLayout) findViewById(R.id.rl_root);

for(int i = 0; i < 5; i++)
{
  TextView textvw = new TextView(this);
  textvw.setText(Integer.toString(i));
  relativeLayout2.addView(textvw);
  for(int j = 0; j < 10; j++)
  {
    TextView textvw2 = new TextView(this);
    textvw2 .setText(Integer.toString(j+5));
    relativeLayout2.addView(textvw2);
  }
}

With this configuration, I can only see the content of generated textviews, not the first two ones that I wrote by hand. Can you tell me how I can show those two TextViews as well? Thanks.

Upvotes: 0

Views: 64

Answers (1)

bond007
bond007

Reputation: 2494

I am able to get the both kind of textviews - generated ones and handwritten. Code seems to be fine. I just removed these lines

tools:showIn="@layout/activity_goster" 
tools:context="com.example.GosterActivity"

enter image description here

Upvotes: 1

Related Questions