0

I have a widget.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
style="@style/WidgetBackground">
<ImageView
    android:id="@+id/myimage"
    android:layout_width="20dip"
    android:layout_height="20dip"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/myimage" />
<TextView
    android:id="@+id/mytext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"       
    style="@style/WidgetText.Title" />

How can I change the size of the image?

[Service]
public class UpdateService : Service
{
    public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
    {
        var updateViews = new RemoteViews(context.PackageName, Resource.Layout.widget);
        // changing textsize works perfectly:
        updateViews.SetTextViewTextSize(Resource.Id.mytext, (int)ComplexUnitType.Sp, 14);
        updateViews.SetViewVisibility(Resource.Id.myimage, ViewStates.Gone);
        updateViews.SetTextViewText(Resource.Id.text, "new text");

        // but changing imagesize does not work, I didn't find a right way to set the size, need something like this:
        updateViews.SetInt(Resource.Id.myimage, "layout_width ???", 200dip ???);

            ComponentName thisWidget = new ComponentName(this, Java.Lang.Class.FromType(typeof(Widget)).Name);
            AppWidgetManager manager = AppWidgetManager.GetInstance(this);
            manager.UpdateAppWidget(thisWidget, updateViews);


    }

updateViews has several setInt, setFloat, Set.. but I didn't find the right method. Any hint please?

Frank23
  • 3
  • 3

2 Answers2

0

You can try this method:

var videoContainer = FindViewById<RelativeLayout>(Resource.Id.local_video_container);
var parameters = videoContainer.LayoutParameters;
parameters.Height = (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, containerHeight, Resources.DisplayMetrics);
parameters.Width = (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, containerWidth, Resources.DisplayMetrics);
videoContainer.LayoutParameters = parameters;
slav4ik51493
  • 21
  • 1
  • 11
  • Thanks, but FindViewById seems to be unavailable in widget-context. https://stackoverflow.com/questions/4666687/how-to-find-view-id-in-appwidgetprovider – Frank23 Jun 08 '18 at 05:59
0

It's impossible to change the height or width of the ImageView in code, but there is some workaround.

You could try to create a new widget layout that the size of your ImageView is what you want. When you call RemoteViews() use it instead of the original one. And when you update the widget, it will be redrawn. For example:

    public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
    {
        //var updateViews = new RemoteViews(this.PackageName, Resource.Layout.widget);
        //Using another layout instead.
        var updateViews = new RemoteViews(this.PackageName, Resource.Layout.layout1);

        ComponentName thisWidget = new ComponentName(this, Java.Lang.Class.FromType(typeof(MyWidget)).Name);
        AppWidgetManager manager = AppWidgetManager.GetInstance(this);
        manager.UpdateAppWidget(thisWidget, updateViews);
        ///...
        ///...

    }

Or you could also try to add manyImageView of different size in your layout and set the Visibility to Gone. Show the one you want to display in the code by calling SetViewVisibility().

Billy Liu - MSFT
  • 2,168
  • 1
  • 8
  • 15