Android elevation and setElevation not the same effect - android

I noticed when I use elevation property in xml and set it to 4dp, I get a normal shadow. When I use setElevation(4) in java, I get less of a shadow than the xml property does. How can I fix that?

The answer given by Anton Kovalyov is almost correct, but it is actually the other way around. To get the correct elevation you need to convert your dp to pixels and feed it into setElevation, so the correct answer looks like this:
setElevation(4 * context.getResources().getDisplayMetrics().density);
setElevation takes a pixel value. User TinTran's comment is correct.

According to android documentation setElevation()
Sets the base elevation of this view, in pixels.
You need to convert pixels to dpi:
setElevation(4 / context.getResources().getDisplayMetrics().density);

That is because java use pixel. so you have to convert it into dp
you can use this :
(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources().getDisplayMetrics())

Related

Android lineHeight vs lineSpacingExtra

can I know what is the difference between lineHeight and lineSpacingExtra in android xml. I tried to compare both, but I got the different result (I need the lineHeight function, however it is only supported in API 28).
Here is part of my code:
left:
android:textSize="14sp"
android:lineSpacingExtra="6dp"
right:
android:textSize="14sp"
android:lineHeight="20dp"
Result:
Any solution for this?
Thank you.
You mentioned you want to set lineHeight in pre-API 28, an alternative approach would be to just set a small lineSpacingExtra / lineSpacingMultiplier value (as you have shown). Otherwise, there are many solutions to setting a line height yourself in this related question.
I'll briefly cover the differences below, with my summary and the official documentation.
android:lineHeight is the total height of each line. This includes the text, as well as any padding on the top and bottom.
Explicit height between lines of text. If set, this will override the values set for lineSpacingExtra and lineSpacingMultiplier.
android:lineSpacingExtra is the additional spacing added after each line of text (except the last one).
Extra spacing between lines of text. The value will not be applied for the last line of text.
Finally, you may be interested in android:lineSpacingMultiplier. This is similar to lineSpacingExtra, but with a multiplier value of the current height (e.g. 1.2) instead of a fixed value.
Extra spacing between lines of text, as a multiplier. The value will not be applied for the last line of text.
Further information (besides the included quotas) is available in the TextView documentation.
MaterialTextView (MTV) includes the ability to set the android:lineHeight.
And if you use the app: prefix, it is backwards compatible until SDK-Version 21.
Btw., under some circumstances (i believe its depending on your style, but am not sure about it), standard TextViews are automatically replaced with MTVs. (Check your LayoutInspector, you might already use them without knowing it)
In case you're still wondering about the differences, there's an excellent talk by the android team about text in general; which should solve your question at ~16:50.
android:lineHeight defines Explicit height between lines of text. while android:lineSpacingExtra defines Extra spacing between lines of text
you can read more android_documentation_for_text_arributes
android:lineHeight
is used to specify the total height for single line of text.
android:lineSpacingExtra
is used to add extra spacing between 2 lines of text in addition to the default line height used by android.
In order to replicate lineHeight functionality with API level < 28, set the lineSpacingMultiplier to 0 and set the lineSpacingExtra to the lineHeight that you want to use
android:lineSpacingMultiplier="0"
android:lineSpacingExtra="<enter line height value here in dp>"

How to change card_view:cardCornerRadius programmatically

In my project, I am using RecyclerView in my listing using CardView. In my listing I have to set the corner radius of CardView dynamically based on device.
Is there any way to set cardview corner radius value dynamically?
Thanks.
Use CardView.setRadius(float), see androidx.cardview.
(It is worth noting that this will have no effect if you also invoke setBackgroundColor, make sure to use setCardBackgroundColor instead).
You must specify a pixel size, rather than dp value, e.g. for corner radius of 4dp you can invoke, in Kotlin:
radius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4f, context.resources.displayMetrics)

What is the default height on Androids TabLayout

Seems like something that should be easy to find, but it's harder than I thought.
What is the default height on the TabLayout in DIPs?
Material Design guidelines state that this height equals 48dp when You have text or image, and 72dp when You have image and text.
its height is "?attr/tabIndicatorHeight"
E.g.
you can use this value in an xml file for another view like:
android:layout_marginBottom="?attr/tabIndicatorHeight"
And it will look ok on all devices.

Set text size in .xml or programmatically

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 -.-

Android text shadow units

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.

Categories

Resources