Tuesday, March 26, 2013

Difference between dp and sp

dp
Density-independent Pixels - An abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi (dots per inch) screen, on which 1dp is roughly equal to 1px. When running on a higher density screen, the number of pixels used to draw 1dp is scaled up by a factor appropriate for the screen's dpi. Likewise, when on a lower density screen, the number of pixels used for 1dp is scaled down. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Using dp units (instead of px units) is a simple solution to making the view dimensions in your layout resize properly for different screen densities. In other words, it provides consistency for the real-world sizes of your UI elements across different devices.

sp
Scale-independent Pixels - This is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and the user's preference.

Taken from here:
http://developer.android.com/guide/topics/resources/more-resources.html#Dimension

px = dp * (dpi / 160)

or equivalently:

dp = (px / dpi) * 160

Wednesday, June 20, 2012

MemoryGame out on Google Play




You think you have great memory? Check how good you really are!


Google Play link:
http://goo.gl/SNtBJ

This game is application for specialized form of exercise based on the Android platform. It is used for the exercise of cognitive abilities of users. It focuses on the needs and capabilities of older users, but also the needs of carers (monitoring the progress of the elderly in a variety of purposes - diagnosis, effective treatment, etc.). Younger people can find it very interesting too.



Check it out! :)

Friday, June 8, 2012

Boxing Champ soon on Google Play



Check out Boxing Champ facebook page
for info on new Android game!
I think you'll find it visually attractive and innovative!

http://www.facebook.com/BoxingChampGame

Want to place your ads in our game?! ..or buy whole level?
Contact us on boxing.champ.game@gmail.com

Our offer includes:

1) Main screen - We can put your logo on the main screen.

2) Ad space inside the game (during the match) - Your logo will bi visible during the fight on billboards through the environment around players. These are premium places for ads so you contact us now if you want them.

3) Link in credits screen - we will put link to your page in our credits screen.

4) Buy whole level - You can also buy entire level. You choose the theme, we draw it, place your ads where you want them, and implement level in our game. It will bi indicated that you sponsored this level on level select menu.




Coming soon on Google Play!

Hope you'll like it!

Thursday, January 5, 2012

esava is out!

This app is made to make your life easier, and to increase your quality of life. Using this app you can see cameras placed at restaurants in "SD Stjepan Radić, Zagreb, Croatia". You can also see the daily menus. Enjoy your meal and wish you success in studies.

Get the app on Market: http://goo.gl/9RSSk

Monday, December 5, 2011

Christmas Hit is out!

Christmas is a time of giving. It is also a time for party, a time for fun, holiday, and family reunion. Wish you merry Christmas and have fun with this game!

"Christmas Hit" is my new game. Check it out on Android market.

Hit the lights! ;-)


 


Thursday, June 9, 2011

Sunday, May 29, 2011

JExpenses on Market


Check this out..

https://market.android.com/details?id=com.jexpenses&feature=search_result

Very simple application which i've made for myself and decided to share it with the world :-) I find it useful, and hope you will too..

Friday, March 25, 2011

Android force locale and orientation problem

In my attemps to force locale in my application, i've faced an orientation problem.
Forcing locale worked, but when the orientation was changed, language would go back to default.
I spent some time resolving this issue, so I've decided to share my solution.

They way to force locale in your application is to extend Application.
You create a class that extends Application class and set it up for use in AndroidManifest.xml

The code i used in my application is this:

public class MyApplication extends Application
{
    public static final String FORCE_LOCAL = "force_local";
    public static final String FORCE_CURRENCY = "force_currency";
   
  @Override
  public void onCreate()
  {
    updateLanguage(this,null);
    super.onCreate();
  }

  public static void updateLanguage(Context ctx, String lang)
  {
    Configuration cfg = new Configuration();
    SharedPreferences force_pref = PreferenceManager.getDefaultSharedPreferences(ctx);
    String language = force_pref.getString(FORCE_LOCAL, "");
   
    if(TextUtils.isEmpty(language)&&lang==null){
        cfg.locale = Locale.getDefault();

        SharedPreferences.Editor edit = force_pref.edit();
        String tmp="";
        tmp=Locale.getDefault().toString().substring(0, 2);
       
        edit.putString(FORCE_LOCAL, tmp);
        edit.commit();
       
    }else if(lang!=null){
        cfg.locale = new Locale(lang);
        SharedPreferences.Editor edit = force_pref.edit();
        edit.putString(FORCE_LOCAL, lang);
        edit.commit();
       
    }else if(!TextUtils.isEmpty(language)){
      cfg.locale = new Locale(language);
    }
   
    ctx.getResources().updateConfiguration(cfg, null);
  }
 
 
  @Override
  public void onConfigurationChanged(Configuration newConfig)
  {
        SharedPreferences force_pref = PreferenceManager.getDefaultSharedPreferences(getBaseContext().getApplicationContext());

      String language = force_pref.getString(FORCE_LOCAL, "");    
       
      super.onConfigurationChanged(newConfig);
      updateLanguage(this,language);
  }
 
}

In the AndroidManifest.xml you should add the following attribute:

<application android:name="janalysis.pocetni.MyApplication" ...

Using onConfigurationChanged(Configuration newConfig) function where updateLanguage(this,language); is called solves the orientation problem.

onConfigurationChanged(Configuration newConfig) is called every time orientation changes from Portrait to Landscape or the other way around.

To change locale from your application you use
MyApplication.updateLanguage(getApplicationContext(), "en");

You use the string of a language you want to force instead of „en“,
for exaple „de“ for German, „fr“ for French, „hr“ for Croatioan..

After using this piece of code in your application only activities loaded after the change has been made will be translated into new language.
Activities that are already open will be displayed with the old language.

The way I solved it is restarting application after updating language. Restarting can be done using PackageManager, like this:

Intent i = getBaseContext()
.getPackageManager()
.getLaunchIntentForPackage(getBaseContext()
.getPackageName());
                               
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

startActivity(i);

It would be nice to let the user know application will/needs to restart. You can use, for example, an AlertDialog.. it's up to you.

After installing and first run, you app will be set up to a locale used on the phone. If yout app does not support phone's local it will use the default one.

After changing the locale within the app itself, that locale will be saved, and app will use it until you change it again within the app itself.

That means that after you've set up you locale in the app, if you change phone's locale, app will still keep it's locale.

You can test your application's behaviour on a device using free apps for forcing locales. These can be found on Market: 'More Locale 2' or 'LocaleSwitch' for example..

Hope this helps ;-)
Ivan

Friday, February 11, 2011

Android using R.string resource in TextView and ArrayList

Here are few examples of getting R.string resource in code of your application.
It all comes down to using getText() and getString() functions when needed.

Example 1
If you have an array list like this:
      private List<String> categories = new ArrayList<String>();
and you want to add a string from strings.xml to that list.

Let's say string is saved in strings.xml like this:
            <string name="first_category">The first category</string>

To add this string from strings.xml to categories list you need to use getString() function.
It goes like this:
            categories.add(getString(R.string.first_category));
If you try this without getString() function Eclipse will show an error.

Example 2
If you have a TextView like this:
      private TextView txtViewExample;
and you want to set its text to a string from string.xml.

The string you want to add is saved in strings.xml like this:
            <string name="stringExample">String Example</string>

  
If you try code like this:
txtViewExample.setText(R.string.stringExample);
TextView will show the correct text.

But, if you have a similar situation that needs you to add a text from strings.xml plus a string from a variable you might get into trouble.

Let's say you have a string variable:
      private String tempString="XX";
and you want txtViewExample to show text like this:
         String Example XX

If you try something like this:
txtViewExample.setText(R.string.stringExample + " " + tempString);

txtViewExample will show something like this:
         2131034177 XX
As you can see instead of text from strings.xml  --> stringExample
a number od R.string.stringExample resource is shown.

To do things right you need to use getText() function:

txtViewExample.setText(getText(R.string.stringExample) + " " + tempString);

Then you will get the right text displayed.

Hope this helps! ;-)