2

Here are my custom view:

public class BuzzView extends View {

    /**
     * Constructor.  This version is only needed if you will be instantiating
     * the object manually (not from a layout XML file).
     * @param context
     */
    public BuzzView(Context context) {
        super(context);
        View.inflate(context, R.layout.buzz_view, null);
    }
}

My buzz_view.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/vignette_image_jauge"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:src="@drawable/buzzomettre_sample" >
    </ImageView>

</RelativeLayout>

Than i want to add my custom view from my FrameLayout in my activity:

 BuzzView buzzView = new BuzzView(MosaiqueListActivity.this);
    FrameLayout frameLayout = (FrameLayout) 
v.findViewById(R.id.vignette_layout_jauge);
                frameLayout.addView(buzzView);

I am not getting my custom view in my RelativeLayout. Do you know why?

6
  • try adding layout params Commented Jan 31, 2013 at 10:57
  • doesn't work :( buzzView.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); Commented Jan 31, 2013 at 10:59
  • is it possible to cast a ImageView to FrameLayout?? Commented Jan 31, 2013 at 11:01
  • You need to Implement all three constructor from superclass. Also don't extends directly from View, user RelativeLayout as superclass instead Commented Jan 31, 2013 at 11:04
  • there is a classcastexception in the code you posted Commented Jan 31, 2013 at 11:05

1 Answer 1

1

Try creating your BuzzView like this

public class CustomView {

private Context mContext;
private View mCustomView;
private LayoutInflater mInflater;
public CustomView(Context context) {
    // TODO Auto-generated constructor stub
    mContext = context;
    mInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public View getView() {
    if(mCustomView==null) {
        mCustomView = mInflater.inflate(R.layout.customView, null);
        //initialize all the child here
    }
    return mCustomView;
}
}

while adding your view just call getView() method like this in your activity

CustomView mCustomView = new CustomView(MyActivity.this);
layout.addView(mCustomView.getView(), new LayoutParams(LayoutParams.WRAP_CONTENT,    LayoutParams.WRAP_CONTENT));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.