I'm trying to build a very simple widget that would show me an activity on click. However, once deployed, it shows me only "Problem Loading Widget". I've tried everything I found on the net: I checked the @Override's, I changed the widget button layout to Relative from Constraint, but nothing worked, it's still just "Problem Loading Widget". Logcat doesn't show anything. Please help.
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:id="@+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00FFFFFF"
app:srcCompat="@drawable/qr_code_banner"
tools:ignore="SpeakableTextPresentCheck" />
</RelativeLayout>
MainActivity.java:
package com.mine.qrcode_widget;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.widget_layout);
}
}
QR_code_widget.java:
package com.mine.qrcode_widget;
import android.annotation.SuppressLint;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.app.PendingIntent;
import android.widget.RemoteViews;
public class QR_code_widget extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
for (int appWidget : appWidgetIds) {
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
@SuppressLint("RemoteViewLayout") RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
views.setOnClickPendingIntent(R.id.imageButton, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, views);
}
}
}
extender_appwidget_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:previewLayout="@layout/widget_layout"
android:minHeight="110dp"
android:minWidth="250dp"
android:resizeMode="none"
android:updatePeriodMillis="3600000"
android:widgetCategory="home_screen"
android:previewImage="@drawable/qr_code_banner">
</appwidget-provider>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mine.qrcode_widget">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@drawable/qr_code_logo"
android:supportsRtl="true"
android:theme="@style/Theme.Covid19qrcodewidget">
<activity
android:name=".MainActivity"
android:exported="true"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:exported="true" android:name=".QR_code_widget" android:label="Covid-19 QR-Code Card">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/extender_appwidget_info"/>
</receiver>
</application>
</manifest>