I want to programmatically set in my layout size. And this layout size get from dimen folder. but the problem is when I get the value from dimen folder, It gets displayed in a normal resolution screen perfectly but when it runs in a high display resolution it doesn't run perfectly
You can set layout size programmatically by using one of the following ways:
Method 1:
int width = getResources().getDimensionPixelSize(R.dimen.layout_width);
int height = getResources().getDimensionPixelSize(R.dimen.layout_height);
Method 2:
int width = (int) (widthdpValue * Resources.getSystem().getDisplayMetrics().density);
int height = (int) (heightdpValue * Resources.getSystem().getDisplayMetrics().density);`
Set this width and height to the layout.
You have to convert the dp value you get from the dimen folder into the proper number of pixels for the device's screen density. Example:
float dp = 5.0f // This is the dp value from your dimen file
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, displayMetrics);
Where px is the scaled pixel value that will properly display on the device.
Refer to this link Different values folders in android!
Create a "values" folder for each screen density and define the size of your layout in each "dimensions.xml" file. This will give you the ability to use different sizes for the same layout without using any piece of Java code.
Hope this helps :)
Yes I solved my screen resolution problem.
When you get value from dimen folder it get in Pixel format. And Pixel size depends on your device resolution Not device size.
So you need create screen size and screen resolution based dimen folder. As like:
In this folder categorized in different screen size and different screen resolution.
You can't use only one dimens for all the devices layout, please try to create some value resouce folder with different size
And u can refer to my project as below which can help u to gen the dimens in different floder automatically
https://github.com/Vaycent/genDimensXml
I hope this can help u
Related
I created my app to support screens from hdpi to xxxdpi and everything was working perfect. Images were shown just like they're supposed to. But, now I have a problem with Xiaomi Mi Max 3. It has 6,9" display with a resolution of only 1080x2160 which gives a ~350 dpi. That falls into xhdpi category (usually 720p screens) and now my images are smaller then they should be. Is there any way to force the use of drawable-xxhdpi folder with code?
just create another file like as:
drawable-xxxdpi/example.png
drawable-anydpi-v21/example.xml
and link your image file from anydpi xml code for supporting every screen size.
see more information from official website or visit What Is The Difference Between -anydpi And -nodpi?
Ok, thanks to Md.Abdullah I found a solution.
You cannot choose which folder of resources to use but you can use specific resource from a folder with getDrawableForDensity() method. This is what I did:
int width = Resources.getSystem().getDisplayMetrics().widthPixels;
int dpi = getResources().getDisplayMetrics().densityDpi;
int imageID = R.drawable.YOURIMAGE;
if (width > VALUE && dpi < VALUE) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
Drawable drawable = getResources().getDrawableForDensity(imageId, DisplayMetrics.DENSITY_XXHIGH);
//you will probably need to adjust width & height of your ImageView, but since the image
//will keep its aspect ratio, you only have to change width | heigth
YOURIMAGEVIEW.setAdjustViewBounds(true);
YOURIMAGEVIEW.setMaxWidth(VALUE);
YOURIMAGEVIEW.setImageDrawable(drawable);
}
}
Is it possible to use common hdpi folder for all screen densities? I have quite a lot images. If I make specific copies to folders drawable-hdpi, drawable-ldpi, drawable-xhdpi, ... but it takes huge data (backgrounds, bitmaps).
Is it possible to set only one drawable folder for all devices and then rescale according to a specific device programmatically?
I think about this code to get display size of the scree:
Display display = getWindowManager().getDefaultDisplay();
width = display.getWidth();
height = display.getHeight();
Then I will get display density of the device, something like this:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
density = metrics.density; // 1 - 1,5 - 2 .....
The I will recalculate size of imageview with density:
ImageView logo = (ImageView)findViewById(R.id.logo);
LinearLayout.LayoutParams logo1 = (LinearLayout.LayoutParams) logo.getLayoutParams();
logo1.width = (int)(logo.getWidth()*density);
logo1.height = (int)(logo.getHeight()*density);
logo1.leftMargin=(int)(logo1.leftMargin*density); // for margin
logo1.topMargin=(int)(logo1.topMargin*density); // for margin
logo1.rightMargin=(int)(logo1.rightMargin*density); // for margin
logo1.bottomMargin=(int)(logo1.bottomMargin*density); // for margin
My main problem is I need to have all proportions of graphic same on all devices. It means I must recalculate imageViews accroding to the screen size.
Is this a right way to get density independent screen? How does android work on other devices if only hdpi folder contains files. Does it take files from this folder? Can I set one common drawable folder to all densities?
I would strongly (strongly) advise against doing this. However, if you want the system to rescale your image assets, design them for mdpi (the baseline density) and put them in drawable/.
That said, you need at least mdpi and hdpi to get reasonable scaling (since hdpi is 1.5x mdpi, scaling algorithms produce worse results than for the other conversions from mdpi).
Make sure you've read and understood Providing Resources and Supporting Multiple Screens before you start dealing with resources.
P.S. The layout solution is wrong for a few reasons (e.g., setting margins instead of size) but it's also the completely wrong thing to do. Don't do it!
How can I put different resources for different dpi on ICS with the same sw800dp smallest width?
Details: There are two tablets with ICS 4.0.4. First one has 1280x800 resolution and mdpi (160) density. Second one has 1920x1200 resolution and hdpi (240) density. So in terms of smallest width they both have the same sw800dp qualifier but different mdpi/hdpi density qualifiers.
I need to have different layouts and images for these two resolutions.
So I created two directories:
layout-sw800dp-mdpi
layout-sw800dp-hdpi
I thought that each device will choose its own directory according to the smallest width AND density.
BUT both of them take resources from the same sw800dp-hdpi folder!
I'm very confused and do not know how to separate resources for that two different resolutions.
Any help is really appreciated.
Thanks in advance.
You should use the same layout, located in /layout/sw800dp, and create /drawable-mdpi , /drawable-hdpi to put your custom drawables, the system will apply the correct ones to each device, using the same layout. Those devices should have similar size and aspect...
Use this to get the density:
float density = getBaseContext().getResources().getDisplayMetrics().density;
Screen height:
int h = 0;
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
h = (int) display.getHeight();
Screen width:
int w = 0;
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
w = (int) display.getWidth()
After that just select the correct resources.
Here i develop one Android application, which can run on all screen size and resolution devices. But one problem is there my TextView's Fontsize is same on all the Screen-Size. I want to change FontSize according to Different ScreenSize and Screen Resolution.
Thanks in Advance.
Use the code from Screen Category or use getSize() method like:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
as described here to get the screen size and and then set the font size accordingly using setTextSize() method, you can also consider using sp unit for font size.
First, if you have not done so already, you should read this
http://developer.android.com/guide/practices/screens_support.html
To provide any resource, including styles that could apply to text, you should read the section Using configuration qualifiers
Another useful document here http://developer.android.com/guide/topics/resources/more-resources.html#Dimension should help you with selecting the right unit of measure for text, ideally you want to use sp's as explained in the excerpt:
sp
Scale-independent Pixels - This is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you
use this unit when specifying font sizes, so they will be adjusted for
both the screen density and the user's preference.
Hope that helps.
Hi create the folders as below in your resources folder and then copy your XML files in to it now you can check the palette window it will show the screens for different sizes based on that u can modify the screen size .
layout-large, layout-small, layout-xlarge ,
Now it supports to all types of screen sizes and your Font size will be clear based on screen size. For more information regarding to Supporting Multiple Screens check the android documentation .
automatic adjust font size as per screen by using this code
Display display;
Point size;
int width, height;
float txtsize;
declare and use in oncreate()
display = getWindowManager().getDefaultDisplay();
size = new Point();
display.getSize(size);
width = size.x;
height = size.y;
txtsize=height*0.024f;
/* if your screen height is 854 its use font size 20.4*/
to set size to textview just use this code..
textView.setTextSize(txtsize);
one way to do this is make necessary folders like layout-large, layout-small, layout-normal,layout-xlarge in res folder . and put your xmls into those folder and then change whatever you want to do with text view and anything
Go to your xml file and add textsize as:
android:textSize="20sp"
This will increase your font size
Why dp used in the XML files are not the same as dp used in activities? When I create a textView in the XML file with 20dp of width, its size will be the same in all different type of screens, but when I use this:
int sizeInDip = 20;
int width= TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, sizeInDip, getResources().getDisplayMetrics());
The size is different in all types of screen.
I want to know if there's a way to get the same result using dp in both, activities and XML files.
Thank you in advance!
Why are you doing it that way? IIRC, theres a TextView.setSize method you can invoke programatically and just set the size as 20dp that. Also, you should use sp(scaleable pixels) for text sizes