Android text shadow units - android

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.

Related

Jetpack Compose: Why is LocalConfiguration.current.screenWidthDp an Int and not a Float?

It seems having LocalConfiguration.current.screenWidthDp be an Int makes conversions from dp to pixels less accurate.
For example, for a device with density 420 and width in pixels of 1080, the width in dp = 1080 * 160/420 = 411.4285714.
However, if you calculate from dp to pixels using an Int of 411 or 412, then the pixels are 1078.875 or 1081.5. You can't do a simple round to get 1080 from these numbers.
Do most use cases prefer that LocalConfiguration.current.screenWidthDp be an Int?
LocalConfiguration.current is a CompositionLocal that exposes the platform's Configuration. In simple terms, you can think of it as an parameter that's automatically passed to each of your composables, which the runtime knows about in order to recompose when it changes. In that sense, this value is an int because it represents the Configuration's screenWidthDp int value.
The reason it's represented as an int at the platform level is because it will always be a positive integer value since that's what's used for determining the right resources to use in a given configuration. In other words, resource qualifiers deal with integer values, so the configuration exposes integer values.
If you're looking to do something like drawing a line the full width of a component, you'd work in pixels the whole time. Density independent pixels don't really make sense for that case because you don't care about the physical size of the line. Compare that to the case of something like a button where the physical size does matter since it needs to be roughly finger size or larger (~48dp+).
In cases where you do care about the physical size, you'd start with dp and convert to pixels when you need to do the actual drawing (typically just using something like 16.dp.toPx())

How can I get the actual text size in px(not textSize attribute)?

My question is not about the number of chars nor the textSize attribute in TextView/EditText.
I have a fixed EditText. And the maxLength is 10. And all the chars must be shown at once.
However, each char has different width and height. For example, l and L. If you drag the mouse over(block selection) the l and then the L. You will notice that they have different width.
So, LLLLLLLLLL and llllllllll has different width.
For instance,
LLLLLLLLLL
llllllllll
(both are 10 letters)
Moreover, different font shows different width.
My question is how to get the actual width of chars in PX so that the string won't hidden in the EditText. Let's say, the width of EditText is 200px. And it's limited to 10 letters. And it will show
LLLL(LLLLLL) // (LLLLL) is not shown because of the EditText's width. But you can see when you move the cursor.
llllllllll
How can you get the width of the chars?
you can use TextPaint and measure letters, but there is an easier way for your case. get familiar with em unit
you can declare this value for EditText/TextView by setEms(intNum) or android:ems="intNum". set also width to wrap_content and your View will have width of intNum widest characters (usually Ms). there are also useful minEms and maxEms attributes
edit: HERE you have more complex explanation
PS. don't declare sizes of your Views with px unit due to different densities, use dp. some units explanation in HERE

what is fadingEdgeLength attribute meant for?

what is the use of android:fadingEdgeLength attribute meant for. i have seen this attribute for some views and wigdets in android project's xml
It's used for adjusting the length of the fading edge that you see when you scroll. You can see the effects on the edges of the screen in the picture below.
From the android official documentation:
Defines the length of the fading edges.
Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).
The fadingEdgeLengthmanages the size of fading shown at edges of a View.
For instance, a ScrollView or a ListView.
You can combine threquiresFadingEdge to show it horizontally or vertically or none (the latter, in order not to show any).

Wrong dimensions - android

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.

Why is my TextView padding unpredictable in pixels

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)

Categories

Resources