[android-developers] Create a custom component using inflate

2010-03-29 Thread arnouf
Hi all,

I'm trying to create a component using an xml layout file.

I defined a layout with some custom attributes. This layout is
inflated  when user add it inside another layout. My problem is I
don't know the parent, so I pass null for the parent viewgroup when I
call inflate method.

I think that's the reason why I see anything in my view.
Do you know how can I do (I don't want to redefine onDraw method
because I use existing component)

Below my xml layout and my custome class.

RelativeLayout
xmlns:android=http://schemas.android.com/apk/res/android;
android:layout_width=wrap_content
android:layout_height=wrap_content

ImageView
android:id=@+id/img_btn_option
android:layout_width=wrap_content
android:layout_height=wrap_content/

TextView
android:id=@+id/txt_btn_option
android:layout_width=wrap_content
android:layout_height=wrap_content
android:layout_toRightOf=@id/img_btn_option
android:textColor=@color/font_white
android:textStyle=bold/

/RelativeLayout

public class ButtonOptionsView extends View {

public ButtonOptionsView(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater li =
(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout vg =
(RelativeLayout)li.inflate(R.layout.button_option, null);
TypedArray ta =
getContext().obtainStyledAttributes(attrs,R.styleable.ButtonOption);

((ImageView)vg.findViewById(R.id.img_btn_option)).setImageResource(ta.getResourceId(R.styleable.ButtonOption_image,
0));

((TextView)vg.findViewById(R.id.txt_btn_option)).setText(ta.getResourceId(R.styleable.ButtonOption_text,
0));
}

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

To unsubscribe from this group, send email to 
android-developers+unsubscribegooglegroups.com or reply to this email with the 
words REMOVE ME as the subject.


Re: [android-developers] Create a custom component using inflate

2010-03-29 Thread Mark Murphy
arnouf wrote:
 Hi all,
 
 I'm trying to create a component using an xml layout file.
 
 I defined a layout with some custom attributes. This layout is
 inflated  when user add it inside another layout. My problem is I
 don't know the parent, so I pass null for the parent viewgroup when I
 call inflate method.

You need to be the parent. You cannot inflate things into an ordinary View.

 I think that's the reason why I see anything in my view.
 Do you know how can I do (I don't want to redefine onDraw method
 because I use existing component)
 
 Below my xml layout and my custome class.
 
 RelativeLayout
 xmlns:android=http://schemas.android.com/apk/res/android;
 android:layout_width=wrap_content
 android:layout_height=wrap_content
 
 ImageView
 android:id=@+id/img_btn_option
 android:layout_width=wrap_content
 android:layout_height=wrap_content/
 
 TextView
 android:id=@+id/txt_btn_option
 android:layout_width=wrap_content
 android:layout_height=wrap_content
 android:layout_toRightOf=@id/img_btn_option
 android:textColor=@color/font_white
 android:textStyle=bold/
 
 /RelativeLayout
 
 public class ButtonOptionsView extends View {
 
   public ButtonOptionsView(Context context, AttributeSet attrs) {
   super(context, attrs);
   LayoutInflater li =
 (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   RelativeLayout vg =
 (RelativeLayout)li.inflate(R.layout.button_option, null);
   TypedArray ta =
 getContext().obtainStyledAttributes(attrs,R.styleable.ButtonOption);
   
 ((ImageView)vg.findViewById(R.id.img_btn_option)).setImageResource(ta.getResourceId(R.styleable.ButtonOption_image,
 0));
   
 ((TextView)vg.findViewById(R.id.txt_btn_option)).setText(ta.getResourceId(R.styleable.ButtonOption_text,
 0));
   }

Make ButtonOptionsView extend RelativeLayout, and replace the
RelativeLayout in your XML with merge. Then, inflate into yourself.

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com | http://twitter.com/commonsguy

_The Busy Coder's Guide to *Advanced* Android Development_
Version 1.4 Available!

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

To unsubscribe from this group, send email to 
android-developers+unsubscribegooglegroups.com or reply to this email with the 
words REMOVE ME as the subject.