Tommy
Tommy

Reputation: 759

textview only display capital letters

I have a button which when clicked jumbles a word and puts the word into a textview.

I want the textview to only display capital letters.

How can I do this?

Thanks.

Upvotes: 1

Views: 4920

Answers (3)

Deepak Goel
Deepak Goel

Reputation: 5684

Use android:textAllCaps:true it will work like a charm

 <TextView
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight="3"
    android:text="@string/app_name"
    android:textAllCaps="true"
    android:layout_gravity="center_vertical"
   android:textStyle="bold"
    android:typeface="sans" />

In Java code use

   text.toUpperCase() 

Upvotes: 8

debracey
debracey

Reputation: 6597

If you mean you want the TextView to capitalize everything that is put into it, then you can set the capitalize XML attribute on the TextView.

If you mean you only want capital letters to show up ex:

"Hi There!" --> "HT"

Then you need a little bit of Java code (from memory, untested):

private static String removeLowercase(String input)
{
   if(input == null)
     return null;

   String retVal = "";

   for(int i=0; i < input.length(); i++)
   {
        char c = input.charAt(i);

        if(Character.isUpperCase(c))
            retVal += c;
   }

   return retVal;
}

You can then set the text in the TextView:

myTV.setText(removeLowercase("SomeInput"));

Upvotes: -2

Greg Zimmers
Greg Zimmers

Reputation: 1400

This will convert the text to all uppercase

text.toUpperCase() 

Upvotes: 2

Related Questions