The ideal is to do as the @ramaral colleague suggested and use Appcompatbutton, since it is part of an official library, tested, debugged etc. But, for those who have some restriction on the use of libraries Support, a simple solution is to create a Custom View that inherits from Button and adds the behavior you want. An example of what the code of Custom View:
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, simply, in XML, instead of Button, 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>
Upshot:
Tested on Android 2.3.3 and 5.0
Blz, that’s how I did it this morning! Hugs.
– viana
I had thought to override the
getText()
but thesetText()
also serves.– ramaral
@ramaral I had done first using gettext() then changed.
– viana
@ramaral Living, catching and learning! Thank you for sharing your experiences.
– viana
@In the tests I did, I did not get the result with the
getText
, then I went tosetText
and it worked. It doesn’t seem right to change the real value that’s there just to display differently, but it was the way. :/– Pablo Almeida
I tested and worked, both in xml and java.
return super.getText().toString().toUpperCase();
– ramaral
In which version? You took references to the theme
AppCompat
in the Styles.xml Apptheme? With the theme inheriting fromAppCompat
, he was leaving in capital letters right here doing nothing.– Pablo Almeida
You’re right, the way I tested it doesn’t guarantee because it was on API23.
– ramaral
@ramaral Ah, blz. :)
– Pablo Almeida
@Pabloalmeida I just made a small change, inside the view. I created a function
setTextAllCaps(String str)
, not to touch all thesetText
... you’re working beauty.– viana
@Cleidimarviana I don’t understand. Where do you call this one
setTextAllCaps
?– Pablo Almeida
@Pabloalmeida there in the
public void setText
default where you call thesuper.setText
, i ended up not using. I created a new called methodsetTextAllCaps
where I pass the same configurationtext.toString().toUpperCase()
. It’s working beauty, relax! =)– viana
@Cleidimarvian OK. It’s just that I thought maybe you’d made the same mistake I did in the first solution I tried, which was to create a method similar to yours and call it in all the builders instead of overriding the
setText
. This works for the text set via XML, but if you do.setText in Java, it no longer works (because the button has already been built and the constructor is no longer called).– Pablo Almeida