Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use addView to add view to layout?

Tags:

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!

like image 848
Majstor Avatar asked Jul 09 '12 15:07

Majstor


People also ask

How do I add a view to activity?

This example demonstrates How to Dynamically Add Views into View. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do I add a view in another view?

All you need to do is inflate the view programmatically and it as a child to the FrameLayout by using addChild() method. Then during runtime your view would be inflated and placed in right position. Per Android recommendation, you should add only one childView to FrameLayout [link].

What is AddView?

The addView method, is used to add a View programmatically to a ViewGroup . A ViewGroup can be for example, a LinearLayout , or a RelativeLayout .. A ViewGroup is itself a View . The addView method is overloaded.

How to dynamically add views into view in Android?

How to Dynamically Add Views into View in Android? This example demonstrates How to Dynamically Add Views into View. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

How do I add a view to a linear layout?

If you want to add a view at a specific index (or position) inside a linear layout you can do it by just writing below the line of code. Here in the above code, your_view will be your view (TextView, ButtonView, etc), and the second parameter is index value or position.

Why can't I add a new view to a GridView?

You forgot to specify the LayoutParameters for the newly added view. 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.

How to add a view at a specific index (or position)?

If you want to add a view at a specific index (or position) inside a linear layout you can do it by just writing below the line of code. Here in the above code, your_view will be your view (TextView, ButtonView, etc), and the second parameter is index value or position. For example, I have given index value as 2, you can give your own value.


2 Answers

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.

like image 191
Arun George Avatar answered Oct 18 '22 11:10

Arun George


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

Try layout.invalidate();

like image 45
Fustigador Avatar answered Oct 18 '22 11:10

Fustigador