1

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.

Community
  • 1
  • 1
woyaru
  • 5,544
  • 13
  • 54
  • 92

1 Answers1

3

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);
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • Thank you, but I don't understand. My `LinearLayout` don't have any child views in layout file. How can I use `getChildAt` method in this case? And I have exception when I try to use `inflater.inflate()` with `LinearLayout` as my `ViewGroup`. My applications works when I use `null` as `ViewGroup` but in this case I again can't use `getChildAt` method. – woyaru Mar 11 '12 at 21:26
  • 2
    @woyaru - After `inflater.inflate` returns, the inflated `TextView` will have been added to `linearLayout`. The exception is coming because you are trying to cast the return value to a `TextView` when it is actually returning `linearLayout` itself. (`inflater.inflate` returns the inflated view only if the root view is `null`. If it is not `null`, then it returns the root view, not the view that was inflated.) – Ted Hopp Mar 11 '12 at 21:31