221

I have a trivial question that has been bothering me for a while. I tried to google this but no one seems to have the same problem as me or doesn't see it as an issue. When I make a button in activity_my.xml under layout

 <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_1_name"
    android:id="@+id/button2"
    android:layout_marginTop="140dp"
    android:layout_below="@+id/textView"
    android:layout_centerHorizontal="true" />

I get a button that looks like this][Imgur

even though my strings code is:

<resources>

<string name="app_name">HelloWorld</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="button_1_name">BuTtOn 1</string>

I know I am definitely missing something small here, but how do I get lower case/upper case working in the button text?

Thanks!

Alex
  • 3,441
  • 4
  • 18
  • 30

12 Answers12

550

You could add android:textAllCaps="false" to the button.

The button text might be transformed to uppercase by your app's theme that applies to all buttons. Check themes / styles files for setting the attribute android:textAllCaps.

Stelian Matei
  • 11,553
  • 2
  • 25
  • 29
  • 1
    Thanks for the response! `android:textAllCaps="false"` did work as a workaround but new buttons that are added are still all capitalized. My styles.xml under res/values does not appear to have the aforementioned attribute. Here's what it had ` ` – Alex Jul 22 '14 at 07:03
  • 15
    You are inheriting the theme `Theme.Holo.Light.DarkActionBar`, that contains that setting. You have to add new explicit property for styling buttons, e.g. `` – Stelian Matei Jul 22 '14 at 09:09
  • 1
    After adding this, i am having inflation error with arrayindexoutofboundsexception. – M. Usman Khan Feb 10 '16 at 16:00
  • @usman I doubt that the exception is related to the `textAllCaps` property. Can you provide more details? – Stelian Matei Feb 10 '16 at 16:33
  • @StelianMatei if we use textAllCaps in EditText, it crashes. http://stackoverflow.com/questions/28644616/android-edittext-gives-indexoutofbounds-exception-while-using-textallcaps – M. Usman Khan Feb 11 '16 at 04:13
  • attribute textAllCaps is used only in API Level 14 and higher – Muhammad Riyaz Apr 02 '16 at 07:58
  • 1
    This will not going to work when you are using your own custom class extending Button class. – Er. Joshi Aug 18 '17 at 13:17
32
<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:buttonStyle">@style/Button</item>
</style>

<style name="Button" parent="Widget.AppCompat.Button">
    <item name="android:textAllCaps">false</item>
</style>
Hun
  • 3,652
  • 35
  • 72
10

It can be done easily by using the following code inside your view in activity_main.xml:

android:textAllCaps="false"

In your case it would be as follows:

 <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_1_name"
android:id="@+id/button2"
android:layout_marginTop="140dp"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:textAllCaps="false" />

You can also change it for all the buttons in your app by adding this to your styles/themes.xml:

<style name="AppTheme" parent="AppBaseTheme">
<item name="android:buttonStyle">@style/Button</item>
</style>

<style name="Button" parent="Widget.AppCompat.Button">
<item name="android:textAllCaps">false</item>
</style>

It can also be set Programatically by using the following in mainActivity.kt or mainActivity.java:

Button btn = (Button) findViewById(R.id.button2);
btn.setAllCaps(false);
Shaurya Goyal
  • 169
  • 1
  • 11
9

This is fixable in the application code by setting the button's TransformationMethod null, e.g.

mButton.setTransformationMethod(null);
Attaullah
  • 3,856
  • 3
  • 48
  • 63
8

add android:textAllCaps="false" in xml button It's true with this issue.

Alex
  • 8,461
  • 6
  • 37
  • 49
Hai Rom
  • 1,751
  • 16
  • 9
  • 1
    This is just repeating [the accepted answer](http://stackoverflow.com/a/24880547/1402846). – Pang Dec 19 '15 at 09:13
  • I posted a nifty littly answer here: http://stackoverflow.com/questions/26958909/why-is-my-button-text-forced-to-all-caps-on-lollipop/43711891#43711891 – user2288580 Apr 30 '17 at 22:15
4

there are 3 ways to do it.

1.Add the following line on style.xml to change entire application

<item name="android:textAllCaps">false</item>

2.Use

android:textAllCaps="false"

in your layout-v21

mButton.setTransformationMethod(null);
  1. add this line under the element(button or edit text) in xml

android:textAllCaps="false"

regards

Dinithe Pieris
  • 1,822
  • 3
  • 32
  • 44
3

You could set like this

button.setAllCaps(false);

programmatically

missionMan
  • 873
  • 8
  • 21
3

In your .xml file within Button add this line--

android:textAllCaps="false"
Pawan Lakhotia
  • 385
  • 1
  • 10
  • 1
    What's the difference between your answer and the [Stelian Matei's](https://stackoverflow.com/a/24880547/4744263) answer? Please, don't repeat the accepted answer. – Filipe Brito Feb 08 '18 at 19:38
3

There is a property in <Button> that is android:textAllCaps="false" that make characters in which you want in your own Small and caps. By Default its became True so write this code and make textAllCaps=false then you can write text on button in small and Caps letter as per your requirement. Complete code for a Button which allow use to write letters as per our requirement.

 <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/btnLogin"
            android:text="Login for Chat"
            android:textAllCaps="false"/>
Pradeep Sheoran
  • 493
  • 6
  • 15
2

in XML Code
add this line android:textAllCaps="false" like bellow code

 <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_1_name"
        android:id="@+id/button2"
        android:layout_marginTop="140dp"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true"
       ** android:textAllCaps="false" ** />

or

in Java code (programmatically)
add this line to your button setAllCaps(false)

Button btn = (Button) findViewById(R.id.button2);
btn.setAllCaps(false);
Vasinda
  • 83
  • 6
2

I have faced this issue in TextView. I have tried

android:textAllCaps="false", 
mbutton.setAllCaps(false);   
<item name="android:textAllCaps">false</item> 

none of that worked for me. Finally I fed up and I have hard coded text, it is working.

tv.setText("Mytext");

tv is object of TextView. But as per coding standards, it is bad practice.

Thirumalvalavan
  • 2,660
  • 1
  • 30
  • 32
1

in XML: android:textAllCaps="false"

Programmatically:

mbutton.setAllCaps(false);