Set color of TextView span in Android

前端 未结 15 1723
眼角桃花
眼角桃花 2020-11-22 05:54

Is it possible to set the color of just span of text in a TextView?

I would like to do something similar to the Twitter app, in which a part of the text is blue. See

15条回答
  •  爱一瞬间的悲伤
    2020-11-22 06:26

    1. create textview in ur layout
    2. paste this code in ur MainActivity

      TextView textview=(TextView)findViewById(R.id.textviewid);
      Spannable spannable=new SpannableString("Hello my name is sunil");
      spannable.setSpan(new ForegroundColorSpan(Color.BLUE), 0, 5, 
      Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
      textview.setText(spannable);
      //Note:- the 0,5 is the size of colour which u want to give the strring
      //0,5 means it give colour to starting from h and ending with space i.e.(hello), if you want to change size and colour u can easily
      

提交回复
热议问题