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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:08:42+00:00 2026-05-31T04:08:42+00:00

I would like to add programmatically to LinearLayout some TextViews . And I want

  • 0

I would like to add programmatically to LinearLayout some TextViews. And I want to use LayoutInflater. I have in my activity layout xml file:

<LinearLayout
     android:id="@+id/linear_layout"
     android:layout_width="wrap_content"
     android:layout_height="fill_parent"
     android:orientation="vertical"
     />

I have written in activity code like this below.

LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear_layout);
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
TextView textView = (TextView) inflater.inflate(R.layout.scale, linearLayout, true);
textView.setText("Some text");
linearLayout.addView(textView);

My scale.xml file looks like:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:layout_marginLeft="50dp"
     android:layout_marginRight="50dp"  
     android:drawableTop="@drawable/unit"
     />

At the line TextView textView = (TextView) inflater.inflate(R.layout.scale, linearLayout, true); I have fatal exception like this below.

 java.lang.RuntimeException: Unable to start activity ComponentInfo{my.package/my.package.MyActivity}: 
 java.lang.ClassCastException: android.widget.LinearLayout
 Caused by: java.lang.ClassCastException: android.widget.LinearLayout

When I replace in problematic line linearLayout with null I don’t have any exception, but the android:layout_marginLeft and android:layout_marginRight from my scale.xml are ignored and I can’t see any margins around added TextView.

I have found question Android: ClassCastException when adding a header view to ExpandableListView but in my case I have exception in first line in which I use the inflater.

  • 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-05-31T04:08:43+00:00Added an answer on May 31, 2026 at 4:08 am

    When you specify the root view (linearLayout) in the call to inflater.inflate(), the inflated view is automatically added to the view hierarchy. Consequently, you don’t need to call addView. Also, as you noticed, the returned view is the root view of the hierarchy (a LinearLayout). To get a reference to the TextView itself, you can then retrieve it with:

    LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear_layout);
    LayoutInflater inflater = (LayoutInflater) getApplicationContext().
        getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    inflater.inflate(R.layout.scale, linearLayout, true);
    TextView textView = (TextView) linearLayout.getChildAt(
        linearLayout.getChildCount()-1);
    textView.setText("Some text");
    

    If you were to give the view an android:id attribute in scale.xml, you could retrieve it with

    TextView textView = (TextView) linearLayout.findViewById(R.id.text_id);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to programmatically add a custom site template (.stp file) to a
I have a horizontal LinearLayout that's programmatically filled with its children. I would like
I would like to programmatically add or remove some elements to a string array
I'm writing a simple VS add-in and would like to programmatically invoke the Document
I would like to add some icons to my treeview. Is there a way
I have a web application where I would like to add or edit Enterprise
I would like to programmatically add to Windows a name / IP association so
I would like to programmatically add the shared libraries to my deployed applications in
Essentially I would like to add a UIImage programmatically to my interface, using: [[self
We would like to add DLLs to an install based on some external command

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.