android widget configuration activity

negazirnegazir ✭✭USMember ✭✭
edited September 2017 in Xamarin.Android

Goood day. i have a widget , and i need to build a confuuguration activity with a simple spinner and a confirm button.

I've added it in appwidgetprovider, and the activity is being called when i add the widget to a screen. But it is not working - i don't see a configLayout .

[Activity(Label = "TestConfig", Name = "com.test.ConfigureActivityBig")]
    [IntentFilter(new string[] { "android.appwidget.action.APPWIDGET_CONFIGURE" })]
    class ConfigureActivity: Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetResult(Result.Canceled);
            SetContentView(Resource.Layout.WidgetConfig);

            var spinner = (Spinner) FindViewById(Resource.Id.widgetSpinner);
            var okButton = FindViewById(Resource.Id.widgetConfigButton);

            var accountList = new List<string> {"1", "2"};
            ArrayAdapter adapter = new ArrayAdapter(this, Resource.Layout.WidgetConfig, accountList);
            spinner.Adapter = adapter;

            var widgetManager = AppWidgetManager.GetInstance(this);
            var configView = new RemoteViews(PackageName, Resource.Layout.MainWidgetLayout);

            var widgetId =Intent.GetIntExtra(AppWidgetManager.ExtraAppwidgetId, AppWidgetManager.InvalidAppwidgetId);
            if (widgetId == AppWidgetManager.InvalidAppwidgetId)
            {
                Finish();
                return;
            }

            okButton.Click += delegate
            {
                var selectedItem = spinner.SelectedItemPosition == 0 ? "1" : "2";

                configView.SetTextViewText(Resource.Id.widgetNumberLabel, selectedItem);
                widgetManager.UpdateAppWidget(widgetId,configView);
                Intent resultValue = new Intent();

                resultValue.PutExtra(AppWidgetManager.ExtraAppwidgetId, widgetId);
                SetResult(Result.Ok,resultValue);
                Finish();
            };
        }        
    }

And here is the config Layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">   
    <Spinner
        android:id="@+id/widgetSpinner"
        android:layout_width="248dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="10dp" />
    <Button
        android:id="@+id/widgetConfigButton"
        android:layout_width="wrap_content"
        android:layout_gravity="center"
        android:layout_height="wrap_content"
        android:text="Ok"
        android:layout_margin="10dp" />
</LinearLayout>
Tagged:

Posts

  • xamajonnyxamajonny ✭✭ Member ✭✭

    did you solve it in meanwhile?

Sign In or Register to comment.