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
Related
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
when declaring some element (let's say a Button) and giving it some width and height (let's say 200dp) from the XML file, I got certain result when running, although when make the same steps but Programmatically I got much smaller width and height, and this case happens only with me in Nexus tablets.
If you set the size of a View programmatically, many times pixels are taken as an argument. You will need to convert your desired size in DPs to pixels first, and use those to set the width or height of your element:
float dp = 200;
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
See the API-docs of the specific Views for infos about what kind of dimensions are taken as an argument.
With the use of weight my layouts are really perfect on every screen size and density. Every view is perfectly scaled.
Unfortunately there is still a big problem with characters size: no matter if I define text size with sp or dp, it still resizes based on density and not on size of the screen, so on tablets text is microtiny!!
What's the best solution for this problem?
Shall I define different layouts for tablets? And in that case, how can I do it?
Is there a way to auto-size the text, in xml or by code (I don't care much which one, if it works)?
Thank you very much.
You can create separate styles for different screen size qualifiers small, normal, large, xlarge, and a bunch of new sw<N>dp, h<N>dp, w<N>dp
Supporting Multiple Screens
Solution 1
use different layouts for different screen size
Solution 2
Use customView and change the text size dynamically. For example
public class CustomView extends TextView{
....................
#Override
protected void onDraw(Canvas canvas) {
calculate the view size and change the text size
}
}
UPDATE
To measure the size you can use following
float size = getHeight() * .8; // getheight will return view's height
setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
You can make use of dimens.xml which you need to create in your values folder and create values folder as values-large values-small values-xlarge etc.....
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
I believe you can set layout_width and layout_height to textview element dynamically using something like:
LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
centimeterLayout.addView(textview, lparams);
But - how do I set the layout width and height in other units - e.g. I want to set the layout width of my text view to 20 mm. How do I do that?
LayoutParams either take one of the constants (FILL_PARENT, WRAP_CONTENT) or a pixel value. Since you want to use millimeters, you have to convert a millimeter dimension to a pixel dimension. Doing so is pretty easy:
Resources r = getResources();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, 20,
r.getDisplayMetrics());
In this example, 20 mm are converted into a pixel dimension that you can use in your LayoutParams. You can use other dimensions too, such as TypedValue.COMPLEX_UNIT_DIP for dp/dip¹. Just change the constant in the arguments. The android doc has a list of useable dimensions.
¹ Density independend pixels, a highly recommended dimension that adjusts to the many different screen sizes and densities. Using mm isn't the best way to do things, since your text will probably look okay on a big screen and fill a big portion of a small screen, or the other way around - which is not ideal. I also recommend sp for font sizes. See the dimension list for details.