Are Android XML style attributes not overridden in order? - android

I have a TextView which should have 10dp padding in the top and bottom, and 30dp on the left. I then tried:
android:padding="10dp"
android:paddingLeft="30dp"
That's what I'd do in CSS. But it doesn't work.
Regardless of the order, my left padding is 10dp. Are Android style attributes not overridden in the order they appear? If so, is there any list of priorities?
Ex: android:padding always overrides android:paddingLeft
Overriding is an important feature in CSS which saves a lot of coding, so I wanted to know if that's possible in Android.
Maybe I just couldn't find the answer with the right terms. If someone could point me where to understand it I'd be very appreciated.

The Android layout system is different from CSS. It does not care about the order of the properties, and padding always overrides paddingLeft, paddingRight, etc.
You can find the relevant source code in View.java here (padding is set) and here (if padding is set it is used, even if paddingLeft is set).
This is the most important snippet:
if (padding >= 0) {
leftPadding = padding;
topPadding = padding;
rightPadding = padding;
bottomPadding = padding;
mUserPaddingLeftInitial = padding;
mUserPaddingRightInitial = padding;
}

As I understand from the source code,
when you set the padding attribute it can be overridden by other padding attributes.

Related

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 plus padding and minus padding?

I have a very small question which occurred while reading wrap content and parent content that what is plus padding and minus padding?
According to me i think plus padding means increase distance (from margin) x-axis and decrease x-axis . is it so?
Related answer: Difference between a View's Padding and Margin
Padding can be seen as an internal margin, or a margin applied to elements inside the padded element, as seen in f.ex. a TextView, where a padding would shift the text away from the border, or make the TextView larger to accomodate the padding. In a layout, using padding will shift all the content away from the padded edges. Using negative padding will shift the content towards the edges, so using android:paddingLeft = "-5px" would shift the content to the left
Yes you are right.
For example ->
android:paddingLeft = "4dp" means it will take shift the text to right by 4dp.
android:paddingLeft = "-4dp" means it will take shift the text to left by 4dp.

Is setPadding broken for LinearLayout?

I have a very short problem. I have a custom control that is based upon LinearLayout. I inflate it's view from xml', in which the root is set as element.
Now when I try to set the padding of this custom control by "this.setPadding(x,x,x,x)" it does not affect the TextView and ImageView I add to this control in a few lines of code later.
Currently I am bypysing it, by setting the margin of each control separately, to achieve the global padding of the whole custom control, but it would be nice to know if there is a catch in using setPadding dynamicaly, or maybe a mistake is mine.
To simplify, my case looks like that:
setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
LinearLayout.LayoutParams lp = new
LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT); setLayoutParams(lp);
setPadding(0, 30, 0, 30); //I know it's in px
Afterwards I'm adding a large image, which shrinks due to it's size, but the padding of LinearLayout(which I try to set dynamicaly) does not affect it, only if I set margin on it directly.
Any tip would be greatly appriciated.
After a little of digging, found an answer through other StackOverflow question:
https://stackoverflow.com/a/13363318/905938
Basicaly if one person sets a background reasource with a selector xml given (as in my case) it overrides completely the previous padding setting. So the padding I was setting within the custom control initialization was lost as soon as it was set.
Now that I know the problem, I basicaly just intercept the call to this method in my custom control like this:
#Override
public void setBackgroundResource(int resid)
{
int paddingLeft, paddingTop, paddingRight, paddingBottom;
paddingLeft = getPaddingLeft();
paddingTop = getPaddingTop();
paddingRight = getPaddingRight();
paddingBottom = getPaddingBottom();
super.setBackgroundResource(resid);
setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
}
That solves the problem, and I hope will solve a problem for anybody who will find this question with a similar problem.

Android Override 9-patch padding in XML of one side

When using a 9-patch image as a background, the padding seems to be always derived from the 9-patch image. Even if you do not use a padding bar in the 9 patch image it uses the drawable.
If the padding lines are not included, Android uses the left and top lines to define this drawable area.
http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch
However you can override it in the XML by using android:padding=0dp or =Xdp.
Unfortunately using android:paddingLeft=Xdp does not work. So you are stuck with uniform padding.
I tried doing this:
android:padding="2dp"
android:paddingLeft="20dp"
It had no effect on the padding on the left. Placing them in a styles.xml produced similar results.
The only hack I have seen to get around this was to set the padding in code.
if (android:padding presented) it overrides all other padding values
else if (android:paddingXXX presented) it overrides bg drawable paddingXXX (XXX = Left|Right|Top|Bottom)
else if (view has drawable bg) padding values from this drawable (nine-patch in your case) will be used
else default padding will be applied (zero usually)
So do no use android:padding="2dp". padding property overrides everything. Just use paddingLeft = 20dp, paddingTop = 2dp, paddingRight = 2dp, paddingBottom = 2dp.
Or you can set paddingLeft = 20dp and other padding values will be taken from bg drawable.

Android TextView padding between lines

I have a TextView which displays a long text. I want to give some space between lines like in CSS with line-height property. How can I do it?
You can use lineSpacingExtra and lineSpacingMultiplier in your XML file.
If you want padding between text, try LineSpacingExtra="10sp"
<TextView
android:layout_width="match_parent"
android:layout_height="180dp"
android:lineSpacingExtra="10sp"/>
you can look into android:lineSpacingExtra and apply it to your XML
Additional Info is on this page
or the related method public void setLineSpacing (float add, float mult)
Additional Info here
This supplemental answer shows the effect of changing the line spacing.
You can set the multiplier and/or extra spacing with
textView.setLineSpacing(float add, float mult)
Or you can get the values with
int lineHeight = textView.getLineHeight();
float add = tvSampleText.getLineSpacingExtra(); // API 16+
float mult = tvSampleText.getLineSpacingMultiplier(); // API 16+
where the formula is
lineHeight = fontMetricsLineHeight * mult + add
The default multiplier is 1 and the default extra spacing is 0.
Adding android:lineSpacingMultiplier="0.8" can make the line spacing to 80%.
You can use TextView.setLineSpacing(n,m) function.
You can either use lineSpacingExtra or lineSpacingMultiplier in your XML file.
lineSpacingExtra add extra spacing between lines of text of TextView
<TextView
android:lineSpacingExtra="4dp" />
lineSpacingMultiplier works as scale factor for height of line space:
<TextView
android:lineSpacingMultiplier="0.8" />
In other words, each line height will be height * multiplier + extra.
You can use 2 attrs
1. lineSpacingExtra:
it use for dp spacing
android:lineSpacingExtra="4dp"
2. lineSpacingMultiplie:
it use for relative scale
android:lineSpacingMultiplier="0.8"
As an extended answer to above solutions
Remember you can add <item name="android:lineHeight">16sp</item> for directly setting the line heights but as per the docs above line works like this -
Explicit height between lines of text. If set, this will override the values set for lineSpacingExtra and lineSpacingMultiplier.
<attr name="lineHeight" format="dimension" />
So be sure to use either lineSpacingExtra & lineSpacingMultiplier or lineHeight and not both.
As of 16/11/2021,
I use this line to increase the line space height:
android:lineHeight="25dp"
For me the other answers weren't helpful because maybe they updated the attribute and quite a lot of stuff changed in this version.

Categories

Resources