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 9016397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:59:06+00:00 2026-06-16T03:59:06+00:00

I am trying to add textview as children to a linear layout but getting

  • 0

I am trying to add textview as children to a linear layout but getting exception.This is the xml file:

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

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/OkButton"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Ok" />

        <Button
            android:id="@+id/AddContactButton"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Add new Contact" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal" >

        <ListView
            android:id="@+id/listViewSetRule"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_margin="2dip"
            android:cacheColorHint="#00000000"
            android:choiceMode="multipleChoice" />
        <!-- android:smoothScrollbar="false" -->

        <ListView
            android:id="@+id/listViewUpdateRule"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_margin="2dip"
            android:cacheColorHint="#00000000"
            android:choiceMode="multipleChoice" />

        <LinearLayout
            android:id="@+id/sideIndex"
            android:layout_width="40dip"
            android:layout_height="fill_parent"
            android:background="@color/black"
            android:gravity="center_horizontal"
            android:orientation="vertical" >
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

Here i am trying to add children to linear layout having id sideIndex using this code:

@Override
    public void onWindowFocusChanged(boolean hasFocus)
    {
        super.onWindowFocusChanged(hasFocus);

        LinearLayout sideIndex = (LinearLayout)ShowTheContacts1.this.findViewById(R.id.sideIndex);
        sideIndexHeight = sideIndex.getHeight();

        ((ViewGroup)sideIndex.getParent()).removeView(sideIndex);

        TextView l_tempText = new TextView(ShowTheContacts1.this);
        l_tempText.setGravity(Gravity.CENTER);
        l_tempText.setTextSize(20);
        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1);
        l_tempText.setLayoutParams(params);

        for(int l_a = 0;l_a < m_arrayOfAlphabets.length;l_a++)
        {
            l_tempText.setText(m_arrayOfAlphabets[l_a]);
            sideIndex.addView(l_tempText);
        }
    }

I am getting exception at this line: sideIndex.addView(l_tempText);This is the logcat :

12-21 20:08:06.291: E/AndroidRuntime(19007): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
12-21 20:08:06.291: E/AndroidRuntime(19007):    at android.view.ViewGroup.addViewInner(ViewGroup.java:1861)
12-21 20:08:06.291: E/AndroidRuntime(19007):    at android.view.ViewGroup.addView(ViewGroup.java:1756)
12-21 20:08:06.291: E/AndroidRuntime(19007):    at android.view.ViewGroup.addView(ViewGroup.java:1713)
12-21 20:08:06.291: E/AndroidRuntime(19007):    at android.view.ViewGroup.addView(ViewGroup.java:1693)
12-21 20:08:06.291: E/AndroidRuntime(19007):    at com.velosys.smsManager.Activities.ShowTheContacts1.onWindowFocusChanged(ShowTheContacts1.java:1335)
12-21 20:08:06.291: E/AndroidRuntime(19007):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onWindowFocusChanged(PhoneWindow.java:1969)
12-21 20:08:06.291: E/AndroidRuntime(19007):    at android.view.View.dispatchWindowFocusChanged(View.java:3731)
12-21 20:08:06.291: E/AndroidRuntime(19007):    at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:657)
12-21 20:08:06.291: E/AndroidRuntime(19007):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1819)
12-21 20:08:06.291: E/AndroidRuntime(19007):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-21 20:08:06.291: E/AndroidRuntime(19007):    at android.os.Looper.loop(Looper.java:123)
12-21 20:08:06.291: E/AndroidRuntime(19007):    at android.app.ActivityThread.main(ActivityThread.java:4363)
12-21 20:08:06.291: E/AndroidRuntime(19007):    at java.lang.reflect.Method.invokeNative(Native Method)
12-21 20:08:06.291: E/AndroidRuntime(19007):    at java.lang.reflect.Method.invoke(Method.java:521)
12-21 20:08:06.291: E/AndroidRuntime(19007):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-21 20:08:06.291: E/AndroidRuntime(19007):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-21 20:08:06.291: E/AndroidRuntime(19007):    at dalvik.system.NativeStart.main(Native Method)

I don’t know how to solve this problem.Please help me.Thanks in advance.

  • 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-16T03:59:08+00:00Added an answer on June 16, 2026 at 3:59 am

    You need to recreate TextView l_tempText within the loop. You can’t use the same instance, change the text and add it, it has to be a whole new object.

    Move all l_tempText code into the loop and it should work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to add a textview to a linear layout progmmatically, but I keep
I am trying to setText() to a TextView resource from a layout XML file.
I am trying to add a Button in a LinearLayout after a TextView but
I am trying to add a context menu to a linear layout and open
I have LinearLayout in my xml file that I am trying to add a
I am trying to add a TextView dynamically into a layout which is part
I am trying to add ads to my layout on top of it. <?xml
I am trying to add a textview inside a tab dynamically. using this code
I'm trying to add a textView to a frameLayout . The TextView has wrap_content
Hi in my app when i was trying to add a link in textview

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.