Different date format for different screen sizes

My android app can be run on tables and phones (read - large and small screens). Throughout my app, I'm using different resources as necessary (layouts, dimensions, styles, strings) by placing them in values or values-sw600dp as appropriate.

One element I'm struggling with though is date formats. In a couple of places I use DateFormat.LONG (which gives me, for example, November 15, 2015). This works and looks very well on larger screen, however on small phone screen, the whole text doesn't fit and it gets truncated. To work around it I want to use different format for small screen, e.g. DateFormat.MEDIUM (which would give me Nov 15, 2015 that would fit fine on the small screen).

I could dynamically determine screen size and use one format or the other. This feels rather inefficient and I would prefer to specify the format in resources so that I could easily change it later - yet I can't find a way to do this. I can think of a few hacks - but they really are hacks (e.g. store string LONG in string resources, then at runtime use reflection to look up field named LONG in DateFormat and get its value).

So, is there a simple way to store an indicator of built-in date formats in resources?

Answers


Use something like this:

First approuch: DateFormat.format(getResources().getString(R.string.main_data_format), data.getTime()).toString();

and in resource you will contain format for different screens.

Second approach: java.text.DateFormat.getDateInstance(getResources().getInteger(R.integer.data_format_style));

where integer is one of:

public static final int DEFAULT = 2;
public static final int FULL = 0;
public static final int LONG = 1;
public static final int MEDIUM = 2;
public static final int SHORT = 3;

depending which format is in which screen size.

To avoid problem with future changes of API you can prepare the own set of constants in default values:

   <resource>
   <integer name="data_format_default">202</integer>
   <integer name="data_format_full">200</integer>
   <integer name="data_format_long">201</integer>
   <integer name="data_format_medium">202</integer>
   <integer name="data_format_short">203</integer>
    </resource>

then for each screen size define the int value which refer to one of above:

<integer name="foobar__format_style">@integer/data_format_medium</integer>

in same util class define the map method:

public class Utils {
public static final int getDataFormatStyle(int data_format_style_from_resource){
         switch(data_format_style_from_resource) {
             case data_format_full:  return DataFormat.FULL:
             case data_format_long:  return DataFormat.LONG:
             case data_format_medium:  return DataFormat.MEDIUM:
             case data_format_short:  return DataFormat.SHORT:
             default:  return DataFormat.DEFAULT:
         }    
    }
}

and then use them like this:

final int style = getResources().getInteger(R.integer.foobar__format_style);
DateFormat.getDateInstance(Utils.getDataFormatStyle(style));

I eventually ended up using reflection, as it gives me the most flexibility to change things easily. Here's what I got now:

In strings.xml:

<string name="title_date_format">LONG</string>

In my code:

String formatString = getString(R.string.title_date_format);
int style = DatFormat.class.getField(formatString).getInt(null);
DateFormat format = DateFormat.getDateInstance(style);

Need Your Help

Filter IP-List with Batch-Program

windows batch-file cmd

I have a list full of servers and I want to check which ones are running and which are offline. So I want to ping them once to see if they respond. I filter them with "findstr TTL". But now I want to

Getting derived type from list of parent types

c# inheritance

Given the following two class types, that are both derived classed of the built in ValidationResult class: