0

I have class which extends LinearLayout

in constructor I call

LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout view = (LinearLayout)inflater.inflate(R.layout.titlebar, this, true);

I can access Views in this view but it is not drawn. I think the reason is that mParent of this view is still null. But why? the Partent should be this (the class which extends LinearLayout)

here the xml:

<RelativeLayout android:id="@+id/header"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="35px" 
android:layout_width="fill_parent"
android:background="@drawable/titlebar_bg">

<TextView android:layout_width="wrap_content" 
    android:textColor="#000000"
    android:text="test" 
    android:id="@+id/title" 
    android:layout_alignParentTop="true" 
    android:layout_marginLeft="10pt" 
    android:layout_height="wrap_content"
    android:textSize="10pt"
    android:layout_marginTop="3dip"></TextView>

<TextView android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:text="10:09 AM" 
    android:id="@+id/time" 
    android:textColor="#000000"
    android:layout_alignParentRight="true" 
    android:layout_marginRight="5px"
    android:layout_marginTop="3px"
    android:textSize="10pt"></TextView></RelativeLayout>

SOLUTION

<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:layout_width="wrap_content" 
    android:textColor="#000000"
    android:text="test" 
    android:id="@+id/title" 
    android:layout_alignParentTop="true" 
    android:layout_marginLeft="10pt" 
    android:layout_height="wrap_content"
    android:textSize="10pt"
    android:layout_marginTop="3dip"></TextView>

<TextView android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:text="10:09 AM" 
    android:id="@+id/time" 
    android:textColor="#000000"
    android:layout_alignParentRight="true" 
    android:layout_marginRight="5px"
    android:layout_marginTop="3px"
    android:textSize="10pt"></TextView>

using the merge tag and use the xml tags.

<package.Titlebar 
            android:id="@+id/testtitlebar" 
            android:layout_height="35px" 
            android:layout_width="fill_parent"
            android:background="@drawable/titlebar_bg"></package.Titlebar>

this is how it looks in the class:

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.titlebar, this);

no onLayout function!!! One of my faults

Mark Adler
  • 101,978
  • 13
  • 118
  • 158
passsy
  • 5,162
  • 4
  • 39
  • 65

2 Answers2

0

You should try : LinearLayout view = (LinearLayout)inflater.inflate(R.layout.titlebar, null, false);

AFAIK this will return view, which root is the root of the inflated XML.

Kocus
  • 1,613
  • 17
  • 31
  • No then i got an error: ERROR/AndroidRuntime(11097): java.lang.RuntimeException: Unable to start activity: android.view.InflateException: Binary XML file line #13: Error inflating class MyClass – passsy Apr 15 '11 at 08:28
  • Can you post the content of this XML file? – Kocus Apr 15 '11 at 08:38
0

You should try

LayoutInflater inflater = LayoutInflater.from(this);

if you are inside your activity class, or

LayoutInflater inflater = LayoutInflater.from(YourActivity.this);

if you are in an inline class inside your activity class.

Then if you want to add the new view to an other inside your activity (say my_canvas):

//[YourActivity.] is needed when you are in an inline class!
//RelativeLayout is not mandatory, you can have any other layout there
final RelativeLayout canvas = (RelativeLayout) [YourActivity.]this.findViewById(R.id.my_canvas);
final View titleBar = inflater.inflate(R.layout.titlebar, canvas, false);

Now you can add this titleBar wherever you need.

Update

This is the apidocs for the inflate method:

public View inflate (int resource, ViewGroup root, boolean attachToRoot) 

Since: API Level 1
Inflate a new view hierarchy from the specified xml resource. Throws InflateException if there is an error.

Parameters
resource ID for an XML layout resource to load (e.g., R.layout.main_page)
root Optional view to be the parent of the generated hierarchy (if attachToRoot is true), or else simply an object that provides a set of LayoutParams values for root of the returned hierarchy (if attachToRoot is false.)
attachToRoot Whether the inflated hierarchy should be attached to the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML.

Returns The root View of the inflated hierarchy. If root was supplied and attachToRoot is true, this is root; otherwise it is the root of the inflated XML file.

rekaszeru
  • 19,130
  • 7
  • 59
  • 73
  • Thanks for your response. But I don't code in an Activity. I wrote a CompountComponent like this one. http://stackoverflow.com/questions/1476371/android-writing-a-custom-compound-component – passsy Apr 15 '11 at 10:14
  • Then you should just specify a component with proper `LayoutParams` instead of the canvas variable. see my update – rekaszeru Apr 15 '11 at 10:19