0

I have a textView in my app and I am populating it with a one-time-password with upper/lower case alphabets and numeric digits. I have a requirement now to have a different color for all three type of characters to increase readability. Looked around in google but could get much. Can anyone point me to the right approach.Will pick it up from there. Thanks in advance!!

Sid
  • 1,224
  • 3
  • 23
  • 48

3 Answers3

3

Try this:

MainActivity.java

public class MainActivity extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView tv = (TextView) findViewById(R.id.myColoredString);
        String x = tv.getText().toString();
        SpannableStringBuilder builder = new SpannableStringBuilder();

        for(int i = 0; i< x.length();i++){

            //UpperCase : RED
            if(x.charAt(i) >= 65 && x.charAt(i) <= 90) {
                String s = x.charAt(i) + "";
                SpannableString spannableString = new SpannableString(s);
                spannableString.setSpan(new ForegroundColorSpan(Color.RED), 0, s.length(), 0);
                builder.append(spannableString);
            }

            //LowerCase: GREEN
            if(x.charAt(i) >= 97 && x.charAt(i) <= 122) {
                String s = x.charAt(i) + "";
                SpannableString spannableString = new SpannableString(s);
                spannableString.setSpan(new ForegroundColorSpan(Color.GREEN), 0, s.length(), 0);
                builder.append(spannableString);
            }

            //Digits: BLUE
            if(x.charAt(i) >= 48 && x.charAt(i) <= 57) {
                String s = x.charAt(i) + "";
                SpannableString spannableString = new SpannableString(s);
                spannableString.setSpan(new ForegroundColorSpan(Color.BLUE), 0, s.length(), 0);
                builder.append(spannableString);
            }
        }

        tv.setText(builder, TextView.BufferType.SPANNABLE);


    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
    <TextView
       android:id="@+id/myColoredString"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="HeLlo98"
    />
</RelativeLayout>

Refer this link

Community
  • 1
  • 1
Sayalee Pote
  • 511
  • 5
  • 22
2

Try this answer,

 TextView TV = (TextView)findViewById(R.id.tv_onetime_password);
String oneTimePass="43434Qe";
for(int i = 0; i < oneTimePass.length(); i++) {

String myChar=oneTimePass.charAt(i);

 Spannable word = new SpannableString(myChar);

//for digits
 if (Character.isLetter(oneTimePass.charAt(i))){
word.setSpan(new ForegroundColorSpan(Color.WHITE), i,(i+1), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 TV.append(word);

}

//for letters
else{
boolean hasUppercase = !myChar.equals(myChar.toLowerCase());

if(hasUppercase){
 word.setSpan(new ForegroundColorSpan(Color.BLUE), i,(i+1), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 TV.append(word);
}

else {
 word.setSpan(new ForegroundColorSpan(Color.RED), i,(i+1), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 TV.append(word);
}
}       

}

More reference, Single TextView with multiple colored text

Note: Not tested the output..

Community
  • 1
  • 1
Ranjithkumar
  • 16,071
  • 12
  • 120
  • 159
  • I have a similar use case and am confused. Above you mention that "Character.isLetter()..." is // for digits. But isn't Character.isLetter() for a letter not a digit? Did you mean to write "Character.isDigit()"? Also, for uppercase test, rather than using the boolean can Character.isUpperCase() be used? – AJW Apr 25 '21 at 04:28
0

You have mentioned its a password field so it should not show any text we entered. But if you really intended to have different style/color for different commination then you can try

  1. First find out the chars grouped as per your requirement (digits, Uppercase & Lowercase)

  2. Set the color for each group using Html.fromHtml method.

String string = “A uppercase. a Lowercase 1 Numeric.";

You can also try Spannable string.

Guru_VL
  • 402
  • 3
  • 6
  • its not a password field. Its an OTP, which the user has to read and enter somewhere else, so it has to be visible – Sid Feb 16 '16 at 13:08