0

Is there any way to set Button text to capital letter for the first character and all text is cap?

Layout:

<Button
    android:id="@+id/q1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="13sp"
    android:textColor="#ffffff"
    android:background="@drawable/ic_btn_q_normal" />

Activity:

final Button q1 = (Button) findViewById(R.id.q1);
q1.setText(answers[numbers.get(0)]);
    q1.setOnClickListener(new OnClickListener() {
        public void onClick(View v){

        q1.setBackgroundResource(R.drawable.ic_btn_q_right);

    }
});

answers[numbers.get(0)] is the text that I get from array list.

I have tried with q1.setAllCaps(true); but it's doesn't work for me.

Thanks.

user1731690
  • 225
  • 1
  • 3
  • 16

2 Answers2

2

You can use: WordUtils

method:

capitalize(String str):

Capitalizes all the whitespace separated words in a String.

or: capitalizeFully(String str)

Converts all the whitespace separated words in a String into capitalized words, that is each word is made up of a titlecase character and then a series of lowercase characters.

Emil Adz
  • 40,709
  • 36
  • 140
  • 187
1
final Button q1 = (Button) findViewById(R.id.q1);
String label = answers[numbers.get(0)];
StringBuilder sb = new StringBuilder();
sb.append( label .substring(0,1) );
sb.append( label .substring(1).toLowerCase() );
label = sb.toString();

q1.setText(label);
    q1.setOnClickListener(new OnClickListener() {
        public void onClick(View v){

        q1.setBackgroundResource(R.drawable.ic_btn_q_right);

    }
});

Code for string conversion taken from: What is the simplest way to convert a Java string from all caps (words separated by underscores) to CamelCase (no word separators)?

Community
  • 1
  • 1
dannrob
  • 1,061
  • 9
  • 10