Disable ImageView if it doesn't fit on the screen - android

I have an ImageView with a dynamic src, and I want to display it, only if there is enough place to show all of it without scaling it.
How should I handle this?

you need to get the image size and screen size then check to see if its larger before displaying
If you want the the display dimensions in pixels you can use
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
If you don't have access to an activity to call getWindowManager on. You can use:
Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
For the use case you're describing however a margin/padding in the layout seems more appropriate.
then do
if (ImageView.getDrawable().getIntrinsicHeight() > height || ImageView.getDrawable().getIntrinsicWidth() >width){
//dont display image
}
coppied and compiled from two post
Get screen dimensions in pixels
and
Trying to get the display size of an image in an ImageView

imageView.setScaleType(ScaleType.FIT_XY);

Related

Android screen size inconsistency

As seen in this image:
I have a rounded rectangle that is the size of 1082 x 1796, and I used getWindowManager().getDefaultDisplay() to get and print the pixel values of the screen as seen in the circle bottom left. However, when I draw this bitmap, this happens:
Is the screen size different from the getDefaultDisplay() method? How do I fix this?
I think your shape is seen by the system as a 9patch. It use the first and last lines/columns in order to know how to resize the image in order to display it consistently.
That's why there is a 2px difference in width and height. I think you can correct it by modifying the png and removing the first and last lines/columns, or just use another way to draw the image. Maybe using another format (jpg ? bmp ?) for the image could do the job. I'm not sure.
you can do that with calculation to fit all screen devices based on width and height of the device screen:
DisplayMetrics dm = new DisplayMetrics();
((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = width * imageHeight / imageWidth;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);

Full width image in android notification

I need to add full width image in the notification for my application.
I do it by sending the URL in the json payload, load the bitmap and on successful image loading I show the notification.
My question is what is the best optimum resolution of the Image
Please help!
Thanks in advance.
The width of your image should be equal to the width of the screen as the width of the dropdown notification bar will be the same.
In order to do that you need to do this:
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
After doing this you can set the value of int width to your image.

Layout stretching

Is there a layout, that would allow me to make it in absolute values, but when it would be on larger / smaller screen, it would stretch and adjusted those values, to fit onto the screen but preserve the same look?
Relative layout still keens on exact values.
For example, I have a button and I want it to have it the width of 1/3 of the screen of every device.
Setting manualy in code the width of an element to (for example) screenWidth/3 works. Yet I don't think it's clean. But this technique works.
Find device dimensions at runtime and set width of button at runtime.
Display mDisplay = getWindowManager().getDefaultDisplay();
int deviceWidth = mDisplay.getWidth();
int deviceHeight = mDisplay.getHeight();
button.getLayoutParams().width = deviceWidth / 3;
Give the values in dpi of your layout and view and they will adjust themselves on any screen

How do I obtain screen resolution information?

I need to obtain the screen resolution, width & height, how do I do that?
Solution:
Display d = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
width = d.getWidth();
height = d.getHeight();
Display d=Display.getInstance();
int width = d.getDisplayWidth();
int height = d.getDisplayHeight();
According to http://forums.java.net/jive/message.jspa?messageID=479230
or possibly
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
http://www.coderanch.com/t/435390/Android/Mobile/screen-size-at-run-time
PLEASE first ask yourself why you need to do this. You very probably don't. If it is to do anything based on the screen size (layout etc) this will return you the wrong information, since it is telling you about the physical resolution of the screen, not taking into account anything like the status bar, window decorations, etc. Even if you are running your app as full-screen, in some cases there may be parts of the screen that are used for system UI that you can't get rid of but will be included in the numbers returned by Display.
The correct thing to do is adjust for the size your view gets during layout, such as when its onSizeChanged() is called.

Unable to get the correct screen size using Services for Android

So i am trying to get the screen size of the Moto Droid on my application. on my Create, i am using the Service windowManager to get the default display.
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
When i set it i get width of 320 and height 570. that looks wrong, it should be 320x480.
i want to place a 300x50 image on the bottom of the screen on any device (actually) so normally i would get the height of the screen and minus the image height so it places it on a view. but since i am getting 570 for some reason, i have to scroll down to see the image. why is it getting the wrong screen size and is there another more accurate way of getting the size. Thank you
Layout code goes as follows
linearOut = new LinearLayout(this);
linear = new LinearLayout(this);
linear.setOrientation(LinearLayout.VERTICAL);
scroll = new ScrollView(this);
linearOut.addView(scroll);
scroll.addView(linear);
linear.addView(text);
linear.addView(bbottom.getViewGroup());
setContentView(linearOut);
try this::
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
I think i got it. so the info above shows full screen viewable. so that does include the status bar. Is there a way to see the height of the status bar? i am not sure if all android devices have the same pixel height for the status bar.

Categories

Resources