When my StackedWidget view was only a textview, it displayed on the launcher screen, but with a more intricate layout (A RelativeLayout, imageview, textviews) it does not display anything
Is there something I need to know about setting RemoteViews ?
widget_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget_item"
android:layout_width="270dp"
android:layout_height="150dp">
<ImageView
android:id="@+id/timelineCellImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:onClick="onClick"
android:scaleType="centerCrop"
android:src="@drawable/bottom_grey_gradient"
/>
<LinearLayout
android:id="@+id/timelineCellTextHolder"
android:layout_width="match_parent"
android:layout_height="@dimen/fifty_dp"
android:layout_alignBottom="@id/timelineCellImage"
android:layout_alignLeft="@id/timelineCellImage"
android:background="@drawable/rounded_edges_bottom_dark"
android:orientation="vertical" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginLeft="@dimen/five_dp"
android:layout_marginRight="@dimen/five_dp"
android:layout_marginTop="@dimen/ten_dp"
android:layout_weight=".5"
android:ellipsize="end"
android:gravity="bottom"
android:lines="1"
android:text="Test"
android:textColor="@color/white"
android:textSize="@dimen/twelve_sp"
android:textStyle="bold" />
<TextView
android:id="@+id/subheading"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginBottom="@dimen/ten_dp"
android:layout_marginLeft="@dimen/five_dp"
android:layout_marginRight="@dimen/five_dp"
android:layout_weight=".5"
android:ellipsize="end"
android:gravity="top"
android:lines="1"
android:text="Subtest"
android:textColor="@color/white"
android:textSize="@dimen/twelve_sp" />
</LinearLayout>
StackWidgetService.java $getViewAt(int)
public RemoteViews getViewAt(int position) {
// position will always range from 0 to getCount() - 1.
RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.widget_item);
if(!mWidgetItems.isEmpty()){
// We construct a remote views item based on our widget item xml file, and set the
// text based on the position.
rv.setTextViewText(R.id.title, mWidgetItems.get(position).getTitle()); //probably only top stories items, editorial stuff
rv.setTextViewText(R.id.subheading, mWidgetItems.get(position).getLinkAbstract());
rv.setImageViewUri(R.id.timelineCellImage, Uri.parse(mWidgetItems.get(position).getCoverImageUrl()));
// Next, we set a fill-intent which will be used to fill-in the pending intent template
// which is set on the collection view in StackWidgetProvider.
Bundle extras = new Bundle();
extras.putInt(StackWidgetProvider.EXTRA_ITEM, position);
Intent fillInIntent = new Intent();
fillInIntent.putExtras(extras);
rv.setOnClickFillInIntent(R.id.widget_item, fillInIntent);
// You can do heaving lifting in here, synchronously. For example, if you need to
// process an image, fetch something from the network, etc., it is ok to do it here,
// synchronously. A loading view will show up in lieu of the actual contents in the
// interim.
/*new ImageRequest(
url,
listener,
maxWidth,
maxHeight,
decodeConfig,
errorListener);
Response.Listener<Bitmap> listener = new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap bitmap) {
// use your bitmap
}
};*/
}
else
{
rv.setTextViewText(R.id.title, "my title");
rv.setTextViewText(R.id.subheading, "");
rv.setImageViewResource(R.id.timelineCellImage, R.drawable.top_grey_gradient);
Bundle extras = new Bundle();
extras.putInt(StackWidgetProvider.EXTRA_ITEM, position);
Intent fillInIntent = new Intent();
fillInIntent.putExtras(extras);
rv.setOnClickFillInIntent(R.id.widget_item, fillInIntent);
}
try {
System.out.println("Loading view " + position);
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Return the remote views object.
return rv;
}
getCoverImageUrl
does it return content URI?setImageViewUri
works with content URI and not network resource. If you are using network resource, then you need to set bitmap. Also removeonClick
andclickable
attributes of ImageView from layout file. – Hubie