Danzeeeee
Danzeeeee

Reputation: 2630

AppWidget layout not showing, "widget failed to load"

Good day guys, I wonder why when i drag the widget out to my home screen, it only display "widget failed to load" instead of my widget layout. I need help.

Widget.java

package com.fyp.atms;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
import android.widget.Toast;

public class Widget extends AppWidgetProvider{

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        // TODO Auto-generated method stub
        super.onUpdate(context, appWidgetManager, appWidgetIds);

    }

    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        // TODO Auto-generated method stub
        super.onDeleted(context, appWidgetIds);
        Toast.makeText(context, "Widget deleted!", Toast.LENGTH_SHORT).show();
    }

}

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" >

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="114dp"
        android:layout_height="40dp" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="100" >
        <TableLayout
            android:layout_width="130dp"
            android:layout_height="80dp">

        <EditText
            android:id="@+id/wid_ToTranslate"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:ems="10" >

            <requestFocus />
        </EditText>

        <Spinner
            android:id="@+id/spinner2"
            android:layout_width="114dp"
            android:layout_height="40dp" />

        </TableLayout>
        <Button
            android:id="@+id/wid_trans"
            android:layout_width="100dp"
            android:layout_height="40dp"
            android:layout_weight="50"
            android:text="Translate" />
    </LinearLayout>

    <TextView
        android:id="@+id/wid_Translated"
        android:layout_width="130dp"
        android:layout_height="35dp"
        android:ems="10" />

</LinearLayout>

mywidget.xml (AppWidgetProvider)

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="274dp"
    android:minHeight="200dp"
    android:initialLayout="@layout/widget"
    android:updatePeriodMillis="1800000">


</appwidget-provider>

Upvotes: 1

Views: 1127

Answers (2)

reVerse
reVerse

Reputation: 35264

You can't use a Spinner or an EditText in a Widget. That's because every Widget layout must be based on RemoteViews. Following every layout that is supported:

Layout classes

FrameLayout
LinearLayout
RelativeLayout
GridLayout

Widget classes

AnalogClock
Button
Chronometer
ImageButton
ImageView
ProgressBar
TextView
ViewFlipper
ListView
GridView
StackView
AdapterViewFlipper

Read more about it in the Docs.

Upvotes: 3

Rolf ツ
Rolf ツ

Reputation: 8781

The problem is your layout contains an EditText which is not supported by widgets on Android.

For a complete list of views you may use see: http://developer.android.com/guide/topics/appwidgets/

Upvotes: 0

Related Questions