Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8307125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:27:56+00:00 2026-06-08T18:27:56+00:00

I’m having one empty Linear Layout in xml. And I’m adding some text view

  • 0

I’m having one empty Linear Layout in xml. And I’m adding some text view to it dynamically.

once these textviews exceeds 5 then i’m creating one more linear layout inside it and adding text views to newly created Layout. And finally adding this new layout to main layout.

I’m able to add, i.e in emulator it occupy that space but, will not display the info in the textviews.

my xml is as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:id="@+id/dyn_layout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="10dip" >
        </LinearLayout>
    </ScrollView>

    <Button
        android:id="@+id/dyn_button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add TextViews" />

</LinearLayout>

and my java file is as follows:

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class AddTextViewsDynamically extends Activity implements
        OnClickListener {

    Button button;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_textview_dynamically);

        button = (Button) findViewById(R.id.dyn_button1);
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.dyn_button1:
            LinearLayout layout = (LinearLayout) findViewById(R.id.dyn_layout);
            LinearLayout layout2 = null;
            LinearLayout layout3 = null;
            for (int i = 0; i < 10; i++) {
                if (i > 4) {
                    if (i == 5) {
                        layout2 = new LinearLayout(this);
                        layout2.setLayoutParams(new LayoutParams(
                                LayoutParams.FILL_PARENT,
                                LayoutParams.WRAP_CONTENT));
                        layout2.setOrientation(LinearLayout.VERTICAL);
                        layout2.setPadding(10, 60, 10, 10);
                        layout3 = new LinearLayout(this);
                        layout3.setLayoutParams(new LayoutParams(
                                LayoutParams.FILL_PARENT,
                                LayoutParams.WRAP_CONTENT));
                        layout3.setOrientation(LinearLayout.HORIZONTAL);
                        layout3.setPadding(10, 10, 10, 10);
                    }
                    System.out.println("**** Adding text view " + i);
                    TextView text = new TextView(this);
                    text.setText("The Value of i is :" + i);
                    text.setTextSize(12);
                    text.setGravity(Gravity.LEFT);
                    text.setLayoutParams(new LayoutParams(155,
                            LayoutParams.WRAP_CONTENT));
                    layout3.addView(text);

                    if (i == 9) {
                        System.out
                                .println("Added second linear layout to first");
                        layout2.setVisibility(View.VISIBLE);
                        layout2.addView(layout3);
                        layout.addView(layout2);
                    }
                } else {
                    System.out.println("###### Adding text view " + i);
                    TextView text = new TextView(this);
                    text.setText("The Value of i is :" + i);
                    text.setTextSize(12);
                    text.setGravity(Gravity.LEFT);
                    text.setLayoutParams(new LayoutParams(155,
                            LayoutParams.WRAP_CONTENT));
                    layout.addView(text);
                }
            }

        }

    }

}

Where am I going wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T18:27:57+00:00Added an answer on June 8, 2026 at 6:27 pm

    Your primary LinearLayout is set to Horizontal so the first 5 text view and the layout2 are shown on the same line. Adding Layout3 to Layout2 makes the Layout3 to be shown from the right of the last text view from primary Linear Layout. On a 10 inch tablet i see only the first 2 elements of your LinearLayout. Perhaps on a smaller screen you don’t see them. Try using

    text.setLayoutParams(new LayoutParams(50, LayoutParams.WRAP_CONTENT));
    

    instead of

    text.setLayoutParams(new LayoutParams(155, LaoutParams.WRAP_CONTENT));
    

    and you should see all your text views.

    EDIT :
    In your case this should do the trick;
    xml :

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <ScrollView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
    
            <LinearLayout
                android:id="@+id/dyn_layout"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:padding="10dip" >
            </LinearLayout>
    
            <LinearLayout
                android:id="@+id/dyn_layout2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:visibility="gone"
                android:padding="10dip" >
            </LinearLayout>
        </ScrollView>
    
        <Button
            android:id="@+id/dyn_button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add TextViews" />
    
    </LinearLayout>
    

    and code :

    @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.dyn_button1:
                LinearLayout layout = (LinearLayout) findViewById(R.id.dyn_layout);
                LinearLayout layout2 = (LinearLayout) findViewById(R.id.dyn_layout2);
                LinearLayout layout3 = null;
                for (int i = 0; i < 10; i++) {
                    if (i > 4) {
                        if (i == 5) {
                            layout2.setPadding(10, 60, 10, 10);
                            layout3 = new LinearLayout(this);
                            layout3.setLayoutParams(new LayoutParams(
                                    LayoutParams.FILL_PARENT,
                                    LayoutParams.WRAP_CONTENT));
                            layout3.setOrientation(LinearLayout.HORIZONTAL);
                            layout3.setPadding(10, 10, 10, 10);
                        }
                        System.out.println("**** Adding text view " + i);
                        TextView text = new TextView(this);
                        text.setText("The Value of i is :" + i);
                        text.setTextSize(12);
                        text.setGravity(Gravity.LEFT);
                        text.setLayoutParams(new LayoutParams(155,
                                LayoutParams.WRAP_CONTENT));
                        layout3.addView(text);
    
                        if (i == 9) {
                            System.out
                                    .println("Added second linear layout to first");
                            layout2.setVisibility(View.VISIBLE);
                            layout2.addView(layout3);
                        }
                    } else {
                        System.out.println("###### Adding text view " + i);
                        TextView text = new TextView(this);
                        text.setText("The Value of i is :" + i);
                        text.setTextSize(12);
                        text.setGravity(Gravity.LEFT);
                        text.setLayoutParams(new LayoutParams(155,
                                LayoutParams.WRAP_CONTENT));
                        layout.addView(text);
                    }
                }
    
            }
    
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
In my XML file chapters tag has more chapter tag.i need to display chapters
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.