43

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?

jonhobbs
  • 26,684
  • 35
  • 115
  • 170

8 Answers8

107

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.

Crawler
  • 1,988
  • 3
  • 21
  • 42
Cheryl Simon
  • 46,552
  • 15
  • 93
  • 82
9
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>
richq
  • 55,548
  • 20
  • 150
  • 144
ashish
  • 91
  • 1
  • 1
6

Programmatically:

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

In xml:

<Button
    android:textSize="10sp"
/>
BRap
  • 529
  • 2
  • 10
  • 29
2

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"/>
Jock Mahon
  • 116
  • 1
  • 9
2

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++);
                }
            });
Iura
  • 95
  • 4
1

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.

Umut D.
  • 1,746
  • 23
  • 24
0
<resource>
<style name="button">
<item name="android:textSize">15dp</item>
</style>
<resource>
sunny
  • 21
  • 1
0

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"