I have an edittext with its textsize set in the layout and another one with the size set programmatically, but what is supposed to be the same size, doesn't match, its bigger.
For performance i don't want to use:
setTextSize(TypedValue.COMPLEX_UNIT_SP, 65);
(that works well). I want to make the calculation before, so I tryed:
sm = (float) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 18, context.getResources().getDisplayMetrics());
sm = (float) context.getResources().getDimension(R.dimen.dsma);
with
<dimen name="dsma">18sp</dimen>
in dimens.
And then
edit.settextsize(sm);
Both gives me big a big font. Fix? tnx.
edit.setTextSize(18);
Theres no way to avoid the calculation, since its done anyway, a simple int(float) will be considered sp.
Credits to pskink, tnx.
Related
In my App I am specifying the width of a few elements dynamically using
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, mycontext.getResources().getDisplayMetrics());
I am also using some other elements whose width are specified in the layout xml in dp terms.
However when I run the app on my Samsung Galaxy Grand, I observe that 100 dip specifed dynamically is not exactly equal to 100 dp specified in the layout xml(slightly more).
However, when I test it on a AVD it appears to be fine(both width matches).
Has anyone else encountered this issue as well?
Any Idea?
Found the issue.
I was doing :
int pixels_unit_five = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, mycontext.getResources().getDisplayMetrics());
Then I was using ths value for views with different widths like:
android.widget.RelativeLayout.LayoutParams params_item = new RelativeLayout.LayoutParams(pixels_unit_five*20,pixels_unit_five*8 );
android.widget.RelativeLayout.LayoutParams params_item = new RelativeLayout.LayoutParams(pixels_unit_five*10,pixels_unit_five*8 );
And so on..
The dip value is being cast to int and that was doing minor rounding off to fit the exact pixel value.This, when multiplied by 20 added up to a considerable difference in pixel value.
Solved it by obtaining exact dip value straightaway and using them.
I have variable at dimens.xml
<resources>
<dimen name="btn_text_size">12sp</dimen>
</resources>
And i can use it in layout file:
<TextView
android:textSize="#dimen/btn_text_size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/dialog_tags_complete"
/>
or programmatically
tagButton.setTextSize(c.getResources().getDimension(R.dimen.tag_text_size));
But this 2 methods give different results. I know that getDimension are based on the current DisplayMetrics associated with the resources.
But what should i do to make this 2 ways looks the same?
setTextSize( float ) expects a scaled pixel value. So, setTextSize( 12 ) would give you the desired result. However, getDimension() and getDimensionPixelSize() return the size in units of pixels, so you need to use the unit-typed variant of setTextSize() as follows:
setTextSize( TypedValue.COMPLEX_UNIT_PX, getDimensionPixelSize( R.dimen.tag_text_size ) );
tagButton.setTextSize(c.getResources().getDimensionPixelSize(R.dimen.tag_text_size));
this will work just fine :)
You should also remember that textView has a setTextSize(int unit,float size), which should be used while setting size from code but not from xml dimen.
I have currently the same thing. Did set a dimension in dimens.xml and applied it programmatically, which is 3 times that big, than when settings via xml.
I checked also:
TextView.getTextSize() = 92f
getResources().getDimension(R.dimen ...) = 92f
TextView.setTextSize(92) != TextView with size from XML, other flags like TypedValue.COMPLEX_UNIT_PX make it even bigger.
The default setTextSize does apply COMPLEX_UNIT_SP by default btw. So once again, the Android API is inconsistent, so setting programmatically only and adapt sizes, so they fit, will be my solution.
Edit: Setting text size programmatically under Galaxy Note 2 (4.4.2) vs Note 4 (5.0.1) leads to a totally different result -.-
I have extended the TextView and added support for borders, the thing is when I am drawing a border I need to put padding on the bordered side, so that the text would move.
I set my widths of borders in pixels, and it draws them accordingly, but on my TF201 tablet when I setPadding on the TextView, out of some reason it multiplies the padding width by 3x in pixels even though the setpadding documentation says it is defined explicitly in pixels.
EDIT:
Even though the answer I have selected is not what was causing my issue, it is a valid answer. The real answer to my question is actually a duplicate from this. Problem was that I have added a value to my padding each time setPadding was called. And it does get called three times on a page that has scrolling to it.
It might be a issue of pixel density. Its true that setpadding docs asks to set the padding in pixels but are you setting it in px, sp or dp ? If you read Supporting Different Densities document it says and I quote:
Different screens have different pixel densities,so the same number of pixels may correspond to different physical sizes on different devices.
So, when you specify spacing between two views, use dp rather than px:
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/clickme"
android:layout_marginTop="20dp" />
When specifying text size, always use sp:
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp" />
Also, based on your comments:
drawRect unit issues android andDraw Rectangle which change size w.r.t different android screen size question might help.
While the method may only accept pixel values, that sadly doesn't save you from needing to take screen densities into account. Instead, you need to determine your values in terms of DP and then programmatically calculate the pixel equivalents at runtime. Fortunately, there are some built-in methods to help you out. This can be done with the following code:
/// Converts 14 dip into its equivalent px
int dimensionInDp = 14;
Resources r = getResources();
float dimensionInPixels = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dimensionInDp, r.getDisplayMetrics());
Although the result is a float, you can easily cast it to an int for use in your setPadding(...) method.
(Referencing: Converting pixels to dp)
I've been having trouble finding a generic way to size UI components so they look good on all devices. I have some ideas but could use any feedback on them or suggestions for better strategies.
Take a simple situation like a dialog with a single EditText which a user will enter a fairly long String into. On smaller devices I would like the EditText to be as wide as possible (because its likely that the maximum size possible on these devices isn't all that large). However, if I set the width of this EditText to FILL_PARENT, the dialog will look silly on tablets, because it will be much, much wider than it needs to be. Also, setting the width to some hard coded DIP value, like 500, isn't great because it doesn't maximize available space on smaller devices, won't expand to take up additional space if the device is rotated, and could be an issue on devices narrowers than 500 DIP.
What I would like to do is approximate the behavior FILL_PARENT by hardcoding the width of the EditText to of the screen's width, but to prevent the EditText from growing to wide on large devices by not allowing the width to be larger than some DIP value, like 500.
I have encountered 2 problems with this strategy:
1: If I set the EditText's width to the screen's width, the EditText doesn't fit on the screen (as shown in the below image). I fixed this by setting the width to 90% of screen width, but this is questionable (what if the device's dialogs have larger margins around them?).
2: My app sets android:configChanges flag in its application manifest such that dialogs don't get dismissed on rotation. But this means that dialogs don't get resized on rotation. So, if the device rotates from landscape to portrait while the dialog is visible, then the EditText will be too wide for the screen; and if it goes the opposite direction, then the EditText will not take up all the available space. I guess this would be fixable by relaying out my dialog on rotate, but it seems painful/ugly for my Activity to have to keep track of the visible Dialog and force it to relayout on rotation (particularly because each dialog would then have re-populate any field values when after relayout).
So, any thoughts on improving my solution, or on alternatives?
class SampleDialog extends Dialog
{
public SampleDialog(Context context)
{
super(context);
setContentView(getContentView());
}
private View getContentView()
{
TextView label = new TextView(getContext());
label.setText("A label:");
int fiveHundredDips = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 500, activity.getResources().getDisplayMetrics());
Display display = getWindowManager().getDefaultDisplay();
float width = .9f * display.getWidth();
Math.min(width, fiveHundredDips);
LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams((int) width, LinearLayout.LayoutParams.WRAP_CONTENT);
EditText editText = new EditText(getContext());
editText.setLayoutParams(linearParams);
LinearLayout layout = new LinearLayout(getContext());
layout.addView(label);
layout.addView(editText);
return layout;
}
}
}
You could use different folders in your resources folder, which define different qualifiers for different devices. You can then specify say on a small device you would like a dimension to be 'x' big, and on a tablet to be 'x' big.
Another solution would be to programatically detect if the device is a Tablet, and perform actions based on that.
I would recommend you look into resource qualifiers, which are a very powerful tool in Android.
Take a look at the android:minWidth property. It's available on TextView as well as others. It seems the minWidth (set using dip units) would be a good option for you. You can make the text boxes large enough but not too large.
These values can be set programmatically. You might also find the following code useful in determining what values you want to use for the minWidth:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
Log.d("density", metrics.density);
Log.d("width dip", (int) (metrics.widthPixels / metrics.density));
Log.d("height dip", (int) (metrics.heightPixels / metrics.density));
Android text shadows have shadowDx and shadowDy to specify the shadow's offset. These are floats and are a factor rather than absolute units. The answer given here implies that there's no easy way of specifying the shadow's position in pixels or dips: TextView:shadowDx/Dy/Radius in dip?
So... what do the units mean? If I give a shadowDx of 1.5, that's 1.5 what? 1.5 times the text size?
OK, I guess the units are physical pixels. The fact that the documentation doesn't state what the units are (it just says "Must be a floating point value"), and that you don't get to choose which units (pixels or dip), was confusing me.
Actually, I think the unit is pd, not pixels.