Cloud to device messaging

SDK Version: 

Android has so many features, that it's hard to go over every one of them. We wanted to try out cloud to device messeging (c2dm) for a long time now, and in the past few days I had some time to play with it. Here is a small tutorial on the topic.

How to draw multiline text to canvas easily

SDK Version: 

There are situations where you have to use a Canvas. What do you do if you have more text than you can properly display?
The problem is, that if you want multiline text on canvas, with the drawText method, you would have to measure how much space a single line of text would take up, and also compare it to the width of the screen, and draw each line separately.
Read more to check my solution.

PreferenceActivity with custom functionality

SDK Version: 

As we previously desribed in PreferenceActivity Basics a preferences screen with basic features can be done with minimal work, only using XML. Now we will inplement some functionality beyond the base ones, this will require some coding in addition to the XML definition.

We will display the actual value in each preferences summary field, like this:

The base XML will be the same as in the previous example, but we dont need to fill summary fields, they will be filled dinamically.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <PreferenceScreen xmlns:android=""
  3.         android:title="Settings">
  4.         <EditTextPreference android:title="Name"
  5.                 android:inputType="text"

Using Facebook SDK in Android development, Part 2.

SDK Version: 

fbIn this part, I show you an android application, which logging in to Facebook, then get the Facebook ID.
Please go through the first part of the tutorial, before reading this post.

Using Facebook SDK in Android development, Part 1.

SDK Version: 

fbConnecting to Facebook from an Android application is not as easy, as it looks. This guide will help you through some problems that you will propably encounter, with a clear and simple solution.

Removing an app icon from launcher

SDK Version: 

Creating an application that does not appear among the launchable applications with an icon is easy.
Just do not put a launcher activity into AndroidManifest.xml

  1. <intent-filter>
  2.   <action android:name="android.intent.action.MAIN" />
  3.   <category android:name="android.intent.category.LAUNCHER" />
  4. </intent-filter>

Removing an application icon after installation programatically is a bit more tricky.
You can not disable the icon itself, but you can disable one component of an application. So disabling the applications launcher activity will result its icon to be removed from launcher.

The code to do this is simple:

  1. ComponentName componentToDisable =
  2.   new ComponentName("com.helloandroid.apptodisable",
  3.   "com.helloandroid.apptodisable.LauncherActivity");
  5. getPackageManager().setComponentEnabledSetting(
  6.   componentToDisable,

Communicating between an activity and the browser - callback

SDK Version: 

A few days ago Gabor made an article about communicating between activites. I'm currently working on a pet project that uses Oauth with the google data api, where I had to get a response from the browser, so let's take a look at communicating between an activity and a browser.

Communicating between running activities

SDK Version: 

Starting a new activity from another and passing some data to it is a simple and basic thing in android. But if you want an already running activity to come to foreground, and pass data to it, it can be a bit tricky.

First of all by default if you call an activity with an intent, a new istance of that activity will be created and displayed, even if another instance is already running. To avoid this the activity must be flagged that, it should not be instantiated multiple times. To achieve this we will set the launchMode of the activity to singleTask in the AndroidManifest.xml

  1. <activity android:name="Activity1" android:launchMode="singleTask" android:label="@string/app_name">

This way when we call this activity using an intent, if there is an existing instance, the system will route the request to it. Hoever the onCreate method, where we usually process the passed extraData, will not run this time.

Parsing UTF-8 encoded content correctly from SQL through Php with Json

SDK Version: 

To get this thing working, just follow these steps:

1. First thing to do is changing the default character set of your SQL database and its tables to UTF-8.

2. Your Php file should look something like this:

  1.   $connection = mysql_connect("server_ip_address", "user", "password");
  3.   if (!$connection) die("Couldn't connect to server");
  4.   mysql_select_db("database_name", $connection) or die("Could'nt select the database!");
  6.   // This is very IMPORTANT!!!
  7.   mysql_query("SET NAMES 'UTF8'");  
  9.   $sth = mysql_query($query);
  10.   $rows = array();
  11.   while($r = mysql_fetch_assoc($sth)) {
  12.       $rows[] = $r;
  13.   }
  14.   print json_encode($rows);
  16.   mysql_close($connection);  
  17. }

3. Now you can parse these json forrmatted strings, to your application. Here's how:

Reading logs programatically

SDK Version: 

Writing logs into logcat from yuor application is quite easy, reading the logcat programmatically is just a bit more tricky.
Reading logs is usually used for bugreport purposes.


Syndicate content