Sid
Sid

Reputation: 1274

Different color for upper/lower case alphabets and digits in textView

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!!

Upvotes: 0

Views: 671

Answers (3)

Ranjithkumar
Ranjithkumar

Reputation: 18396

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..

Upvotes: 2

Sayalee Pote
Sayalee Pote

Reputation: 523

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

Upvotes: 3

Guru_VL
Guru_VL

Reputation: 402

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.

Upvotes: 0

Related Questions