jonhobbs
jonhobbs

Reputation: 27962

Android button font size

I;ve been trying to create a custom button in android using this tutorial - http://www.gersic.com/blog.php?id=56

It works well but it doesn't say how to change the font size or weighting. Any ideas?

There was another question on here and the only answer was to use html styling but you can't change a font size in html without using css (or the deprecated font tag). There must be a better way of setting the pixel size of the font used on buttons?

Upvotes: 44

Views: 133676

Answers (8)

ANUSHKA GUPTA
ANUSHKA GUPTA

Reputation: 1

In XML file, the font size of ant text including the button, textView, editText and others can be changed by the adding

android:textSize="10sp"

Upvotes: 0

Iura
Iura

Reputation: 95

Another approach:

declaring an int first with the default fontsize

int txtSize = 16;

           button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mTextView.setTextSize(txtSize++);
                }
            });

Upvotes: 2

Umut D.
Umut D.

Reputation: 1846

Another programmatically approach;

final Button btn = (Button) findViewById(R.id.btnSize);

        final float[] size = {12};

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                size[0] +=2;
                btn.setTextSize(size[0] +2);
            }
        });

Every time you click your button, Button text will be change (+2px size). You can add another button and change size -2px too. If you want to save size for another openings, you may use Shared Preference interface.

Upvotes: 1

Cheryl Simon
Cheryl Simon

Reputation: 46844

You define these attributes in xml as you would anything else, for example:

<Button android:id="@+id/next_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/next"
            android:background="@drawable/mybutton_background"
            android:textSize="10sp" /> <!-- Use SP(Scale Independent Pixel) -->

You can find the allowed attributes in the api.

Or, if you want this to apply to all buttons in your application, create a style. See the Styles and Themes development documentation.

Upvotes: 107

sunny
sunny

Reputation: 21

<resource>
<style name="button">
<item name="android:textSize">15dp</item>
</style>
<resource>

Upvotes: 0

Jock Mahon
Jock Mahon

Reputation: 126

I tried to put the font size in the styles.xml but when i went to use it it was only allowing resources from the dimen folder so put it in there instead, dont know it this is right

        <Button
                android:layout_weight="1"
                android:id="@+id/three_btn"
                android:layout_height="match_parent"
                android:layout_width="0dp"
                android:onClick="onButtonClick"
                android:textColor="#EEEEEE"
                android:textStyle="bold"
                android:textSize="@dimen/buttonFontSize"
                android:text="3"/>

Upvotes: 2

BRap
BRap

Reputation: 625

Programmatically:

Button bt = new Button(this);
bt.setTextSize(12);

In xml:

<Button
    android:textSize="10sp"
/>

Upvotes: 7

ashish
ashish

Reputation: 91

Button butt= new Button(_context);
butt.setTextAppearance(_context, R.style.ButtonFontStyle);

and in res/values/style.xml

<resources>   
    <style name="ButtonFontStyle">
            <item name="android:textSize">12sp</item>
    </style>
</resources>

Upvotes: 9

Related Questions