I read this official link because i want to set different XML for larger screen.
First, i checked my device's width (in dp) with this code :
private void getScreenDp() {
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
float dpHeight = displayMetrics.heightPixels / displayMetrics.density;
float dpWidth = displayMetrics.widthPixels / displayMetrics.density;
}
The width is 320dp.
Then i create a new folder called layout-sw320dp. When i run the app in 320dp width device, its using layout-sw320dp correctly.
However, the layout that my apps used when running in 600dp width device is the one in layout-sw320dp instead of the normal layout folder. I tried to change the layout to layout-w320dp but the devices bigger than 320dp width still not using the normal layout as it should be.
Please kindly help me, i have spent hours for this.
Related
I am testing my app on a Nexus 5 (full HD screen) and an HTC One X (720p).
Using sp as a good Android developer, actually gives me a result I do not want!
The text on the Nexus 5 is bigger than on the HTC One X, even taking into account that the screen is bigger in size.
When I use 'dp' instead of 'sp', they are actually more alike...the result I want...
But you want to use sp, as that is the way you should do it.
So I created a values-sw380dp directory and put there a different value. But, the Nexus 5 does NOT use these values! Then I used this code:
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
float dpHeight = displayMetrics.heightPixels / displayMetrics.density;
float dpWidth = displayMetrics.widthPixels / displayMetrics.density;
Surprisingly: for both phones, the dpWidth == 360! So I have no way to do this nicely for 720p and 1080p screen ?
Does anyone have an idea or solution?
I am tempted to use DP...
I'm new in designing the layouts. I am getting the bitmap from the server and I'm setting the imageview height such that it look good on every density either ldpi, mdpi, hdpi, xhdpi.
stripImageView.getLayoutParams().height = (int) ((Util.screenWidth(this) * strip.getHeight() / strip.getWidth()) * getResources().getDisplayMetrics().density);
But This code not working for me. Some device images looking very small and Some device it look fine
Try just setting the dimensions in your layout using dp instead of px. Dp (or dip) stands for density independent pixels and will automatically scale based on density.
EDIT: another issue I see is that you're referencing both the screen width and height so your screen aspect ratio would also affect that calculation.
convert them to DIP:
stripImageView.getLayoutParams(). height =
(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, <HEIGHT>,
getResources().getDisplayMetrics());
where HEIGHT is the height in px you want. You could base it off the strip height or something.
At the top of my app, I have a title which should be shown in the middle, and a button on the right. As the textViews length is behind my control, I sometimes have my title crossing the button due to long length of the content of it.
After following this, I somehow tend to solve the problem. My device was HTC desire. Unfortunately, if I check with Galaxy SIII, it doesn't do the trick.
I am wondering how I can manage this in terms of different devices with different densities.
My controls inside the relative layout
You can also check the device screen density by this--
WindowManager wm = (WindowManager) _context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int screenWidth = display.getWidth();
int screenHeight = display.getHeight();
And can manage accordingly whats your apps needed..
just use weightsum in your xml and make width of all the views as fill parent ..... this makes auto resizing of your textview
you can maintain layouts according to their DPI`s
replicate the same XML data in XHDPI (As S3 falls in XHDPI) and test it similarly replicate the XML data in HDPI
but
keep in mind the following Thing Pixel Ratio of the layOut as
following
in LDPI its 1:0.75
in MDPI its 1:1
in HDPI its 1:1.5
in XHDPI its 1:2
Display display = ((WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
DisplayMetrics dm = new DisplayMetrics();
display.getMetrics(dm);
float density = dm.density;
int screenWidth = display.getWidth();
With this code above, you'll have your screen density as float.. So you can use it to calculate your textView's width like:
int newWidth = (int) (density * 100);
which 100 is here based size.
Or you can have a ratio according to your screenWdith.
int newWidth = screenWidth / 2;
I am new to Android. I like having the free range of drawing objects where ever I want. So i have been using Absolute Layout. I get a message saying to use a different layout. And I have read that this is because of the different res of different phones. My question is, is this the only reason in not using Absolute Layout? I have made a method that uses metrics to adjust the pixels.
public int widthRatio(double ratioIn){
DisplayMetrics dm = new DisplayMetrics(); //gets screen properties
getWindowManager().getDefaultDisplay().getMetrics(dm);
double screenWidth = dm.widthPixels; //gets screen height
double ratio = screenWidth/100; //gets the ratio in terms of %
int displayWidth = (int)(ratio*ratioIn); //multiplies ratio desired % of screen
return displayWidth;
}
//method to get height or Ypos that is a one size fits all
public int heightRatio(double ratioIn){
DisplayMetrics dm = new DisplayMetrics(); //gets screen properties
getWindowManager().getDefaultDisplay().getMetrics(dm);
double screenHeight = dm.heightPixels; //gets screen height
double ratio = screenHeight/100; //gets the ratio in terms of %
int newHeight = (int)(ratio*ratioIn); //multiplies ratio by desired % of screen
return newHeight;
}
//sets size of any view (button, text, ...) to a one size fits all screens
public void setSizeByRatio(View object, int width, int height){
LayoutParams params = object.getLayoutParams();
params.width = widthRatio(width);
params.height = heightRatio(height);
}
So if i say setSizeByRatio(Button, 10, 25); It will set the buttons width to 10% of the width of the screen and the height to 25% percent of the screen.
Are there any phones that Absolute Layouts do not work on? Does this layout cause any other issues?
The reason why the AbsoluteLayout is deprecated is because you have alternatives in the LinearLayout or the GridLayout that does the same and more. It seems that you are trying to calculate positions based on absolute positions and number of pixels, an approach that should in general be avoided due to issues with varoius screen sizes and densities.
Read the link that #amiekuser provided and focus on the understanding how the best practice is. Some hints are creating images for ldpi, mdpi and hdpi folders, using the unit for dpi (density independent pixels) instead of raw pixels and how to test your app on multiple screen sizes and densities using the emulator.
Edit:
To set the x- and y-position of a View you must use LayoutParams. See this question on how to to set the TopMargin and LeftMargin for a View using LayoutParams.
Android phones comes in many form factors i.e. not only it varies greatly in terms of screen size(2.7", 3.2", 3.7" ....) but also in terms of resolution (480X800, 480X848 etc).
Google itself suggest not to use AbsoluteLayout. in fact its deprecated in the newer api versions.
The link below explains all these in details:
http://developer.android.com/guide/practices/screens_support.html
Check the best practices section.
This question already has answers here:
How do you make layouts for several Android screen sizes?
(3 answers)
Closed 9 years ago.
I am developing the app in 320*480. How do make the app to be run in 480*854 screen? When i tried to run in 480*854 screen, the app original design looks like small. Do I want to create separate layouts for each screen in android? If so, please provide me the sample hint to proceed.
I have implemented my own way of Handling Multiple Screen Resolutions.
You could certainly avoid this problem by setting LayoutParams at run time in terms of Percentage
The Problem occurs only with Views/Layouts that have some constant width or height lets say 280dp. Solution is quite simple if we programmatically set Layout Params of our Views/Layouts in terms of Percentage and only use constant width or height where necessary, elsewhere try to use match_parent to fill up empty space or use weights and define every View relative to other Views this will help your layout to look good in almost every screen resolutions
Here is a sample xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/mLayout"
android:layout_width="280px"
android:layout_height="300px" />
</RelativeLayout>
Notice: I have used px for fixed sized layout's width/height because in LayoutParams layoutParams = new LayoutParams(int width, int height); the width and height take value as pixels
Here is an example code of setting width and height in terms of percentage
final ViewTreeObserver mLayoutObserver = mLayout.getViewTreeObserver();
mLayoutObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener()
{
#Override
public void onGlobalLayout()
{
DisplayMetrics metrics = getResources().getDisplayMetrics();
int deviceWidth = metrics.widthPixels;
int deviceHeight = metrics.heightPixels;
float widthInPercentage = ( (float) 280 / 320 ) * 100;
float heightInPercentage = ( (float) 300 / 480 ) * 100;
int mLayoutWidth = (int) ( (widthInPercentage * deviceWidth) / 100 );
int mLayoutHeight = (int) ( (heightInPercentage * deviceHeight) / 100 );
LayoutParams layoutParams = new LayoutParams(mLayoutWidth, mLayoutHeight);
mLayout.setLayoutParams(layoutParams);
}
});
Now might be some people are wondering what is happening here
float widthInPercentage = ( (float) 280 / 320 ) * 100
Let me explain 280 is the width of my LinearLayout and 320 is the width of my device screen(on which i'm developing), i know currently i'm testing on a device having resolution 320 x 480, what i'm doing is calculating how much area my layout is covering in terms of percentage and then
int mLayoutWidth = (int) ( (widthInPercentage * deviceWidth) / 100 )
here i'm calculating the new width for my layout according to the screen resolution and by this way your Views/Layouts will look exactly the same on every screen resolution.
Conclusion: If you need to set some constant width/height for your Views/Layouts always set value in px in layout file (i.e xml) and then programmatically set LayoutParams.
A Suggestion for Google Android Engineers, i guess you guys should seriously think of changing the dp/dip units to percentage
for that you can make your layout dynamic with reference to available width and Height of the device
Display mDisplay= activity.getWindowManager().getDefaultDisplay();
int width= mDisplay.getWidth();
int Height= mDisplay.getHeight();
set your layout in terms of perecentage with reference to avail size
Mr. Babar,
I think you should follow the below formula instead of calculating like
float widthInPercentage = ( (float) 280 / 320 ) * 100;
float heightInPercentage = ( (float) 300 / 480 ) * 100;
int mLayoutWidth = (int) ( (widthInPercentage * deviceWidth) / 100 );
int mLayoutHeight = (int) ( (heightInPercentage * deviceHeight) / 100 );
I would suggest like if you take this as follows:
int baseWidth=320;
int baseHeight=480;
int mLayoutWidth = (int) ( (280* deviceWidth) / baseWidth);
int mLayoutHeight = (int) ( (300* deviceHeight) / baseHeight);
Also this is going to work for Views and not for text-sizes;
In Android, you don't go by the the absolute resolution, but rather focus on the screen density.
And yes you might need to create different layout to support various densities and screen sizes. In general there are four densities supported by Android (low, medium, high, extra high)
I suggest you read Supporting Multiple Screens for Android. I think it will help you achieve what you need to do. It has various sections on Designing alternative layouts and drawables and other useful info related to supporting multiple screen resolutions.
hi welcome to stack overflow...
From version 1.6 onward, Android supports multiple screen resolutions and densities separated into three classes:
Small: devices with a screen size smaller than the T-Mobile G1 or Samsung I7500, like the HTC Tattoo
Normal: devices with a screen size roughly the same as the G1 or I7500.
Large: devices with a screen size larger than the G1 or I7500 (such as a tablet-style device.)
Developers can control if and how apps appears to devices in each group by using tools introduced in the Android framework APIs and SDK. Details on implementation can be found in the Android Dev Guide article Supporting Multiple Screens. you may refer this link MultiResolution
or may use
Display displayparm= activity.getWindowManager().getDefaultDisplay();
int width= displayparm.getWidth();
int Height= displayparm.getHeight();