Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set ImageView width and height programmatically on a widget

I am using below code to set an image on a widget imageview using remoteview now i want to set height and width to imageview runtime.

if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(WidgetActivity1_1.this);
    RemoteViews remoteViews = new RemoteViews(WidgetActivity1_1.this.getPackageName(), R.layout.widget_layout1_1);
    Intent configIntent = new Intent(WidgetActivity1_1.this,UpdateWidgetService.class);
    configIntent.putExtra("Appid", appWidgetId);
    PendingIntent configPendingIntent = PendingIntent.getService(WidgetActivity1_1.this, appWidgetId, configIntent, 0);
    remoteViews.setImageViewResource(R.id.imgviewGrow, R.drawable.flower1);
}
like image 627
PankajAndroid Avatar asked Mar 02 '13 06:03

PankajAndroid


1 Answers

You can set the Layout Params progrematiclly this way:

myImageView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));

or set a number instead.

Image size manipulation sometimes maybe tricky and it's recommended to first get image layout params, change it and set it back:

android.view.ViewGroup.LayoutParams layoutParams = myImageView.getLayoutParams();
layoutParams.width = 30;
layoutParams.height = 30;
myImageView.setLayoutParams(layoutParams);

and if you still have a problem to change it's size try to put it inside of a LinearLayout and manipulate the imageView size using the layout params:

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(30, 30);
myImageView.setLayoutParams(layoutParams);

Update: The following posts should help you with what you need:

RemoteViews setLayoutParams?

http://pastebin.com/As3L8xWu

like image 95
Emil Adz Avatar answered Sep 17 '22 16:09

Emil Adz