High-box text button in Android API < 14

Asked

Viewed 604 times

4

We see that at the launch of Android 5.0 Lolipop came Material Design that has indeed changed a lot on Android and along with this, the component of Dialogs which by default comes with the high text box button as shown below in the image:

inserir a descrição da imagem aqui

But I have an app with Android 2.3.3 Gingerbrend, that I would like to customize to stay close to the material. Then I fell in this question of the text in high box (UPPERCASE). I saw that there is the android:textAllCaps which in this case may be used only from API 14. When I entered this property to my button appeared the following alert:

Attribute "textAllCaps" is only used in API level 14 and Higher (Current min is 10)

Is there any method that replaces this property?

3 answers

4


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:

Captura de tela mostrando um celular rodando Android 2.3.3 exibindo uma aplicação que só tem um botão, no qual está escrito, em letras maiúsculas, "TEXTO QUE ERA MINÚSCULO"

Tested on Android 2.3.3 and 5.0

  • Blz, that’s how I did it this morning! Hugs.

  • I had thought to override the getText() but the setText() also serves.

  • 1

    @ramaral I had done first using gettext() then changed.

  • 1

    @ramaral Living, catching and learning! Thank you for sharing your experiences.

  • @In the tests I did, I did not get the result with the getText, then I went to setText 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. :/

  • I tested and worked, both in xml and java. return super.getText().toString().toUpperCase();

  • In which version? You took references to the theme AppCompat in the Styles.xml Apptheme? With the theme inheriting from AppCompat, he was leaving in capital letters right here doing nothing.

  • You’re right, the way I tested it doesn’t guarantee because it was on API23.

  • 1

    @ramaral Ah, blz. :)

  • @Pabloalmeida I just made a small change, inside the view. I created a function setTextAllCaps(String str), not to touch all the setText... you’re working beauty.

  • @Cleidimarviana I don’t understand. Where do you call this one setTextAllCaps?

  • @Pabloalmeida there in the public void setText default where you call the super.setText , i ended up not using. I created a new called method setTextAllCaps where I pass the same configuration text.toString().toUpperCase(). It’s working beauty, relax! =)

  • @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).

Show 8 more comments

3

Use the Appcompatbutton.

Appendage

xmlns:app="http://schemas.android.com/apk/res-auto"

to the root layout

and declare the button so:

<android.support.v7.widget.AppCompatButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:textAllCaps="true"
    android:text="uppercase"/>
  • In this case I would need the library android.support.v7 that today we do not use (yet). This app that I’m working on was finalized in early 2013, so everything still adapted to api 10, no material, no pattern, all 90’s style, style those fonts of word 2003 you know?! (hehe) Would you have some other way or I’ll have to download the library?

  • Where are the button texts? If they are in the button declaration, adding the attribute or changing the text by hand to uppercase is the same job. If they are in a "Resource" the job of changing them is even less.

  • is an app for internal use company, and in this project I arrived and caught the streetcar walking, and walking for a long time, however I’ve even suggested to us to make an improvement, considering that today the amount that uses Android 2.3.3 is minimal. I even created a service to get device information at the time of installation, but it hasn’t been for production yet. With this we will have a usage statistics, so we can make some improvement decision.

  • Are in the resource. English, Portuguese and Chinese. We will even make available to Africa, but will continue using as English there.

  • I don’t know any other alternative, or urge her to library or change by hand.

  • 1

    By hand it is already, but I didn’t want to stay that way because sometimes I use the same key of the string in other situations, so it’s not cool. But I’m going to look at the use of the library here, in case people don’t agree, I create several strings with uppercase and without. But thanks for the help.

  • 1

    @Cleidimarviana You can create a Customview that extends Button, and does this job of leaving in high box when displaying.

  • 1

    @Pabloalmeida Good idea because it is easy to implement. Put an answer.

  • 1

    @I’ll put it down as soon as I have a few minutes left here.

  • @Pabloalmeida your idea was nice, I have implemented it! If you want to insert the answer here, I will give you the merits. But if you don’t have much time, I’ll post the answer because it might help someone who needs.

  • @Cleidimarviana Beauty. If I don’t have time by tonight, I give you OK here.

  • @Pabloalmeida Ok.

  • @Cleidimarviana I wrote a response with the strategy I suggested. See if it looked like your solution or if you need some improvement. :)

  • 1

    @ramaral has to remember that we have to put API support in dependencies compile 'com.android.support:appcompat-v7:25.0.0'

Show 9 more comments

0

I recommend you use the AppCompatDialogFragment to do this!

This class is available within the appcompatv7, and will already do all the work of using the correct theme for your application.

To use, simply create a class you inherit from AppCompatDialogFragment:

public class ConfirmDialogFragment extends AppCompatDialogFragment {
    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog dialog = new AlertDialog.Builder(getActivity())
            .setTitle(title)
            .setMessage(message)
            .setPositiveButton(R.string.alguma_string,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        //Fazer algo
                    }
                }
            )
            .setNegativeButton(R.string.outra_string, 
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        //Fazer algo
                    }
                }
            )
            .create();

        return dialog;
    }
}

And, to show off:

ConfirmDialogFragment dialog = new ConfirmDialogFragment();
dialog.show(getSupportFragmentManager(), dialog.getClass().getName());

Refs:

https://developer.android.com/reference/android/support/v7/app/AppCompatDialogFragment.html

  • Nice guy, but I wanted to do it without using the lib appcompatv7. Our @ramaral comment gave a cool idea to solve the problem. Thanks for the help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.