1

I have been trying relentlessly to figure out to have my widget update with a press of the button that is on the widget. The widget displays the current RAM remaining, CPU level, and Battery charge.

The code to determine those values work well. I just cant get it to update when I press the button. I have tried these methods :

Force Android widget to update - both methods shown

Android: How do I force the update of all widgets of a particular kind

Clickable widgets in android

And an alarm manager

None of them have worked.

My XML for my widget layout:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:background="@drawable/widgetbg">

        <TextView android:id="@+id/widgetCPU"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/derp"   
            android:textSize="14sp"
            android:typeface="sans"
            android:textColor="@android:color/holo_blue_bright"
            android:gravity="center"/>

        <TextView android:id="@+id/widgetRam"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/derp"   
            android:textSize="14sp"
            android:typeface="sans"
            android:textColor="@android:color/holo_blue_bright"
            android:gravity="center"/>

        <TextView android:id="@+id/widgetBat"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/derp"   
            android:textSize="14sp"
            android:typeface="sans"
            android:textColor="@android:color/holo_blue_bright"
            android:gravity="center"/>
        <Button android:id="@+id/widgetUpdate"
                android:layout_weight="1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/button_send" />

    </LinearLayout>

And the method I would like to call when the button is pressed :

    public class AppWidget extends AppwidgetProvider
    {
    //...
         public void update(Context c, AppWidgetManager a)
         { 
          //...
         }
    //...
    }

How would I be able to call the update method when my button is pressed?

Community
  • 1
  • 1

2 Answers2

1

You could add an onClickListener to the button, and call the update method from within the listener.

Check out This link for more info on the interface

dannyRods
  • 552
  • 6
  • 23
1

register a listener, e.g. in your onCreate() method:

     findViewById(R.id.widgetUpdate).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
                        update();
        }
    });
koljaTM
  • 10,064
  • 2
  • 40
  • 42