Android set image size programmatically - android

How can I set the image size on a widget programmatically? I am using this code to change the image but how can I set the size?
RemoteViews updateViews = new RemoteViews(EditPreferences.this.getPackageName(), R.layout.widgetmain2);
updateViews.setImageViewBitmap(R.id.ImageView00, ((BitmapDrawable)EditPreferences.this.getResources().getDrawable(R.drawable.normal_black_op100)).getBitmap());
ComponentName thisWidget = new ComponentName(EditPreferences.this, HelloWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(EditPreferences.this);
manager.updateAppWidget(thisWidget, updateViews);
Thanks in advance

It's a very wide question in fact. There are multiple answers for it:
Usually you do it in layout.xml setting appropriate layout parameters (width, height) and scaleType: http://developer.android.com/reference/android/widget/ImageView.html#attr_android:scaleType. This handles a lot of cases - whenever the image is set its scaling will be recalculated according to it's parameters.
But sometimes you ned to do it programmatically. But it is more complex because it needs to take into account density of the screen and density of the bitmap, screen size independent pixel size and so on. There are two points here:
Getting bitmap in the right size. It depends whether you load bitmap from resources (it seems you do) or whether you load it manually. If you want to fully control the size of the bitmap, then you have to understand how it all works - I recommend reading http://developer.android.com/guide/practices/screens_support.html and especially the chapter Scaling Bitmap Objects created at Runtime.
Setting bitmap's dimensions on screen. If you want to do it on runtime and you already calculated required size in physical pixels. Remember there are also independent pixels and you need to convert physical pixels to another (look at "density independent pixels" definition and algorithms for calculation in http://developer.android.com/guide/practices/screens_support.html . Once you know the physical pixels, you can apply LayoutParameters to the ImageView. It will be something like:
Example:
LinearLayout.LayoutParams layoutParams = new
LinearLayout.LayoutParams(width_physical_pixels, height_physical_pixels);
imageView.setLayoutParams(layoutParams);
IMPORTANT! LinearLayout here is just example. It should be the PARENT class of the ImageView - so layout params are always of the type of the object container rather than of the object they are set on.

Related

Fix the Screen Size Issue Programmatically in Android

I was creating Android application. that will dynamically create questions. but based on the mobile screen size view is not coming properly. I don't know how to fix this problem. I have enclosed image that is taken form different mobiles.
Screen size -3.7" , Screen Size -5.0"
All the Element sizes are varied based on the screen size.can any one help to display responsive screen in Android.
My Code is:
lView.setBackgroundColor(Color.parseColor("#FFFFFF"));
lView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 220);
GradientDrawable gdtitle = new GradientDrawable();
gdtitle.setCornerRadius(5);
ImageView title = new ImageView(Main2Activity.this);
title.setImageResource(R.drawable.logo);
title.setLayoutParams(layoutParams2);
title.setBackgroundDrawable(gdtitle);
lView.setOrientation(LinearLayout.VERTICAL);
lView.addView(title);
GradientDrawable gd3 = new GradientDrawable();
gd3.setCornerRadius(30);
gd3.setColor(Color.parseColor("#003366"));
gd3.setStroke(0, 0xFF000000);
TextView uname1 = new TextView(Main2Activity.this);
uname1.setText("Hello , "+Sessionname);
uname1.setGravity(Gravity.LEFT);
uname1.setTextColor(Color.parseColor("#003366"));
uname1.setTextSize(20);
uname1.setLayoutParams(l2);
et1 = new TextView(Main2Activity.this);
et1.setHeight(100);
et1.setTextColor(Color.WHITE);
et1.setHintTextColor(Color.WHITE);
et1.setGravity(Gravity.CENTER);
et1.setHint("Select Date");
et1.setBackgroundDrawable(gd3);
et1.setTextSize(15);
et1.setLayoutParams(l2);
lView.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams l4 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
l4.gravity=Gravity.CENTER;
lHorizontalView1.setOrientation(LinearLayout.HORIZONTAL);
lHorizontalView1.setLayoutParams(l4);
lHorizontalView1.addView(uname1);
lHorizontalView1.addView(et1);
lView.addView(lHorizontalView1);
You're hard-coding your values in pixels. For example for your height you call setHeight(100) that means set this View to be 100px tall. However, with different screen densities that's going to look completely different. Given a fix screen size (say 5 inches), one device could have a resolution of 480x800 and another one could be 1440x2560, your setting of 100px will look exactly how you have it now.
Use Display Metrics instead to retrieve the screen density and use device independent pixels (dip or dp) instead of pixels (px) as units. For example you can set your height based on the density of the device like this:
setHeight(100 * getResources().getDisplayMetrics().density);
However, your desired height may have to be smaller as that will become 100dp so you may have to set it to something like 50 instead. Play around until you get your desired results.
then your View will look uniform across devices.

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 ?
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.
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.

Spinning wheel half way off screen android

I have a requirement for a spinning wheel graphic that is half-off the bottom of the screen and then animates (spins) when users take action.
I can place the wheel graphic off the screen by calling:
Animation animation = new TranslateAnimation(Animation.ABSOLUTE,0,Animation.ABSOLUTE, 0,Animation.ABSOLUTE,wheelPos, Animation.ABSOLUTE, wheelPos);
animation.setDuration(1);
animation.setFillAfter(true);
wheel.startAnimation(animation);
That is fine, I can set the width to whatever I want via LayoutParams.
But how can I make the wheel the same size for different screen sizes? For example, I need to place the wheel Y pixels off the screen (wheelPos variable) above, but on a Nexus S versus Nexus 7 how can I calculate an appropriate Y value?
The same goes for width, how can I calculate a width so that it appears exactly the same size?
I note the Catch android app https://catch.com/ - if I expand their wheel menu, it's size is identical on the Nexus S versus Nexus 7 - how might they have achieved that?
Developer of Catch here. Thanks for the email pointing us to your question here.
For our wheel menu, its width and positioning are specified within XML layout files, using dp/dip (density-independent pixels). For a given dimension, say, 300dp, it will appear the same size on any Android device; the system will scale that value depending on whether the device's screen is low, medium, high, xhigh, etc. density.
So, set your width and Y offset using dp and you should be good to go. You can do it completely programmatically if you wish:
final int wheelWidth = 300;
final int wheelYoffset = 64;
DisplayMetrics dm = getResources().getDisplayMetrics();
final float scaledWheelWidth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, wheelWidth, dm);
final float scaledWheelYoffset = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, wheelYoffset, dm);
I personally find it much easier to maintain all of my UI dimensions as resources. In res/values/dimens.xml I'd add:
<dimen name="wheel_width">300dp</dimen>
<dimen name="wheel_y_offset">64dp</dimen>
and then if I needed to use those dimensions in code, I can grab them with a one-liner:
final float scaledWheelWidth = getResources().getDimensionPixelSize(R.dimen.wheel_width);
final float scaledWheelYoffset = getResources().getDimensionPixelOffset(R.dimen.wheel_y_offset);
Using dp for layout is one of the keystone principles of making an Android UI that scales nicely to different devices, and is especially critical for anything you expect your user to touch/interact with, since you can size your hit targets during development and rest assured that they will be physically similarly-sized no matter what device your app is running on.

what does android getIntrinsicHeight and getIntrinsicWidth mean?

Hi I am confused by the two methods from Android Drawable class
getIntrinsicHeight()
getIntrinsicWidth()
api definition says
http://developer.android.com/reference/android/graphics/drawable/Drawable.html#getIntrinsicHeight()
what does the word intrinsic height/width mean?
i mean is it a width of the actual image?
If you want to know the meaning of intrinsic, it is nothing but the actual property possessed by an object. In our case getIntrinsicWidth/Height simply means to provide you with the default width/height of that drawable.
This returns the exact size of the drawable which you have put in the resource folder without any modification.
Now you have to know that getWidth or getHeight will return a value which might vary according to the width and height you specify for your ImageView in your XML layout.
Let's say that you have provided the width and height of your ImageView as 100*100 in the XML layout and the drawable you used as the background is of size 200*200.
Now getIntrinsicWidth must return 200 whereas getWidth must return 100.
related question here on stackoverflow.
IF your image is downloaded from the internet, .getIntrinsicWidth() and .getIntrinsicHeight() indeed give you the "real" width and height, respectively of the image.
It's called intrinsic, because it depends ONLY on the image and on nothing else (such as your phone).
Alas, what you get is NOT intrinsic in all circumstances - it DOES depend things other than the image, unfortunately.
Here is where you get a wrong (namely, non-intrinsic) result. Let's say you are using the default launcher icon, then
Log.i("", "ic_launcher intrinsic width " + getResources().getDrawable(R.drawable.ic_launcher).getIntrinsicWidth());
will tell you the width (in pixels) of the launcher icon. But of which one? - you have several of them, one in drawable-xhdpi folder, one in drawable-hdpi folder, etc. Well, if your device is, say, xhdpi, it gives you 96, which is indeed the pixel-width of the version of the launcher icon residing in the drawable-xhdpi folder. Now, delete the icon in the drawable-xhdpi folder, and run again (still using an xhdpi device (real or emulated)). The image that will be used will be from drawable-hdpi folder, because that's "closest" to the xhdpi version. That icon has a pixel width of 72. But above code WILL STILL GIVE YOU 96!!!
That is clearly NOT "intrinsic" (in the proper sense of the word), as it does not depend only on the image used.
So if you are as lazy as I am, and are therefore not generating 4 versions of each resource icon/image (but instead using only 1 or 2, and scaling them by hand), you have to beware the mentioned androidal misnomer.
In android a drawable can be of many types such as color, bitmap, shape etc.
Some of these drawables have an intrinsic height such as a BitmapDrawable which is the dimension of the image.
Drawables such as ColorDrawable (used to draw just solid colors) don't have an intrinsic height. In this case the value of getIntrinsicHeight/Width returns -1.
Even if a drawable doesn't have intrinsic height/width, every drawable needs to have their bounds set before they can render itself (i.e before you call mydrawable.draw(canvas))
If you using a drawable as a background for a view, the view internally sets the bounds for you. But if you are using drawables in your own onDraw, then you need to explicitly set the bounds via setBounds.

set COMPLEX_UNIT_PX on Layout Parameters

I am developing for two different devices and need to force pxs on one device's layoutparameter object through code when specifying width and height. How do I do that?
GridView.LayoutParams glp = new GridView.LayoutParams(GridView.LayoutParams.WRAP_CONTENT, GridView.LayoutParams.WRAP_CONTENT);
glp.width = 80; <- need to ensure this is in px or LDPI interprets it .75 of 80
glp.height = 80;
Normally I would use setFontSize(TypedValue.COMPLEX_UNIT_PX, size) and it works. How about Layouts themselves?
You need the params to be fixed? If you want to make sure adding pixels, create dimen resources e.g: 80px in string.xml then load in LayoutParams
new GridView.LayoutParams(pixels, pixels); They are already pixels.
See the docs
http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html#ViewGroup.LayoutParams(int, int)

Categories

Resources