[android-developers] Density independent values through code for views
Hi All, I have added views to layout in xml through code(i.e dynamically).And given properties for button such as topMargin,leftMargin etc.Yeah it will appear properly in one device.Because I gave values as per adjustment to the current device(hdpi854) but if I will run the same code on other screen devices i.e for mdpi it will give more gap. If we write views in xml ,we use to give values such as 100dp,100dpi etc.So, it can adjust according to the screen size.But if we give these values dynamically ,it always I think take in terms of pixels. One of the solution can be multiplying these values with ratio (according to device).Is there any other way to do the same? -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
Re: [android-developers] Density independent values through code for views
On 15 February 2011 15:03, Deepak Kumar deepak.kumar...@gmail.com wrote: One of the solution can be multiplying these values with ratio (according to device).Is there any other way to do the same? Don't think there're much alternatives. What I do is keep all the values in dip in my code (for items I need to draw/adjust etc from code) and on setting/drawing have them multiplied by getResources().getDisplayMetrics().density and that's shall do the trick ( +0.5f for rounding). -- Regards, Marcin -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
Re: [android-developers] Density independent values through code for views
On 15/02/2011 15:03, Deepak Kumar wrote: One of the solution can be multiplying these values with ratio (according to device).Is there any other way to do the same? In code, I define constants in 'dp' and then multiply with DisplayMetrics#density http://developer.android.com/reference/android/util/DisplayMetrics.html#density at runtime to get the correct pixel value. Pepijn -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
Re: [android-developers] Density independent values through code for views
You can look into writing a utility method e.g. : public class Util { private static float DIP_SCALE = -1; public static int dipsToPx(int dips, Context context) { if(DIP_SCALE == -1) DIP_SCALE = context.getResources().getDisplayMetrics().density; return (int)(dips * DIP_SCALE + 0.5f); } } and using that in your code... On 15 Feb 2011, at 14:13, Pepijn Van Eeckhoudt wrote: On 15/02/2011 15:03, Deepak Kumar wrote: One of the solution can be multiplying these values with ratio (according to device).Is there any other way to do the same? In code, I define constants in 'dp' and then multiply with DisplayMetrics#density at runtime to get the correct pixel value. Pepijn -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
Re: [android-developers] Density independent values through code for views
Thanks for the code.It is proper for view alignment within layout in different devices.But for textSize of textview its not happening properly. I am using following code:- TextView txtForIcon=new TextView(this); txtForIcon.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); txtForIcon.setTextSize(*Utils.dipsToPx(11,this))*; txtForIcon.setText(name); size 11 is for hdpi(480*854) but for mdpi(320*480).It is appearing very small. Thanks, On Tue, Feb 15, 2011 at 7:54 PM, Richard Leggett richard.legg...@gmail.comwrote: You can look into writing a utility method e.g. : *public* *class* Util { *private* *static* *float* *DIP_SCALE* = -1; *public* *static* *int* dipsToPx(*int* dips, Context context) { *if*(*DIP_SCALE* == -1) *DIP_SCALE* = context.getResources().getDisplayMetrics().density; *return* (*int*)(dips * *DIP_SCALE* + 0.5f); } } and using that in your code... On 15 Feb 2011, at 14:13, Pepijn Van Eeckhoudt wrote: On 15/02/2011 15:03, Deepak Kumar wrote: One of the solution can be multiplying these values with ratio (according to device).Is there any other way to do the same? In code, I define constants in 'dp' and then multiply with DisplayMetrics#densityhttp://developer.android.com/reference/android/util/DisplayMetrics.html#densityat runtime to get the correct pixel value. Pepijn -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
Re: [android-developers] Density independent values through code for views
That is the correct code. What do you mean by appearing very small? Of course at mdpi it will be significantly smaller than hdpi. On Tue, Feb 15, 2011 at 8:29 AM, Deepak Kumar deepak.kumar...@gmail.comwrote: Thanks for the code.It is proper for view alignment within layout in different devices.But for textSize of textview its not happening properly. I am using following code:- TextView txtForIcon=new TextView(this); txtForIcon.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); txtForIcon.setTextSize(*Utils.dipsToPx(11,this))*; txtForIcon.setText(name); size 11 is for hdpi(480*854) but for mdpi(320*480).It is appearing very small. Thanks, On Tue, Feb 15, 2011 at 7:54 PM, Richard Leggett richard.legg...@gmail.com wrote: You can look into writing a utility method e.g. : *public* *class* Util { *private* *static* *float* *DIP_SCALE* = -1; *public* *static* *int* dipsToPx(*int* dips, Context context) { *if*(*DIP_SCALE* == -1) *DIP_SCALE* = context.getResources().getDisplayMetrics().density; *return* (*int*)(dips * *DIP_SCALE* + 0.5f); } } and using that in your code... On 15 Feb 2011, at 14:13, Pepijn Van Eeckhoudt wrote: On 15/02/2011 15:03, Deepak Kumar wrote: One of the solution can be multiplying these values with ratio (according to device).Is there any other way to do the same? In code, I define constants in 'dp' and then multiply with DisplayMetrics#densityhttp://developer.android.com/reference/android/util/DisplayMetrics.html#densityat runtime to get the correct pixel value. Pepijn -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -- Dianne Hackborn Android framework engineer hack...@android.com Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails. All such questions should be posted on public forums, where I and others can see and answer them. -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
Re: [android-developers] Density independent values through code for views
(For the code you have, at mdpi the density scale is 1, so you just end up with a size of 11, which yes is pretty small.) On Tue, Feb 15, 2011 at 9:07 AM, Dianne Hackborn hack...@android.comwrote: That is the correct code. What do you mean by appearing very small? Of course at mdpi it will be significantly smaller than hdpi. On Tue, Feb 15, 2011 at 8:29 AM, Deepak Kumar deepak.kumar...@gmail.comwrote: Thanks for the code.It is proper for view alignment within layout in different devices.But for textSize of textview its not happening properly. I am using following code:- TextView txtForIcon=new TextView(this); txtForIcon.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); txtForIcon.setTextSize(*Utils.dipsToPx(11,this))*; txtForIcon.setText(name); size 11 is for hdpi(480*854) but for mdpi(320*480).It is appearing very small. Thanks, On Tue, Feb 15, 2011 at 7:54 PM, Richard Leggett richard.legg...@gmail.com wrote: You can look into writing a utility method e.g. : *public* *class* Util { *private* *static* *float* *DIP_SCALE* = -1; *public* *static* *int* dipsToPx(*int* dips, Context context) { *if*(*DIP_SCALE* == -1) *DIP_SCALE* = context.getResources().getDisplayMetrics().density; *return* (*int*)(dips * *DIP_SCALE* + 0.5f); } } and using that in your code... On 15 Feb 2011, at 14:13, Pepijn Van Eeckhoudt wrote: On 15/02/2011 15:03, Deepak Kumar wrote: One of the solution can be multiplying these values with ratio (according to device).Is there any other way to do the same? In code, I define constants in 'dp' and then multiply with DisplayMetrics#densityhttp://developer.android.com/reference/android/util/DisplayMetrics.html#densityat runtime to get the correct pixel value. Pepijn -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -- Dianne Hackborn Android framework engineer hack...@android.com Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails. All such questions should be posted on public forums, where I and others can see and answer them. -- Dianne Hackborn Android framework engineer hack...@android.com Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails. All such questions should be posted on public forums, where I and others can see and answer them. -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en