androidandroid-edittextandroid-background

How is the line under the text of an EditText created?


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.

enter image description here


Solution

  • 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