Thursday, May 24, 2018

Adding ImageView crashes android app widget

Leave a Comment

I am making a very simple widget that shows an image using ImageView.

My layout Looks like this

<RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:padding="16dip">      <!-- ImageView logo -->     <ImageView         android:id="@+id/logo"         android:src="@drawable/giants"         android:contentDescription="@string/app_name"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_centerInParent="true"         android:scaleType="fitXY" />  </RelativeLayout> 

My Widget Provider Class Looks like this

 public class LogoWidgetProvider extends AppWidgetProvider {      @Override     public void onUpdate(Context context, AppWidgetManager      appWidgetManager, int[] appWidgetIds) {          // What Happens While App Goes Through Each Widget Id//         for (int currentWidgetId: appWidgetIds) {              // Define And Instantiate Variable RemoteViews widgetLogo//             RemoteViews widgetLogo = new RemoteViews(context.getPackageName(), R.layout.widget_logo);              // Update Widget//             appWidgetManager.updateAppWidget(currentWidgetId, widgetLogo);         }     } } 

The receiver in my manifest looks like this

    <!-- Widget Provider Logo -->     <receiver android:name=".LogoWidgetProvider">          <!-- Tell The App That There Is A Widget To Display -->         <intent-filter>             <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />         </intent-filter>          <!-- Tell The App Where To Find Widget Data -->         <meta-data             android:name="android.appwidget.provider"             android:resource="@xml/appwidget_info_logo">         </meta-data>      </receiver> 

Lastly, the widget provider XML looks like this

<!-- Basic Information For Widget Logo --> <appwidget-provider     xmlns:android="http://schemas.android.com/apk/res/android"     android:initialLayout="@layout/widget_logo"     android:minHeight="110dip"     android:minWidth="110dip"     android:minResizeWidth="40dip"     android:resizeMode="vertical|horizontal"     android:updatePeriodMillis="3600000"     android:widgetCategory="home_screen">  </appwidget-provider> 

For some reason, the widget crashes every time. If I was to switch ImageView To Button it works fine. What am I missing to make the widget display the Image?

Thanks

1 Answers

Answers 1

I figured it out. You can use any image regardless of resolution but it needs to be put inside a box of 400 pixels by 400 pixels. I used paint pad to do it. Once it is in the box the image shows up

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment