0

I want the hint size of an EditText to be smaller than the real text.

At present I know two solutions:

  1. using Html.fromHtml() programmatically, or

  2. using <font size=""> in the XML.

However, I don't want to use the first solution because I want the hint to be directly written in my layout XML, while, regarding the second solution, I don't like using the font element because (if I'm not wrong) I cannot explicit the dp measure. Furthermore, the font element is deprecated in HTML5, so it belongs to the "old-style".

I've tried to use the span element with the font-size attribute (from CSS) in my XML, but it doesn't work. Is there any other up-to-date solution?

redcrow
  • 1,743
  • 3
  • 25
  • 45

1 Answers1

1

I am not aware of any way in which you can accomplish this through xml only (apart from sticking html tags in the hint text).

But there is another way that was not outlined above, that would allow you to keep the font size in sp and separate from the java code:

final int hintSize = <read_this_from_xml_resources_but_take_into_account_density>;
final int textSize = <read_this_from_xml_resources_but_take_into_account_density>;
editText.addTextChangedListener(new TextWatcher(){
    @Override
    public void afterTextChanged(Editable arg0) {
    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
    }

    @Override
    public void onTextChanged(CharSequence arg0, int start, int before, int count) {
        editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, arg0.length() > 0 ? textSize : hintSize);
    }
});
N.T.
  • 2,601
  • 1
  • 14
  • 20
  • What do you mean with "(apart from sticking html tags in the hint text)"? I've tried including ``html`` and ``span`` element in my string, but nothing changes. – redcrow Aug 05 '15 at 08:13
  • I meant something like: [http://stackoverflow.com/questions/3235131/set-textview-text-from-html-formatted-string-resource-in-xml](http://stackoverflow.com/questions/3235131/set-textview-text-from-html-formatted-string-resource-in-xml) - check more than just the accepted answer. It doesn't say anything about font size, but some other html tags. but that is what I meant. – N.T. Aug 05 '15 at 10:50
  • Yes, but in that case you need to also use the ``Html.fromHtml()`` method (as I wrote in my question post). If I'm not wrong, you cannot use just HTML tags in your XML string without using a Java function that reads the markup, right? – redcrow Aug 06 '15 at 13:35
  • No, it depends on your string. Even in Java code, you don't have to call `fromHtml` if you don't have any escaped characters (see difference between `getString()` vs. `getText()`). So if you put something like "Hello World" in your resources, you could use it from xml (`@string/hello_world`). – N.T. Aug 06 '15 at 14:35
  • Yes, I never used it this way, to be honest. I prefer styling it Android's way (dimensions, styles and themes) to get a consistent look and feel - which is why I suggested the chunk of code from my answer. – N.T. Aug 06 '15 at 14:50