Hello good morning I have a question with the SpannableString class of android, I am very new with this class and also my knowledge related to when something works for an api or not or what api should choose are limited.
I have this piece of code in my onCreate () method:
String texto = "Texto para usar";
Spannable timeSpannable = new SpannableString(texto.toUpperCase());
boton.setText(timeSpannable);
This does not work, to make sure I have given another use to the Spannable class and I have tried this method that I found in this same forum in English:
public static SpannableString highlight(String s, int k) {
SpannableString ss = new SpannableString(s);
ss.setSpan(new ForegroundColorSpan(Color.BLUE), 0, k, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
return ss;
}
Then I'll call it on onCreate () so that my program code looks like this:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout.LayoutParams layoutparams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
Button boton = (Button) findViewById(R.id.ir);
boton.setText("Texto para usar");
boton.setLayoutParams(layoutparams);
boton.setText(highlight(boton.getText().toString(),3));
}
That does not work either, my question is: is there something I'm doing wrong? Is there any kind of restriction when using the Spannable class in any specific API, and how should I modify my code to make it work?
The minimum api of my project is 15: iceCreamSandwich.
The api of my mobile phone is the 22nd one where I test the application.