Tuhin
Tuhin

Reputation: 27

Can I universally change a Colour of a specific string?

I have created a real time firebase database whose one of the child is "availability". While populating all the firebase data in listview, I want the text from the child to be coloured in Green if the string value of "availability" is "yes" else in Red. Is it possible? If yes, please show me the way. (I have populate the child in various activities, it will be really helpful if I could state the condition universally) Thank You.

Upvotes: 0

Views: 52

Answers (1)

akshay_shahane
akshay_shahane

Reputation: 4633

yes possible use below class You have to create custom Textview like this

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.TextView;

public class CustomTextViewTest extends android.support.v7.widget.AppCompatTextView {
    public CustomTextViewTest(Context context) {
        super(context);
    }

    public CustomTextViewTest(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomTextViewTest(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        super.setText(text, type);

        // here set color according to text 
        if (text.toString().contains("availability")) {
            this.setTextColor(Your Color);
        } else {
            this.setTextColor(Your Color);
        }
    }
}

Upvotes: 2

Related Questions