4

I'm trying to color a word (house) as long as user types "house" in the edittext. This is what I've done:

if (textA.getText().toString().equals("house")){
    String name = String.valueOf(textA.getText().toString().equals("house"));
    name.setTextColor(Color.parseColor("#bdbdbd"));
}

my xml is as follows

<LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="15dp"
                    android:layout_marginEnd="15dp"
                    android:orientation="vertical"
                    android:padding="5dp">


                    <EditText
                        android:id="@+id/textA"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="5dp"
                        android:inputType="textMultiLine"
                        android:maxLines ="4"
                        android:maxLength ="2000"
                        android:textColorHint="@color/grey"
                        android:background="@android:color/transparent"
                        android:gravity="top"/>

                </LinearLayout>

however this causes the app to crash. Dont know what it is I'm doing wrong. I'm new to android. Any advice please?

2
  • What does the LogCat say? What is the crash error? Commented Aug 13, 2017 at 2:27
  • You should get compile time error for name.setTextColor as name is string(local var) Commented Aug 13, 2017 at 17:49

3 Answers 3

1

this works for me

 textA.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String str="home";
            Spannable spannable =textA.getText();
            if(s.length() != 0) {
                textA.setTextColor(Color.parseColor("#0000FF"));
                ForegroundColorSpan fcs = new ForegroundColorSpan( Color.GREEN);
                String s1 = s.toString();
                int in=0; // start searching from 0 index

// keeps on searching unless there is no more function string found
                while ((in = s1.indexOf(str,in)) >= 0) {
                    spannable.setSpan(
                            fcs,
                            in,
                            in + str.length(),
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    // update the index for next search with length
                    in += str.length();
                }
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
Sign up to request clarification or add additional context in comments.

12 Comments

works but color change applies to all text. how can we make this apply to only "home" whiles the other colors remain normal?
means in edittext only "home" word should contain different colour?
exactly. cant seem to figure this out
wait, will let you know with answer
you sir are a genius! One last question then this will be perfect, what if I want to apply the highlight to multiple text: "home", "office", "chair"
|
1

You have to write code like below

 textA.addTextChangedListener(new TextWatcher() { @Override public void afterTextChanged(Editable s) {} @Override public void
 beforeTextChanged(CharSequence s, int start, int count, int after) { }
 @Override public void onTextChanged(CharSequence s, int start, int
 before, int count) {
      if(s.length() != 0 && s.contains("home"))  name.setTextColor(Color.parseColor("#bdbdbd"));
    });

3 Comments

It may be syntax errors because i post it from mobile.
can you help me out please?
cannot resolve s; cannot resolve setTextColor; cannot resolve parseColor
0

equals return values is boolean. so you have to change it.

public boolean equals(Object anObject)

  if (textA.getText().toString().equals("house")){
      textA.setTextColor(Color.parseColor("#bdbdbd"));
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.