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
?
Use:
ImageView view = new ImageView();
view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
First parameter of LayoutParams
is for width, second for height.
Here, I have created a new ImageView for explanation. You should use your own ImageView variable from your code, to do the above. I hope that's clear.
[Update to code]: Sorry, the MATCH_PARENT
variable is under LayoutParams
not under View
. I've updated the code.
[Update]: Above answer does not work for RemoteViews
as RemoteViews
cannot have Layout Params. Based on the answer given in this SO Question, there is no way to set or find dimensions for RemoteViews by normal methods.
I found this Android Widget Tutorial, where under the section Widget Size, the author says:
A Widget will take a certain amount of cells on the homescreen. A cell is
usually used to display the icon of one application. As a calculation rule
you should define the size of the widget with the formula:
((Number of columns / rows)* 74) - 2.
These are device independent pixels and the -2 is used to avoid rounding issues.
As of Android 3.1 a Widgets can be flexible in size, e.g. the user can make it
larger or smaller. To enable this for Widgets you can use the
android:resizeMode="horizontal|vertical" attribute in the XML configuration file
for the widget.
From this, I can suggest you that instead of setting a specific integer size value directly, why not set the number of rows and columns to resize? For example, if your initial widget size was 4 Columns and 1 Row
, then on user input, you can change it to 4 Columns and 4 Rows
, thus making it occupy the whole screen (Max size for widgets is 4 Rows and 4 Columns)
I know the above method is not what you wanted, but this seems like the only way to me. Try and see if it's helpful.
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
.