Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Widget becomes invisible after reinstall

Tags:

android

widget

I have a widget with configure activity. When the user wants to place the widget on the home screen, the configure activity opens, the user selects the content, background color and textcolor of the widget and there is it. When I reinstall the app, the widget becomes invisible. It is still there, but with no text and color. See img: enter image description here

I am going to post all my files, and at the end, I will tell you what may be the problem. What I do not know is the solution.

widgetconfigure.xml

This is the layout of the configuration acitivty. I am not going to post this xml. It has textviews in layouts.

widget_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="72dip"
    android:layout_height="72dip"
    android:layout_gravity="center"
    android:id="@+id/widgetlayout">

    <ImageView android:id="@+id/ImageView01"
     android:layout_width="72dip" 
     android:layout_height="72dip"
     android:scaleType="fitXY">
    </ImageView>

    <TextView
        android:id="@+id/tvConfigInput"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:gravity="left"
        android:textColor="#FFFFFF"
        android:layout_marginLeft="3dip"
        android:layout_marginRight="3dip"   
        android:textSize="12dip" />

</RelativeLayout>

Part of the manifest where I register my configuration activity and the appwidgetprovider:

   <activity
            android:name=".WidgetConfig"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
            </intent-filter>
        </activity>

        <receiver android:name=".MyWidgetProvider" android:label="@string/app_name">

            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE"></action>
            </intent-filter>
            <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_info"></meta-data>
        </receiver>

xml/widget_info.xml

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:initialLayout="@layout/widget_layout"
    android:minHeight="72dp"
    android:minWidth="72dp"
    android:configure="com.b2creative.notepad.WidgetConfig"> 
</appwidget-provider>

WidgetConfig.java

This is the configuration activity. The user selects the background and textcolor of the widget and the text from a listview to appear on the widget. I am going to post only the relevant part:

     AppWidgetManager awm;
        int awID;
        Intent i = getIntent();
        Bundle extras = i.getExtras();
        if (extras != null)
        {
            awID = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
        }
        else
        {
            finish();
        }
        awm = AppWidgetManager.getInstance(c);


//the code from here is in a button onclicklistener that sets the widget.

            RemoteViews views = new RemoteViews(c.getPackageName(), R.layout.widget_layout);
            views.setTextViewText(R.id.tvConfigInput, widgettext);
            views.setTextColor(R.id.tvConfigInput, loadedtextcolor);
            views.setFloat(R.id.tvConfigInput, "setTextSize", int_widgetfontsize);

            Paint p = new Paint(); 
            p.setAntiAlias(true);
            p.setStyle(Style.FILL);
            p.setColor(loadedbgcolor);
            Bitmap bitmap = Bitmap.createBitmap(GetDipsFromPixel(72), GetDipsFromPixel(72), Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            RectF rect = new RectF(0, 0, GetDipsFromPixel(72), GetDipsFromPixel(72));
            canvas.drawRoundRect(rect, 10, 10, p);
            views.setImageViewBitmap(R.id.ImageView01, bitmap);
            Intent in = new Intent(c, Notepad.class);
            PendingIntent pi = PendingIntent.getActivity(c, 0, in, 0);

            Intent result = new Intent();
            result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, awID);
            setResult(RESULT_OK, result);

            awm.updateAppWidget(awID, views);

            finish();

MyWidgetProvider.java

public class MyWidgetProvider extends AppWidgetProvider {

        @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {

        super.onUpdate(context, appWidgetManager, appWidgetIds);
        final int N = appWidgetIds.length;
        for (int i=0; i<N; i++)
        {
            int awID = appWidgetIds[i];
            RemoteViews v = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
            //v.setTextViewText(R.id.tvConfigInput, "Something");
            appWidgetManager.updateAppWidget(awID, v);
        }
    }

    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        super.onDeleted(context, appWidgetIds);
    }

}

So I am pretty sure the problem is here in the onUpdate method, as I am not setting the layout of the widget. I left a line with // to show if I add it, I get a widget with no background but with the text "Something".

How am I supposed to set the background color, the textcolor of the widget and the text of the TextView on the widget in this method? I don't know them, the user sets them when adding the widget. Or what am I supposed to do?

EDIT

I have found this code and modified mine like this:

I added saveTitlePref(this, AppWidgetManager.INVALID_APPWIDGET_ID, widgettext); to the onClick method in the WidgetConfig.java, so when the user presses OK in the configuration acitivty, the the widgettext would be saved with the appwidgetid (Idk what appwidgetid, since the code uses AppWidgetManager.INVALID_APPWIDGET_ID).

For this to work I needed this:

  static void saveTitlePref(Context context, int appWidgetId, String text) {
            SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit();
            prefs.putString(PREF_PREFIX_KEY + appWidgetId, text);
            prefs.commit();
        }

In the MyWidgetProvider class I modified the for loop like this:

final int N = appWidgetIds.length;
    for (int i=0; i<N; i++)
    {
        int awID = appWidgetIds[i];
        RemoteViews v = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
        //v.setTextViewText(R.id.tvConfigInput, "Something");
        v.setTextViewText(R.id.tvConfigInput, loadTitlePref(context, AppWidgetManager.INVALID_APPWIDGET_ID));
        appWidgetManager.updateAppWidget(awID, v);
    }

where I added

  static String loadTitlePref(Context context, int appWidgetId) {
            SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
            String prefix = prefs.getString(PREF_PREFIX_KEY + appWidgetId, null);
            String nothing = "empty";
            if (prefix != null) {
                return prefix;
            } else {
                return nothing;
            }
        }

It still doesn't work, but getting closer. I place two widgets with different texts on the home screen, then I reinstall the app. Now both widget have texts (of course, since I set it), but they have the same text, the one I selected for the 2nd widget.

Please help me.

like image 448
erdomester Avatar asked Jun 13 '12 17:06

erdomester


1 Answers

If the image is stored to SD card, this post might have the answer: http://www.technipages.com/android-icons-disappear-from-home-screen.html

like image 144
Niko Avatar answered Oct 15 '22 08:10

Niko