3

I am building a widget that has several pages of information. Only one page will be displayed at a time, and when a user taps on the widget, the next page will be displayed. I would like to create this functionality by using a ViewFlipper, however I can not find any documentation on how to implement it for a widget.

Here is skeleton code I am using while trying to get the ViewFlipper to work.

I have widget (DivisionStandings ) that has the content inserted into the layout (mainLayout) with a service (UpdateService).

When running the widget, I initially see the text for header1 set to "Updated Header" as intended. Touching the widget however does nothing.

[BroadcastReceiver (Label = "aaWdiget")]
[IntentFilter (new string [] { "android.appwidget.action.APPWIDGET_UPDATE" })]
[MetaData ("android.appwidget.provider", Resource = "@xml/divisionstandings")]
public class DivisionStandings : AppWidgetProvider
{
    public override void OnUpdate (Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
    {
    context.StartService (new Intent (context, typeof (UpdateService)));
    }
}


[Service]
public class UpdateService : Service
{
    public override void OnStart (Intent intent, int startId)
    {
        RemoteViews mainView= new RemoteViews (this.PackageName, Resource.Layout.mainLayout);;
        ComponentName thisWidget = new ComponentName (this,  "mlbstandings.DivisionStandings");
        AppWidgetManager manager = AppWidgetManager.GetInstance (this);

       mainView.SetTextViewText(Resource.Id.header1, "Updated Header");

        ViewFlipper flipper = (ViewFlipper)Resource.Id.viewFlipper1;
        flipper.Touch += (object sender, View.TouchEventArgs e) => { 
            flipper.ShowNext(); 
        };

        manager.UpdateAppWidget (thisWidget, mainView);
    }


<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainLayout" >
    <ViewFlipper
        android:id="@+id/viewFlipper1">
        <TextView
            android:id="@+id/header1"
            android:text="---Header" />
        <TextView
            android:id="@+id/header2"
            android:text="Other Header" />       
    </ViewFlipper>
</LinearLayout>
Mason240
  • 2,924
  • 3
  • 30
  • 46

1 Answers1

2

Problem probably is in ViewFlipper flipper = (ViewFlipper)Resource.Id.viewFlipper1; 'Resource.Id.viewFlipper1' is int not ViewFlipper

So you need to get you view by id like that:

ViewFlipper flipper = mainView.FindViewById<ViewFlipper> (Resource.Id.viewFlipper1)

Update since it looks like there is no method to access directly to views you need to use mainView.SetOnClickPendingIntent and than do yor action in OnRecive() method.

Here is some more information in Java

Community
  • 1
  • 1
ad1Dima
  • 3,185
  • 2
  • 26
  • 37