2

I use Android Studio. I want to write colorful text like in the photo below in the same EditText. I have researched but I have found nothing. Can anyone help me?

The sample output is below.

photo

This is my code: anaM_LV_BG_text is a list view name_title and content are edittext renk_koduları_string_text is an array that includes html colors

   private void registerClickCallBack_text() {
        anaM_LV_BG_renk_text.setOnItemClickListener(new AdapterView.OnItemClickListener() {


            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(DisplayNote.this, position + 1 + " . seçildi", Toast.LENGTH_SHORT).show();
                name_title.setTextColor(Color.parseColor(renk_koduları_string_text[position]));
                content.setTextColor(Color.parseColor(renk_koduları_string_text[position]));
               SharedPreferences preferences_text = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                SharedPreferences.Editor editor_text = preferences_text.edit();

                String color_text= renk_koduları_string_text[position];
                editor_text.putString("MyColorText",color_text);
                editor_text.commit();

            }
        });
    }
Nompumelelo
  • 929
  • 3
  • 17
  • 28
emowise
  • 121
  • 1
  • 14

2 Answers2

5

Use a SpannableString

EditText editText = (EditText)findViewById(R.id.my_edit_text);

Spannable spannable = new SpannableString("This is a wonderful day");        
spannable.setSpan(new ForegroundColorSpan(Color.BLUE), 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new ForegroundColorSpan(Color.RED), 5, 10, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
editText.setText(spannable);
dumazy
  • 13,857
  • 12
  • 66
  • 113
  • can i use this? – emowise Jan 01 '17 at 15:33
  • i use list view. When i click list view color is changed. i have color_array from xml file. I use setOnItemClickListener and i get text from edittext.getText().toString. can i use this line like below? spannable.setSpan(new ForegroundColorSpan(array_color[position]), 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); – emowise Jan 01 '17 at 15:38
0

All you need to do is create a string with an "html tag" and while you are integrating it in the EditText just use this method: Html.fromHtml().

Example:

String strData =  "<b>Hi</b> <i>I am</i> programmer"</br>
myEditText.setText(Html.fromHtml(strData));
Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121