By default android.widget.EditText has a line under its text. How exactly is that technically done? I checked the source code of the EditText class, but couldn't find where this is done?
I think to know that the line is technically a background, but where is this background set? And especially, as it's just a background, how does Android ensure that this line is drawn below the text and not behind it? Answers with proofs / links to source highly appreciated.
yes, this underline is a background, which is probably ColorStateList (e.g. it changes color when pressed), but if you run your code on Android 4.x or older (pre Material Design) then this background is fixed bitmap/drawable
EditText have an android:background attribute, but it is also present for all other Views. so it is set inside source code of View, not extending EditText. and pointer to the proper drawable of background is set by styling/theming (in HERE you have some example)
EditText is using TextPaint (getPaint method) which may be used to add some extra spacing between/over/under/next to letters to ensure not-overlaying. also drawable may have padding attribute - may draw line in last row of pixels but set bottom padding to 1px and that ensures that content of this View (in this case EditText) will not be drawn under/over padding (but drawable does) - sooner View will expand a bit if have wrap_content for height
sorry for not-so-precise answer, topic needs more inwestigation and searching in sources, but maybe my points will help you find answers by yourself
Related
I've got code that iterates over views, after inflation, so I can hotswap certain colors.
The problem I'm facing at the moment is reading the current color used by, for example, a AppCompatButton runtime. I know that it's being colored by my AccentColor in some cases, but I don't know where to find that color programmatically on the View.
I've been using reflection and checking the private fields of the RippleDrawable (AppCompatButton.Background) and its children. But I'm coming up blank. I can't find the color and ColorStateLists are empty, Tints are null, Paint objects have White as the current color, etc.
I've been going over the Android source code on GitHub but I haven't found the solution yet, any help would be appreciated.
To clarify, I know what colors are being used, but I need to check if a specific AppCompatButton is using a specific color or not.
Update 1
It appears that the following two xml defines the AppCompatButton:
btn_default_material.xml
btn_default_mtrl_shape.xml
The second xml defines a shape with a android:tint="?attr/colorButtonNormal", that would be the GradientDrawable I was trying to extract information from.
I'll give it another go tomorrow and see if I can't extract the tint from the GradientDrawable...
I found the color, in my specific case and Android OS version. It was mColor, inside mTintFilter, inside the GradientDrawable.
Hierarchy: AppCompatButton.Background.GetDrawable(0).Drawable.mTintFilter.mColor
I've searched SO for a while but couldn't find an answer to my exact question. I usually found 2 similar problems that do not suit my needs:
Many people asked for how to draw a border around a view, and the solution is often to use a shape with a stroke as the background of the view. But this uses a static background.
I can't use this (or don't know how in my case), because I'm creating a color picker and I draw the background programmatically.
Other people asked how to draw a border programmatically, but that's only a fallback option for me.
One solution obviously came to my mind: wrapping my view with dynamic background in another view with the stroke. But I don't like this one as it unnecessarily complicates the layout, and could have an impact on performance too. I'd rather draw the border programmatically with the background than use nested views.
Hence my question:
Is there a clean (androidic) way to set the border of a view in the XML when the background is set programmatically (without using nested views)?
If not, I guess I'll just go for a dynamic stroke.
You can create your own view which does the same as your current view but with an added border.
I haven't tried this yet so I apologize for the lack of information but I believe this should be possible.
So I know there's a way to style the setError font color (and a lot of questions / answers about how to do this) using a spannableString but it seems that it's impossible to change the actual background without extending the EditText widget?
I know right now that it's a background image, but I have a replacement drawable for the background I'd like to use.
If it's the case where you have to override, can someone recommend a library to do it?
I'm just starting to learn android and i had a doubt concerning to view padding. Referring to What controls the default padding between views? , Is the padding included in the size we specify to the view ? All help appreciated!
EDIT: Also does a layout add padding to itself by default ?
In the question you linked to, the padding is caused by the button's background being a nine-patch and having some transparent borders in it. Since the "padding" is part of the background resource, it is include in the image size and you don't need to do anything to make it show up.
However, if you are creating your own Views, or changing the background or foreground resources of the system provided Widgets, you may want to use the android:padding* attributes to specify the various paddings.
In Android 4.0 is there a way to change the colour of the blue lines above/below the EditText in the stock NumberPicker Widget? I have not found a way to change the colour in the xml and am wondering if anyone else has found a way to do this without creating a completely custom widget. Thanks.
I can't see any blue line in the current NumberPicker, though it is a layout and the line is probably an ImageView on fixed indexes i1,i2,... ,therefore you can use
View v = picker.getParent(i);
and modify the returned view or alternatively remove it completely.