The ideal is to do as colleague @ramaral suggested and use AppCompatButton, since it is part of an official library, tested, debugged etc. But for anyone who has any restrictions on using Support libraries, a simple solution is to create a Custom View that inherits from Button and adds the behavior you want. An example of what the Custom View code looks like:
package com.minhaempresa.ui.customview;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.Button;
public class AllCapsButton extends Button {
public AllCapsButton(Context context) {
super(context);
}
public AllCapsButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AllCapsButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void setText(CharSequence text, BufferType type) {
super.setText(text.toString().toUpperCase(), type);
}
}
With this, it is enough, in XML, instead of Button, to use com.minhaempresa.ui.customview.AllCapsButton
. For example:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.minhaempresa.ui.customview.MainActivity">
<com.minhaempresa.ui.customview.AllCapsButton
android:id="@+id/botao"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Texto que era minúsculo"/>
</RelativeLayout>
Result:
Tested on Android 2.3.3 and 5.0