Search code examples
androidandroid-widgetandroid-4.2-jelly-bean

widget not shown on android 4.XX jellybean


Hi im developing a program whit a widget on android versions before 4 widget is shown and work ing properly but in 4.xx versions its not even listed in widget neighter lockscreen or home screen .

here is my code:

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public class widget extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[]     appWidgetIds) {
    final int N = appWidgetIds.length;

    // Perform this loop procedure for each App Widget that belongs to this provider
    for (int i=0; i<N; i++) {
        int appWidgetId = appWidgetIds[i];

        // Create an Intent to launch ExampleActivity
        Intent intent = new Intent(context, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

        // Get the layout for the App Widget and attach an on-click listener
        // to the button
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
        views.setOnClickPendingIntent(R.id.wdbtn, pendingIntent);

        // Tell the AppWidgetManager to perform an update on the current app widget
        appWidgetManager.updateAppWidget(appWidgetId, views);
        //test

        int widgetId = 0;
        Bundle myOptions = appWidgetManager.getAppWidgetOptions (widgetId);

        // Get the value of OPTION_APPWIDGET_HOST_CATEGORY
        int category = myOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY, -1);

        // If the value is WIDGET_CATEGORY_KEYGUARD, it's a lockscreen widget
        boolean isKeyguard = category == AppWidgetProviderInfo.WIDGET_CATEGORY_KEYGUARD;
        int baseLayout = isKeyguard ? R.layout.keyguardwidget : R.layout.widget;

    }
}

and here is my widget 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" >

<ImageButton
    android:id="@+id/wdbtn"
    android:layout_width="61dp"
    android:layout_height="66dp"
    android:background="@null"
    android:contentDescription="@null"
    android:maxHeight="48dp"
    android:maxWidth="48dp"
    android:minHeight="48dp"
    android:minWidth="48dp"
    android:padding="0dp"
    android:scaleType="fitXY"
    android:src="@drawable/ic_launcher" />

and the widget xml info:

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 

android:initialLayout="@layout/widget"
android:updatePeriodMillis="3000"
android:previewImage="@drawable/ic_launcher"
android:resizeMode="horizontal|vertical"
android:widgetCategory="home_screen|keyguard"
android:initialKeyguardLayout="@layout/keyguardwidget">

and the manifest :

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

as i said widget working on older devices perfectly but in new devices its not shown .sorry for long question i want to show everything . and of course i have another layout for keyguard but i think its not necessry to put it here .


Solution

  • i fixed it in manifest u should add .widget and in provider.xml :

    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:minWidth="60dp"
    android:minHeight="30dp"
    android:initialLayout="@layout/widget"
    android:updatePeriodMillis="300000"
    android:previewImage="@drawable/ic_launcher"
    android:resizeMode="horizontal|vertical"
    android:widgetCategory="keyguard|home_screen"
    android:configure=""
    android:initialKeyguardLayout="@layout/keyguardwidget">
    

    now new error whern i add a widget to screen it says app isnt installed on AVD i didnt tested on real device yet.