Search code examples
androidandroid-widgetandroid-imageview

How to dynamically set height and width of an ImageView in an Android widget to "match_parent"?


There is one ImageView in my homescreen widget. In the layout file, I am setting the height and width to wrap_content. But depending on user's input, I have to change its height and width to match_parent. How can this be done ?

RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

Now I tried to use the setInt() method on this RemoteViews object like this to just check if it is possible to change height and width:

rv.setInt(R.id.widgetImageView, "setMinimumHeight", 300);

But this is what I get on the logcat:

couldn't find any view, using error view
android.widget.ImageView can't use method with RemoteViews: setMinimumHeight(int)

So how do I change its height and width to match_parent ?


Solution

  • For those wondering how this can be done, I used two ImageView in the layout. One with height and width set to wrap_content and another with height and width set to match_parent. Depending on the user's input, I called the setVisibility to hide the ImageView which was not required through RemoteView.

    rv.setInt(R.id.widgetImageView1, "setVisibility", View.GONE);
    

    There are a very few ImageView methods which we can call through RemoteView.