Set color of TextView span in Android

前端 未结 15 1803
眼角桃花
眼角桃花 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

    Set Color on Text by passing String and color:

    private String getColoredSpanned(String text, String color) {
      String input = "" + text + "";
      return input;
    }
    

    Set text on TextView / Button / EditText etc by calling below code:

    TextView:

    TextView txtView = (TextView)findViewById(R.id.txtView);
    

    Get Colored String:

    String name = getColoredSpanned("Hiren", "#800000");
    

    Set Text on TextView:

    txtView.setText(Html.fromHtml(name));
    

    Done

提交回复
热议问题