The Blog

Miglior Smartphone Android – Come sceglierlo

03 Giu 15

Rotto il vostro smartphone o è semplicemente  arrivato il momento di cambiarlo? Periodicamente ci si ritrova con il problema di andare a scegliere il miglior smartphone che fa per voi.

Purtroppo online c’è molta confusione a riguardo e moltissime volte vi ritroverete a leggere decine e decine di recensioni per poi non capirci più nulla.

Fortunatamente lo staff di androidiani.com mantiene costantemente una classifica dei migliori smartphone android sul mercato. Questa classifica, anche se non definitiva, vi aiuterà a restringere il campo di ricerca a pochi telefoni invece che continuare a navigare nel buio tra schede tecniche e opinioni personali fatte da diverse persone.

La classifica si presenta in modo chiaro e conciso ed è suddivisa in fasce di prezzo. Avrete così modo di considerare solo i migliori smartphones per fascia di prezzo. Attualmente le fasce proposte dallo staff di androidiani per miglior smartphone android sono:

Buon acquisto!

Discerning if the user is using your android app from a phone, tablet or even Google Tv.

24 Ago 12

At some point every Android developer will need to develop an app that works ( exploiting each device functionalities ) on both Phones and Tablets ( or even Google TV ).

At the very beginning the first question that pops up in the dev mind is ( at least this was the question that popped out in mine ):

Should I make a single app version that works on both android phones and tablets? Or should i leverage the Google Play Store console benefits and upload different version of the same app optimized per each device?

Lets figure it out using a PRO/CONS approach

Single App

PROs (against to the multiple Apps):

  • Less code to maintain — when making a modification to some device agnostic code you don’t need to replicate it anywhere
  • Much more scalable

CONs:

  • You’ll be required to handle all the logic that routes specific device activities.

Multiple App

PROs (against to the Single App approach):

  • All the routing staff is made upfront from the Google Play store + AndroidManifest filtering.

CONs:

  • More code to maintain  — Fixing a bug on some shared code will require you to make 2+ app updates.

In this post i’ll cover how to understand if the used device is a tablet, a phone or a google tv. This is mostly useful for the Single App approach ( but in some cases could be helpful for in the Multiple App ).

The goal (yes again) is to have some piece of code that will help you understand if the device is a phone, tablet or a google tv.

I started googling and I found a couple of valuable resources on stackoverflow which helped a bit. But I didn’t find a complete and elegant solutions in none of them so I started my own research.

I needed to find an open source app that fullfilled the above mentioned prerequisites. It was surprisingly simple since Google, each year, releases the source code of their Google IO app. ( which uses the single app approach ).

If you never used the app I can assure you it works on both tablet and phones.

Lets talk no more and get to the real juice: Google IO Sched uses a  class called UIUtils which provides some static methods. Among of them you’ll be able to find the following methods:

  • hasHoneycomb: which returns true if the device is “at least” honeycomb apis => ics returns true
  • isTablet(Context): which returns true if the device is recognized as a tablet
  • isHoneycombTablet(Context): which returns true if both isTablet(Context) and isHoneycomb() returns true.
  • isGoogleTV(Context): which returns true if the device is a GoogleTV

Now you’ll probably wondering why there are 2 methods in order to recognize if the device is a tablet or not. Well, the answer is simple: There are tablets out there that are pre-honeycomb so you won’t be able to use the (out of the stock) Fragment apis available since Honeycomb.

Here the source code of those methods:

Now lets use this goodness to make something real. Lets suppouse you’ve 2 activities:

  • MapActivity: which is the activity that will be used for phones and eventually google tv
  • MapMultiPaneActivity: which is the activity designed specifically for tablets.
You could use the approach shown in the code below that also exploits the Facade pattern:
Warning: none of the previous code is mine. Everything was taken from the source code of Google IO Sched. BTW, I hope you enjoyed my effort of making this easy to understand/use

Android – Validate an email inside an EditText ( and more )

26 Mag 12

Input validation could be very painful. Building forms, in every technology, is a boring and painful job.

Fortunately on the web many libraries have born in order to facilitate this task to web developers. In android there are, built-in, some best practices you can use to facilitate the job but you’ll always have to take care about:

  • in-depth data validation
  • mandatory fields check
  • error presentation

Bored about giving birth another painful form I decided to write an opensource library that does almost everything for me. The library is called FormEditText and you’ll be able to browse/download the sourcecode here in github.

The basic usage of the library will allow the developer to get an edit text validated through xml attributes.

In this snippet of xml code we have a lot of good tech ( both from Android and my library ):

  • line #3:  we define a new namespace. You’ll need to modify the package name ( com.andreabaccega.edittextformexample in the code ) with yours
  • line #9: we provide an hint for the user so that he’ll know what he should write inside the field
  • line #10: we provide a value for the stock android:inputType attribute.
  • line #12: we tell the library that the field should be a valid email address

The last thing we’ll need to do is to ask the library to check the edittext validity:

This piece of code, performed when the user clicks the “submit” button, will ask the library to validate the content. Here two things could happen:

  1. the input is valid: in this case the library will just return true ( obviously )
  2. the input is not valid: in this case the library will return false and will set an error message that will look like the image on the top of the page.

When in the first case the library won’t do anything except returing “true” on line #3 of the previous snippet.

Further readings:

I’ll suggest to take a look at the github project. If you want to try the library I also setted up an example app that you can download from the market.

android


FormEditText Library Example
Andrea Baccega
Gratuito
1.000-5.000
Link Android Market

Android: How to execute some code only on first time the application is launched

12 Apr 12

It happens you need to execute some piece of code only on the first time the user start using your app.

Lets say you want to show a quick tutorial to the user just once — Indeed, when the user open your app for the first time.

Well, an easy solution would be to use a SharedPreference to store the info we need to accomplish this “task”.

Below, a snippet with a simple method ( to be included inside your activity class ) that will “solve” the problem.

onChange event on EditText in Android

09 Ott 10

Sooner or later you’ll have to deal with it. If you’re an html developer and you write also in javascript you’ll surely know the onchange event.

Unfortunately it’s a little bit tricky to find the same event on android.

The onChange event is helpful when you’ve to deal with the following things:

  • Let the user know (in realtime) how many characters he typed.
  • Let the user know (in realtime) how many remaining characters he is allowed to type.
  • Make realtime processing of the content ( like sending it online and fetch some partial results of the partial typed edittext )

You’ve to implement your own instance of TextWatcher and let the edittext know that you want to be notified at each change by calling the method EditText.addTextChangedListener.

Below i will give you a simple example ( it’s written on the fly but you’ll understand the idea )

[sourcecode lang=”java”]
((EditText)findViewById(R.id.et_testo)).addTextChangedListener(new TextWatcher() {

public void afterTextChanged(Editable s) {
((TextView)findViewById(R.id.numcaratteri)).setText(String.format(getString(R.string.caratteri), s.length()));

}

public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub

}

public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub

}

});

[/sourcecode]

Write an SMS without sending it on Android 2.2 Froyo

12 Ago 10

I just figured out how to write an sms without sending it really on android froyo 2.2 using the content provider.

I’ll write only the snippet here. Hope it helps

[sourcecode language=”java”]
/**
* writes an sms on the contentprovider
* @param ctx Context
* @param mobNo mobile number
* @param msg text of the message
*/
private final static void storeMessage(Context ctx,String mobNo, String msg) {
ContentValues values = new ContentValues();
values.put("address", mobNo);
values.put("body", msg);
ctx.getContentResolver().insert(Uri.parse("content://sms/sent"), values);
}
[/sourcecode]

Obviously you should set the right permissions on the manifest ( Yes the following are all needed for this task ) :

[sourcecode language=”xml”]
<manifest>
….
….
<uses-permission android:name="android.permission.WRITE_SMS"></uses-permission>
<uses-permission android:name="android.permission.READ_SMS"></uses-permission>
</manifest>
[/sourcecode]

Add Events on Google Calendar on Android Froyo and above

09 Ago 10

Since i started developing applications for android i noticed there were some undocumented apis. Google does reccomend to not use these apis but since there are no “other nice ways” to achieve some tasks sometimes they are useful ( but still unreccomended)

It’s the case of the Google Calendar Apis. Out there you can find a lot of docs about these undocumented & unsupported apis but you’ll get some troubles if google decides to change them.

For example if you want to add an “event” to the calendar programmatically you can follow the snippet below which is SDK proof. In fact i did ( It’s not refactored for better reading ) write some code that would work on Sdk from 1.5 to 2.2 ( aka Froyo ) solving the provider issue on froyo and above.


Hope it helps to solve the problem about google calendar in froyo :)

Reference to the CP: Xda

Android Usefull Docs

27 Lug 10

Today i surfed the web searching about guidelines and best practices on writing android apps. In this in-depth search i found some useful slides and tips:

1. Android Ui Design Guidelines

A slideshare with some usefull tips about android ui design:

[slideshare id=4827211&doc=uidesigntips-100723230420-phpapp02]

2. Android Ui Stencil and PSDs

I also found a great resource where there are some other stencils and psds to download.

Link

3. Android icon templatepack

I also noticed the Google Android team released the icon template pack so you can easily design you’r own menus and icons using their psd with a lot of presets.

Link

Simple Gps Info for Android

20 Mar 10

Simple Gps Info is a simple android application that would help you know and share your gps coordinates with your android smartphone. The code is really fast and efficient and the app consist on a simple to understand layout without any kind of menu.

Once started, Simple Gps info for Android will start automatically your android gps ( Even if it’s disabled by you ) and then it will start seeking for a gps-fix.

In the meanwhile you could also look at the radar that would rotate following  the north pole. You can try and see where is the north-pole by simply rotating your smartphone. Furthermore the radar will show you the satellites you’re smartphone is receiving.

When the Gps sensor get the fix you’ll start to see the coordinates changing and you’ll be able to share your location with your android phone.

In order to accomplish this task you’ve to push the bottom-placed button and then choose how to share it.

Right now Simple Gps info will support the following way to share the location:

  • Facebook
  • Twitter
  • Sms
  • Gmail
  • Others activity implementing text/plain content-type intent receiver.

Here are some screenshots of the first version. ( Now improved with better graphics )

If you’ve installed Barcode Scanner on your android phone you can scan this:

Google Analytics Bridge for Android developers

23 Ago 09

Google Analytics Bridge is a java library for android ( Soon to be Open Source licensed ) that will help developers understand what the user interaction with their own application.

Google Analytics Bridge is really easy to setup and it runs on a separate thread. This is do to the problem with the internet request sometime freezing the UI and setting off the hated message:

“The Application xyz is not responing”

With the two choises :

  1. Force Close
  2. Wait

Since The Google Analytics Bridge runs on a different thread to avoid this error.

First Setup

Download and import the jar file to the application you want use the Google Analytics Bridge on. The setup is a fast one-line command but you need the following information:

  • Google Analytics Tracking Code ( EX: “UA-123123123-1” )
  • The Domain Name

Set up a brand new Google Analytics Profile

you will need to set up a new Google Analytics Profile or use an existing one. You will need to open a new website profile on google analytics.

The procedure is fast and simple but it does require The domain name.

In that field you can specify whatever you want, but i suggest something like:

track.your-application-name.com

After creating the new profile, copy the UA-XXXXXX-Y code for a further usage.

The Constructor

Once you’ve done all of step one it’s time to create our constructor.

In the onCreate routine place the following code:

[JAVA]new AnalyticsBridge(this.getApplicationContext(),”UA-XXXXXX-Y”,”track.your-application-name.com”);[/JAVA]

You don’t need to create an AnalyticsBridge object , after this line you will call the methods in a static way.

NOTE: You should call the constructor once per application lifetime.

Public Methods

_trackPageview(String pageTitle)

If you’ve some experience integrating google analytics on your own website you’ll also remember what this method will do.

_trackPageview method on the web it’s used for logging a new page View by the user. Similarly calling this static method on android will generate a google analytics request that will log a new Activity View.

Lets say you’ve a lot of activity, and some of theese will show the same content but in a different way, then i would put the following code on each onCreate Subroutine:

[JAVA] /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

AnalyticsBridge._trackPageview(this.getClass().getName());

}[/JAVA]

This code will log each opened activity to google analytics.. Isn’t it easy? :)

_trackEvent(String category, String action)

_trackEvent method is the same used on the web google analytics apis. I made this bridge because you should want to track some event made by the user.

Let suppouse you want to track a button click event on your user interface – this could help you understand how the user interact with your user interface – then you simply write the following statement on your View.OnClickListener implementation:

[JAVA] Button loginbutton=new Button();
loginbutton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
AnalyticsBridge._trackEvent(“Click”, “Login Button”);

}
});[/JAVA]

That code will track the event of category “Click” and action “login Button”.

Download:

Right now the library is quite stable . You can download it here

Thanks

Thanks to barakinflorida who corrected my bad english :)