4

I have implemented a custom view like this:

public class HeaderView extends LinearLayout {

    @Bind(R.id.bill_summary_layout)
    LinearLayout summaryLayout;
    @Bind(R.id.bill_balance)
    TextView balance;
    @Bind(R.id.bill_closedate)
    TextView closedDate;
    @Bind(R.id.bill_status)
    TextView status;
    @Bind(R.id.bill_partial_overdue)
    ViewStub partialOverdueView;
    @Bind(R.id.bill_partial_closed)
    ViewStub partialClosedView;
    @Bind(R.id.bill_partial_open)
    ViewStub partialOpenView;

    Bill bill;

    public HeaderView(Context context) {
        super(context);
        initUI(context);
    }

    public HeaderView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initUI(context);
    }

    public HeaderView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initUI(context);
    }

    private void initUI(Context context) {
        LayoutInflater.from(context).inflate(R.layout.header_view, this);
    }

    public Bill getBill() {
        return bill;
    }

    public void setBill(Bill bill) {
        this.bill = bill;
        notifyMustRebind();
    }

    public void notifyMustRebind() {
        setupUI();
    }

    private void setupUI() {
        if(this.bill == null)
            throw new RuntimeException("O objeto bill tem que estar setado e nao ser nulo. Talvez esqueceu de chamar o setBill?");

        switch (bill.getState()) {
            case OVERDUE:
                setOverdueState();
                break;
            case OPEN:
                setOpenState();
                break;
            case CLOSED:
                setClosedState();
                break;
            case FUTURE:
                setFutureState();
                break;
        }
    }

    private void setOverdueState(){
        summaryLayout.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.overdue));
        LayoutInflater.from(getContext()).inflate(R.layout.partial_overdue_view, this, true);
        partialOverdueView.inflate();
    }

    private void setOpenState(){
        summaryLayout.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.open));
        partialOpenView.inflate();
    }

    private void setClosedState(){
        summaryLayout.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.closed));
        partialClosedView.inflate();
    }

    private void setFutureState(){
        summaryLayout.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.future));
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        ButterKnife.bind(this);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        ButterKnife.unbind(this);
    }
}

The header_view.xml:

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

    <LinearLayout
        android:id="@+id/bill_summary_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_content_arrow"
        android:backgroundTint="@color/colorAccent"
        android:orientation="vertical"
        android:paddingBottom="@dimen/header_margin"
        android:paddingTop="@dimen/header_margin"
        >

        <TextView
            android:id="@+id/bill_balance"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:paddingBottom="@dimen/header_text_margin"
            android:text="R$ 2.300,10"
            android:textColor="@android:color/white"
            android:textSize="22sp"
            android:textStyle="bold"
            />

        <TextView
            android:id="@+id/bill_closedate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:paddingBottom="@dimen/header_text_margin"
            android:text="Vencimento 15 MAI"
            android:textColor="@android:color/white"
            android:textSize="18sp"
            />

        <TextView
            android:id="@+id/bill_status"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:paddingBottom="@dimen/header_text_margin"
            android:text="Fechamento em 5 de Julho"
            android:textAllCaps="true"
            android:textColor="@android:color/white"
            android:textSize="12sp"
            />
    </LinearLayout>

    <ViewStub
        android:id="@+id/bill_partial_overdue"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout="@layout/partial_overdue_view"
        />

    <ViewStub
        android:id="@+id/bill_partial_closed"
        layout="@layout/partial_closed_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <ViewStub
        android:id="@+id/bill_partial_open"
        layout="@layout/partial_open_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/colorAccent"/>

</LinearLayout>

The view above is inside another view called bill_view:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <br.com.leonardo2204.nubanktest.ui.custom.HeaderView
        android:id="@+id/header_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/list_header_padding">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:paddingLeft="@dimen/list_header_padding"
            android:paddingStart="@dimen/list_header_padding"
            android:text="De 5 Mar até 5 abr"
            android:textAllCaps="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:paddingEnd="@dimen/list_header_padding"
            android:paddingRight="@dimen/list_header_padding"
            android:text="Valores em R$"
            android:textAllCaps="true" />

    </RelativeLayout>

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

The view above is inflated inside a PagerAdapter, as this:

@Override
    public Object instantiateItem(ViewGroup container, int position) {
        View v = LayoutInflater.from(container.getContext()).inflate(R.layout.bill_view, container, false);
        HeaderView header = (HeaderView) v.findViewById(R.id.header_view);
        header.setBill(billList.get(position));
        container.addView(v);
        return v;
    }

The problem is, I'm getting the stack trace below :

   Process: br.com.leonardo2204.nubanktest, PID: 10815
                                                                            java.lang.IllegalArgumentException: ViewStub must have a valid layoutResource
                                                                                at android.view.ViewStub.inflate(ViewStub.java:289)
                                                                                at br.com.leonardo2204.nubanktest.ui.custom.HeaderView.setClosedState(HeaderView.java:112)
                                                                                at br.com.leonardo2204.nubanktest.ui.custom.HeaderView.setupUI(HeaderView.java:91)
                                                                                at br.com.leonardo2204.nubanktest.ui.custom.HeaderView.notifyMustRebind(HeaderView.java:76)
                                                                                at br.com.leonardo2204.nubanktest.ui.custom.HeaderView.setBill(HeaderView.java:72)
                                                                                at br.com.leonardo2204.nubanktest.ui.adapter.ViewPagerAdapter.instantiateItem(ViewPagerAdapter.java:30)
                                                                                at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:870)
                                                                                at android.support.v4.view.ViewPager.populate(ViewPager.java:1086)
                                                                                at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
                                                                                at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1474)
                                                                                at android.view.View.measure(View.java:17547)

What I am doing wrong ? Is there a better approach to inflate the header view other than inside instantiateView ?

Thanks !

5
  • you forget to post header_view.xml
    – Selvin
    Feb 25, 2016 at 16:09
  • Sorry, I'm editing. Thanks !
    – Leonardo
    Feb 25, 2016 at 16:11
  • 6
    I think the problem is layout instead android:layout in your ViewStub(bill_partial_closed and partial_open_view)
    – Selvin
    Feb 25, 2016 at 16:15
  • Wow, what a stupid mistake, that worked ! Please, add this comment as an answer so I can tick it ! And thank you very much !
    – Leonardo
    Feb 25, 2016 at 16:28
  • Ohh, another thing, setting the LinearLayout background color is not working, and backgroundTint is 21+, how can I change the bg color ?
    – Leonardo
    Feb 25, 2016 at 16:29

1 Answer 1

3

USE

android:layout="@layout/xyz"

instead of

layout="@layout/xyz" 

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.