Custom View not appearing
Your custom layout view is not appearing because you're not putting anything into it. In your onFinishInflate
you have the line inflatedView = li.inflate(R.layout.provider_view, null);
But you don't add that to your view. You have two options to add the views to your custom layout view.
Change your custom view to extend RelativeLayout
, change the enclosing RelativeLayout
to <merge>
in your provider_view.xml, and fix your findViewId
lines in to this.findViewId(...)
since the views will be inflated into your layout.
In your layout xml do:
<com.bookcessed.booksearch.SearchProviderButton
android:id="@+id/csp_spb_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/csp_tv_title"
rs:providerName="BOOKP">
<!-- include this so it's added to your custom layout view -->
<include layout="@layout/provider_view" />
</com.bookcessed.booksearch.SearchProviderButton>
provider_view becomes:
<?xml version="1.0" encoding="utf-8"?>
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:rs="http://schemas.android.com/apk/res/com.bookcessed.booksearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="@+id/pv_rl_strings"
.
.
.
</merge>
SearchProviderButton:
public class SearchProviderButton extends RelativeLayout{
.
.
.
@Override
protected void onFinishInflate() {
super.onFinishInflate();
//the <include> in the layout file has added these views to this
//so search this for the views
ImageView logo = this.findViewById(R.id.pv_logo);
logo.setImageResource(connector.getLogoDrawableID());
TextView searchTV = (TextView)this.findViewById(R.id.pv_tv_search);
TextView andTV = (TextView)this.findViewById(R.id.pv_tv_and);
if(!connector.isSearchSupported()){
andTV.setText("");
searchTV.setVisibility(GONE);
}
setgenreIcons();
}
.
.
.
Or you could properly inflate the view in onCreate
by layoutInflater.inflate(R.layout.provider_view, this, true)
. That call will inflate the referenced layout into the passed ViewGroup
, in this case your custom view, and add the inflated View
s to it. Then you can fix up the findViewId
calls in your onFinishInflate
.
For wrap_content to work, you need to properly implement the measure
and layout
functions for your custom View. See How Android Draws Views for details on what these methods mean.
My guess would be that getMeasureWidth()
and getMeasureHeight()
functions for your view are always returning 0.