Centering text in a button?

by CyberQat » Wed, 11 Nov 2009 03:42:30 GMT

Sponsored Links
 Hi Guys,

Ive just started fooling around with programming my Motorola Droid.
I'm programmatic ally laying out an interface such that the text
contained in my Button is sized at layout time to fit the button.  My
questions are:

(1) I know the pixel size of the button, what is the best way to set
the text size to fit the button size?
(2) I can't seem to get it to center.  I've tried a Gravity of 11 and
that helps, but ist still not right.  How do I get my text to center
itself properly horizontally and vertically in the button?



Centering text in a button?

by CyberQat » Thu, 12 Nov 2009 14:19:22 GMT

 Some more info

The text is perfectly centered (but tiny) if I lave it at the default
setting.  If I try to scale it
up to match the button size however I lose vertical centering :(

Ive tried looking at the drawing code but it is massively complex.

Anyone know a soln?


Sponsored Links

Other Threads

1. Dialog (built using AlertDialog.Builder) not complete when showing dialog in runOnUiThread

I have a game activity in which i have a dialog which is created in
the onCreateDialog method of the activity:

        case VICTORY_DIALOG:

                                String[] items={"Next Level", "Play Again", 

                                AlertDialog.Builder builder = new 
                                builder.setMessage("You won!")
                                .setItems(items, new 
DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface 
dialog, int id) {
                                                if(id==0) {
                                                } else if(id==1) {
                                                } else if(id==2) {

When the game ends i open this dialog from the game thread (not the ui
thread) with the following code:

Runnable() {
                                                        public void run() {

However... the dialog is just a small empty rectangle. No title, no
items, nothing!
The buttons (setPositiveButton,setNegativeButton,setNeutralButton) are
working though.

Wat am i doing wrong?

Thanks in advance

2. Accelerometer is cukoo???

I see an acceleration of -9 in the y-direction ( due to gravity, of
course). Now, keeping the phone fixed, not rotating the screen or
anything, I interrupt the app my pressing the call button and then
coming back, I see an acceleration of +9 in the x-direction. This
switching of the coordinates happens without onAccuracyChanged being
called, so how on Earth can we consistently use the accelerometer's

Please shed some light on this. Is this fixed in SDK 1.5?

3. ToggleButton - how to implement iPhone-style ToggleButton?

4. Google Talk Shared Status Messages

5. How to find out screen status: active/dark?

6. How to change a Uri to a file?

7. Using Prebuilt Toolchain with configure and make