7

I am a client side developer that entered recently into an android project.

In client side world we got frameworks like SASS that let us do math operations on variables.

In Android styles resources, we got Variables and Dimensions. My Question is if its possible to achieve that in Android as well.

For example:

dimens.xml
<dimen name="baseFontSize">18sp</dimen>

styles.xml
<style name="heading1">
    <item name="android:textSize"> @dimen/baseFontSize * 2.5 </item>
    ...
</style>

I define a base font size unit, and plays with it along all the app.

If itsn't possible, whats the best practice to achive it using the Android Way.

Idemax
  • 2,712
  • 6
  • 33
  • 66
neoswf
  • 4,730
  • 6
  • 39
  • 59
  • I guess no but you could set a dimen dynamically e.g. text size in a TextView. – Idemax Jul 15 '14 at 13:41
  • What you mean Marcelo? Can you elaborate about it in an answer? – neoswf Jul 15 '14 at 13:44
  • What exactly is your end-goal in testing the font sizes like this? Are you testing for multiple screens? – Andrew Schuster Jul 15 '14 at 13:47
  • @AndrewSchuster No. As in ```CSS```, I want to create a scalable style definitions, that once I change in a centralized document a fontSize value (fontSize is just an example. it can be any thing), it influence all my app, and I don't have to correct to many places on future modifications, that can come from the designer, or Themes adaptations. – neoswf Jul 15 '14 at 14:01
  • Besides that, in the case of fontSize, when you got a basefont size, you can create a very stable typographic system. E.G. - all headings, all paragraphs, all labels - everything will be based on a single variable. – neoswf Jul 15 '14 at 14:04

1 Answers1

2

You could create a main controller to handle all your scale sets.

For example, you have a layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/my_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        android:textSize="16sp" />

</RelativeLayout>

And you wanna scale the text size android:textSize="16sp" by a scale in dimens.xml.

Create your scale rate in a dimens.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <item name="scaleRate" format="float" type="dimen">2.5</item>

</resources>

And after you create you main controller as this:

package com.example.stackoverflowsandbox;

import android.content.Context;
import android.widget.TextView;

public class MyScaleController {

    public static void applyTextSizeScale( final Context context, final TextView myTextView ) {
        final float currentTextSize = myTextView.getTextSize();

        myTextView.setTextSize( currentTextSize * context.getResources().getDimension( R.dimen.scaleRate ) );
    }
}

So you use like that:

package com.example.stackoverflowsandbox;

import android.app.Activity;
import android.widget.TextView;

public class MainActivity extends Activity {
    @Override
    protected void onCreate( final android.os.Bundle savedInstanceState ) {
        super.onCreate( savedInstanceState );

        this.setContentView( R.layout.activity_main );

        final TextView myTextView = ( TextView ) this.findViewById( R.id.my_text_view );

        MyScaleController.applyTextSizeScale( this, myTextView );
    }
}

Now you can create many dimens of scale rate to many screen sizes. What you wann at first time, make math calc in XML file, not will work. But thi way I guess you have best approach.

Idemax
  • 2,712
  • 6
  • 33
  • 66
  • 1
    Thank you for your answer @Marcelo, but I wished to find a way to do it in XML only. As I said in one of the comments in the OP, I wish to create a scalable and stable style system. That I can change simply in the style file, and that it will be accessible easily for future modifications. Doing it inside Classes breaks the simpleness and scalability I wish to achieve. ```Sorry``` I made you write me such an invested answer. I didn't understand what you meant in your comment. – neoswf Jul 15 '14 at 14:29
  • What you could do it I did is create a main controller that you use to get dimens do you could keep you dimens of styles and scale rate and user your main controller to manage it. I will update my answer to figure out what I said. – Idemax Jul 15 '14 at 15:03
  • 1
    @neoswf check my answer now, I guess help you. – Idemax Jul 15 '14 at 15:19