Reputation: 11097
I have extended a RelativeLayout with all its constructor.And I have created a layout in xml that I am inflating in one of constructor of my class.
My Code for MyClass
public class MyClass extends RelativeLayout
{
LayoutInflater inflater = null;
EditText edit_text;
Button btn_clear;
public MyClass(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public MyClass(Context context, AttributeSet attrs)
{
super(context, attrs);
// TODO Auto-generated constructor stub
inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.my_class_layout, this, true);
edit_text = (EditText) findViewById(R.id.clearable_edit);
btn_clear = (Button) findViewById(R.id.clearable_button_clear);
btn_clear.setVisibility(RelativeLayout.INVISIBLE);
}
public MyClass(Context context)
{
super(context);
// TODO Auto-generated constructor stub
}
}
My class layout which I inflate
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/clearable_edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/clearable_button_clear"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignParentRight="true"
android:background="@drawable/image_clear"
android:layout_centerVertical="true"
android:layout_marginRight="5dip"/>
</RelativeLayout>
And I use MyClass in my activity's layout like this
<com.and.MyClass
android:id="@+id/my_class"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
This works fine. but when I inflate my class layout in constructor MyClass(Context context) then it doesn't work.
I am not getting what is the problem. I hope you got my question.
Upvotes: 0
Views: 534
Reputation: 37729
This is OK.
When we use a View
in xml
public MyClass(Context context, AttributeSet attrs)
this constructor will be called.
Because all the attributes from XML are passed to this constructor using AttributeSet
, so that these values will have effect on View
's layout and other fields.
However if you want to use your View
in Java also, you should also inflate in all View
's constructor (Which is recommended approach).
Upvotes: 2
Reputation: 7472
Please change your code like this
public class MyClass extends RelativeLayout
{ LayoutInflater inflater = null; EditText edit_text; Button btn_clear;
public MyClass(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
init();
}
public MyClass(Context context, AttributeSet attrs)
{
super(context, attrs);
// TODO Auto-generated constructor stub
init();
}
public MyClass(Context context)
{
super(context);
// TODO Auto-generated constructor stub
init();
}
public void init() {
inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.my_class_layout, this, true);
edit_text = (EditText) findViewById(R.id.clearable_edit);
btn_clear = (Button) findViewById(R.id.clearable_button_clear);
btn_clear.setVisibility(RelativeLayout.INVISIBLE);
}
}
Upvotes: 2