22

I have read probably all posts and documentation but I still can't solve this issue.

I want to use addView() method to add view to the existing (running) layout but for some reason I cant. I know that this should be easy and basic but still I cant do it. So, please help me.

Here is a code:

LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout);
TextView text       = new TextView(this);
text.setText("test");
layout.addView(text);

That's a code, and the result is that I have displayed only views which are defined in XML file. There is no this new view I have added. When I debug I see this added view as a child of the parent to which I have added it but it isn't displayed.

here is main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
                android:id="@+id/mainLayout"
              android:layout_width="fill_parent" 
              android:layout_height="fill_parent"
              android:orientation="vertical"
              android:background="@drawable/main1" >
    <TextView android:id="@+id/app_title"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:textColor="#FFF"
              android:text="@string/app_title"
              android:textSize="25dp" 
              android:gravity="center_horizontal"/>
    <TextView android:layout_width="fill_parent" 
              android:layout_height="wrap_content"
              android:layout_marginTop="5dp"
              android:text="@string/main_screen_counter_title"
              android:textSize="15dp" 
              android:textColor="#FFF"
              android:gravity="center_horizontal"/>
   <TextView android:id="@+id/frontScreenCounter"
              android:layout_width="fill_parent" 
              android:layout_height="wrap_content"
              android:textColor="#FFF"
              android:text="@string/reading"
              android:textSize="33dp"
              android:gravity="center_horizontal" />   
    <GridView android:id="@+id/gridview"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:columnWidth="90dp"
    android:numColumns="3"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
    android:textColor="#888"
/>
</LinearLayout>

Please help. This will driving me nuts!

Bruno Bieri
  • 9,724
  • 11
  • 63
  • 92
Majstor
  • 859
  • 1
  • 12
  • 22
  • Could you add the layout where you want to add the `TextView`? – user Jul 09 '12 at 15:35
  • Hey Luksprog, It was because android:layout_height="fill_parent" in LinearLayout attributes. I have now displayed text but at a bottom of the screen. How to put it at a top? You have helped me. Indirectly, but still. Tnx a lot. – Majstor Jul 09 '12 at 15:45
  • 2
    If you want to place that `TextView` at a certain position you should use another flavor of the `addView` method that takes an int, the position where to put the new `View`, something like this: `addView(text, 0);` – user Jul 09 '12 at 15:49
  • Done! That's it! Tnx a lot one more time. Great speed. Take care. – Majstor Jul 09 '12 at 15:52

4 Answers4

22

You forgot to specify the LayoutParameters for the newly added view.

LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout);
    TextView text=new TextView(this);
    text.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    text.setText("test");
    layout.addView(text);

EDIT

The GridView with an id of @+id/gridview is defined with a layout height of fill_parent, leaving you with no space to add a new view. Changing its height to wrap_content may solve your problem.

Adding my comment to this post to help others easily verify the solution.

Arun George
  • 18,352
  • 4
  • 28
  • 28
  • Which params should I assign? I tried adding margin, width, height but still nothing.. – Majstor Jul 09 '12 at 15:28
  • Can you post your mainlayout.xml? Because Arun's code works for me too – firefistace Jul 09 '12 at 15:36
  • I tried your code and still nothing. Its there as a child of that parent but isn't displayed. Have more ideas? Tnx for your effort. – Majstor Jul 09 '12 at 15:37
  • With the code you provided this is the only solution I can come up with. Maybe if you post more code, the error can be identified. – Arun George Jul 09 '12 at 15:40
  • 1
    The `GridView` with an id of `@+id/gridview` is defined with a layout height of `fill_parent`, leaving you with no space to add a new view. Changing its height to `wrap_content` may solve your problem. – Arun George Jul 09 '12 at 15:44
  • Tnx Anun. I've changed LinearLayout Atribut of height to "wrapp content" and now I have displayed the text at a bottom. Tnx a lot for your time. You really fascinate me with this live chat. Amazing! – Majstor Jul 09 '12 at 15:49
  • Ye, really necessary advice! Thks! – PavelGP Jul 27 '16 at 10:47
1

I can't remember, but maybe there is any "refresh" method to load again the layout.

Try layout.invalidate();

Fustigador
  • 6,339
  • 12
  • 59
  • 115
1

I had the same problem and wasted several time finding the reason.

My fault was that I was adding a CustomView that had match_parent set as height.

I hope to save some precious time to anybody who did this silly annoying mistake :)

TextView tv = new TextView(this);
lytMarks.addView(tv);


CustomView cv = new CustomView(this, someObject);
lytMarks.addView(cv); // <-- This one had height = match_parent!!


EditText et = new EditText(this);
lytMarks.addView(et);
voghDev
  • 5,641
  • 2
  • 37
  • 41
0

Your linearLayout content is over the parent's View. so you need to use ScrollView. like this:

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<LinearLayout
    android:id="@+id/mainLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/main1"
    android:orientation="vertical">

    <TextView
        android:id="@+id/app_title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="@string/app_title"
        android:textColor="#FFF"
        android:textSize="25dp" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:gravity="center_horizontal"
        android:text="@string/main_screen_counter_title"
        android:textColor="#FFF"
        android:textSize="15dp" />

    <TextView
        android:id="@+id/frontScreenCounter"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="@string/reading"
        android:textColor="#FFF"
        android:textSize="33dp" />

    <GridView
        android:id="@+id/gridview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:columnWidth="90dp"
        android:gravity="center"
        android:horizontalSpacing="10dp"
        android:numColumns="3"
        android:stretchMode="columnWidth"
        android:textColor="#888"
        android:verticalSpacing="10dp" />
</LinearLayout>
</ScrollView>
peter zhang
  • 156
  • 4