2

I want to do a custom view coming from an XML.

Here's my XML :

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    <TextView 
        android:id="@+id/movieTitle"
        android:text="@string/movietitle"
        android:textSize="35dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        />
    <com.galite.headliner.views.LoaderImageView
        android:id="@+id/moviePoster" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/movieTitle"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:contentDescription="@string/poster"
        />
    <TextView
        android:id="@+id/movieDescription"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/description"
        android:layout_below="@id/moviePoster"
        android:gravity="center"
        />
</RelativeLayout>

And i want to inflate to a custom view because i need to use methods like onClick and everything.

Here's my constructor :

public MyView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    this.context = context;
    initializeView();
}

public void initializeView(){
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    v = inflater.inflate(R.layout.movie, null);
}

How can i make MyView equal to v ?

1 Answer 1

2

You'll have to attach the inflated view v to MyView like this:

v = inflater.inflate(R.layout.movie, this, true);

If you want that exact layout then make MyView to extend RelativeLayout and modify the xml layout like this:

<merge xmlns:android="http://schemas.android.com/apk/res/android" >
    <TextView 
        android:id="@+id/movieTitle"
        android:text="@string/movietitle"
        android:textSize="35dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        />
    <com.galite.headliner.views.LoaderImageView
        android:id="@+id/moviePoster" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/movieTitle"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:contentDescription="@string/poster"
        />
    <TextView
        android:id="@+id/movieDescription"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/description"
        android:layout_below="@id/moviePoster"
        android:gravity="center"
        />
</merge>
Sign up to request clarification or add additional context in comments.

1 Comment

The method inflate(int, ViewGroup, boolean) in the type LayoutInflater is not applicable for the arguments (int, MovieView, boolean)

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.