0

I'm working on an app and I have a series of buttons on the main menu that I want to all have the same text size. Is it possible for me to specify that in a properties file?

Here is my current XML file.

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:orientation="horizontal"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent">
    <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
        <Button
                android:id="@+id/sensors_available"
                android:text="@string/sensors_available"
                android:textSize="12sp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
    </TableRow>
    <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
        <Button
                android:id="@+id/b_start_hm"
                android:text="@string/hm_start_service"
                android:textSize="12sp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        <Button
                android:id="@+id/b_stop_hm_service"
                android:text="@string/hm_stop_service"
                android:textSize="12sp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
    </TableRow>
    <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
        <Button
                android:id="@+id/b_start_hot_video"
                android:text="@string/video_service_start"
                android:onClick="startHotVideoService"
                android:textSize="12sp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        <Button
                android:id="@+id/b_hv_stop_service"
                android:text="@string/hv_stop_service"
                android:textSize="12sp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
    </TableRow>
</TableLayout>

Is it possible for me to replace all the 12sp with something like @string\custom_text_size and then define that in the string.xml file? When I tried this, I got a fatal exception when I started my app.

JTate
  • 332
  • 1
  • 13
  • 1
    u can use dimen ... see more here for more info http://stackoverflow.com/questions/11121028/load-dimension-value-from-res-values-dimension-xml-from-source-code – Matej Špilár Sep 30 '13 at 18:44

2 Answers2

2

You were so close, but you need to define a dimen instead of a string

<dimen name="textSize12">12sp</dimen>

And your Button would look like this.-

<Button
    android:id="@+id/b_stop_hm_service"
    android:text="@string/hm_stop_service"
    android:textSize="@dimen/textSize12"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

Ideally, dimen resources are defined in a separate resources file, such as dimens.xml

ssantos
  • 16,001
  • 7
  • 50
  • 70
0

Create a style in res/values/styles.xml:

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

Declare it in your XML:

<Button
    android:id="@+id/b_start_hot_video"
    android:text="@string/video_service_start"
    android:onClick="startHotVideoService"
    style="@style/TextSize"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

By using styles, you can store multiple settings, like background drawables, textColor etc...

Evan Bashir
  • 5,501
  • 2
  • 25
  • 29
  • PS: imho, there's no need to create a style just for setting `textSize`. Unless you're setting more properties (such as color, shadow, margins...), it makes more sense to me just using a `@dimen` resource. – ssantos Sep 30 '13 at 18:58
  • Agreed @ssantos, I was just trying to give a more versatile answer. – Evan Bashir Sep 30 '13 at 18:59