0

I'm making an app in which I there is an EditText and a ToggleButton. I want to create a highlighter mechanism with the help of these. For example, when the state of the ToggleButton is ON, the text that'll be entered in the EditText will have Green background as if it is being highlighted. Again, when the ToggleButton state will be changed to OFF, the text in the EditText will also stop getting highlighted immediately.

Please Help Me.

Thanks in Advance!!

Swap
  • 480
  • 2
  • 7
  • 21

3 Answers3

1

You can change the background color of part of the text using SpannableString as described here: Set color of TextView span in Android

Another way of doing the same thing is to load the text from HTML with the tag set to the background color you want.

Community
  • 1
  • 1
atok
  • 5,880
  • 3
  • 33
  • 62
0

To change the colour of text use

textElement.setTextColor(0xFF00FF00); //this is green colour

To change the colour of edit text use

textElement.setBackgroundColor(Color.MAGENTA);

For more information Check this link.

Zohra Khan
  • 5,182
  • 4
  • 25
  • 34
0

Simply you set onCheckedChangeListener on your ToggleButton to change the background of your EditText. Your code will be something like this :

toggleButtonHighLight.setOnCheckedChangeListener(new OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(CompoundButton button, boolean isChecked) {
        if (checked) {
            //highlight the EditText by setting its background to green
            editText.setBackgroundResource(R.color.green);
        } else {
            //delete the highlight when the toggleButton is OFF
            editText.setBackgroundColor(android.R.color.white);
        }
    }
});
Houcine
  • 24,001
  • 13
  • 56
  • 83
  • I tried your suggestion but unfortunately, the app crashes with a java.lang.NullPointerException !!! – Swap Apr 09 '14 at 18:25
  • it's because you are not getting the edittext from your xml, try `EditText editText = (EditText) findViewById(R.id.yourEditTextId);`. if you want more help , you should share more code and the more logs about the stack trace of the exceptions you got . i can't figure out the solution magically like that without having any idea about what's going on in your code – Houcine Apr 09 '14 at 22:04