Archives 2012

Android's digest

Written by Xavier Gouchet - 07 december 2012 - no comments

I don't have many news to give (yet), but here's a digest of articles I've read this week, and found rather useful.

Runtime Theme Change

I made a couple of test earlier this year to provide a theme changing features in some of my apps. The best way I cam across was to restart the activity to set the theme in the onCreate() of my activity.

Sriram Ramani provides a solution to change the theme in no time, and without restarting the activity. Find out how in this article.

Plugins

Communication between 3rd party applications and services is very easy in Android, thanks to the Intent system. But what if you want to embed a piece of code from another application inside your own, and staying in the same activity.

On a desktop app, plugins can easily be done using DLLs, but on Android, it's not as easy. Gabor Paller presents an architecture to have a master application, and any 3rd party app used to extend it.

Read his explanations and download a sample project from his article.

Time Circuits Widget update : now Android 4.2 compatible

Written by Xavier Gouchet - 21 november 2012 - no comments

Android 4.2 has been released a couple of weeks ago, and brought with it new features. One of them is the ability to add Widgets to the lock screen. The perfect occasion for me to update my Time Circuits Widget

Time Circuits Widget - Lock Screen

Additionally, you can now resize the widget on your home screen to let it take the whole width of your phone and have it centered on screen.

Android app on Google Play

Embedding meta data in an application's manifest

Written by Xavier Gouchet - 19 november 2012 - 1 comment

A little tip I just discovered : besides having information on Activities, Services and such, you can embed any metadata inside an application's manifest.

To do this, simply include the following meta-data tag in an application, activity, activity-alias, service or receiver. The tag can either be used to store a value (int, float, string) or a resource id :


<meta-data
    android:name="MD_NAME"
    android:value="1.5" />

<meta-data
    android:name="DB_OWNER_ID"
    android:resource="@string/meta_owner" />

The meta data is stored in a Bundle instance, which you can retrieve using one of the following lines :


PackageManager pm = getPackageManager();
PackageItemInfo info;

info = pm.getActivityInfo(component, PackageManager.GET_META_DATA);
info = pm.getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
info = pm.getReceiverInfo(component, PackageManager.GET_META_DATA);
info = pm.getServiceInfo(component, PackageManager.GET_META_DATA);
		
Bundle metaData = info.metaData; 

You can then read any metadata stored in the bundle using the getter methods.

A developer tool : Device Art Generator

Written by Xavier Gouchet - 14 november 2012 - no comments

The Android Developper website contains lots of hidden pages, which I discover kind of randomly.

Today I discovered the Promoting section, and especially the Device Art Generator page.

This page lets you embed screenshots (or any image) into a picture of a real Android Nexus device. It even adds a shadow beneath the device, and a glare on the screen over your image. Ideal to create promotional image of your apps !

Ted promotional image

Ted Update : now with fonts

Written by Xavier Gouchet - 12 november 2012 - no comments

I just released a new version of my text editor for Android : Ted.

This version, among other things, now lets you set a custom font to display your text. You can now select any TrueType font available on your External / Internal storage.

Ted with custom fonts

Android app on Google Play Fork me on GitHub

A developer tool : Intent Intercept

Written by Xavier Gouchet - 09 november 2012 - no comments

One of the greatest idea in the Android framework is the concept of Intents, which let potentially any application interact with any other application, without any of them knowing explicitly the other.

The best example is the "share" intent. When you click the Share button on an application, an Intent is created, with a Action named "ACTION_SEND", and maybe some data embedded in it. Then Android tries to find all application who declare themselves as able to handle an Intent with the "ACTION_SEND" action, and let you choose which one to use.

Share intent - Application chooser

Intent Intercept is a pretty usefull app to know what intent is used for a specific action in an application. When an implicit intent is launched on the device, you'll always have the option to handle it with Intent Intercept which will then analyse the intent's content for you.

Intent Intercept

As always, you can download it from the Google Play Store.

Chrome extension : Android SDK reference search

Written by Xavier Gouchet - 31 october 2012 - no comments

I recently discovered and set up search macros in Google Chrome, and I really like how it can save time. "Android SDK reference search" Chrome extension goes a step further.

Android SDK reference search

Firstly, by typing "ad Act" in Chrome's address bar, you get as suggestions direct link to the Android SDK reference, for classes beggining with "Act" (Activity, ActionBar, ...).

Secondly, when browsing the Android SDK reference, you get a "View Source" link just next to the class name, which takes you directly to the source code of the given class.

You can install it from this link

Axel : an new Android XML Editor

Written by Xavier Gouchet - 05 october 2012 - no comments

Sometimes, for many reasons, I have to read, or edit an XML document from my phone. Although my Text Editor, Ted does this very well, it's not entirely perfect to edit a markup text.

Searching on the play store for a solution brought me no satisfaction : either apps were paid, specific for HTML or not user friendly. So, I decided to create one, and here it is.

Axel - a XML document Axel - Context menu

Axel displays any (well formed) XML or HTML file in a tree, and let you edit each node, with proper validation and syntax coloration.

In the future, I'll add the option to drag'n drop nodes anywhere in the tree, handling Doctype validation, and add suggestion for tag names and attributes according to XSD / DTD.

You can download it from the Play Store, and if you see anything that you would like to have in this app, please let me know !

Axel - a HTML document Felix - Edition

A developer tool : Android UI Patterns

Written by Xavier Gouchet - 27 september 2012 - no comments

There are lots of apps out there that we, developers, make for people. But sometimes, there is an application made for us.

Today I'll present Android UI Patterns, created by Groidify. This app goes in a long list of tools and websites to help UI/UX designers to create wonderful applications.

This application presents many UI Pattern libraries, to add rich UI to Android apps. And the great thing is that you can preview a sample of each library effect. This is perfect to choose between two similar applications.

Android UI Pattern Android UI Pattern

I also tested a couple of similar apps : The Ultimate Android Library, but without the ability to preview the library in action, and Droid UX, which is more focused on look and feel than UI pattern.

Felix : A new version of my Felix' plorer

Written by Xavier Gouchet - 18 september 2012 - no comments

It's always a strange feeling to dig out some code written several years ago.

After the big rewrite of Ted, my most successful app, as well as making a new icon set myself, I decided to rewrite my File Browser app to have some coherence between my utility applications

Felix - browsing and options

In the process, I also added some features who where missing for a 2012 app, like multiple selection (only available on Honeycomb and higher), or zip file management (archive, extract), or a grid view.

Felix - the grid view Felix - preview of image files

Now Felix is available in its latest version with a brand new look and feel, and more powerful than ever, and can be downloaded from the Play Store.

If you don't find Views by id, Views will find you

Written by Xavier Gouchet - 11 july 2012 - no comments

One of the first thing an Android developer has to learn, it's to match Views declared in an XML layout to fields in the Activity class. Something that looks like this, repeated for all the views you need to manipulate from your activity.


class MyActivity extends Activity {

    private Button myButton; 

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sample_views);
        myButton = (Button) findViewById(R.id.myButtonId);
    }
}

Now this can be quite troublesome, and could be automatized. So here it is, a class to help you fill in your fields. You can download it or fork it directly from GitHub.

The use can be pretty simple, and is just two lines, whatever the number of views you may have :


class MyActivity extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_layout);

        ViewFieldMatcher matcher = new ViewFieldMatcher(R.id.class);
        matcher.matchViews(findViewById(android.R.id.content), this);
    }

This code will automatically match any view to the corresponding field, matching the id name to the field's name.

Master On/Off Preferences with Ice Cream Sandwich

Written by Xavier Gouchet - 27 june 2012 - 5 comments

One of the easiest thing to do with android is storing user preferences. It can be as simple as a single xml files and a few lines of code. Since the first version of Android, you have access to several common preference widgets, such as checkboxes, text fields, or even lists.

In Android ICS, new widgets were introduced, among which the SwitchPreference, looking a lot like the iOS famous SwitchButton. If you have paid attention to the Android Design website, you have noticed that Google encourages you to use a Master On/Off switch in the way they used it with the Wifi Preference screen. Here's a reminder for those of you without an ICS device.

Android's Wifi Settings master On/Off Switch

The Wifi can be enabled from the first screen, using the Switch widget. And when you press the Wifi row, you land on the Wifi Preferences screen, where you can select a Wifi Network. At the top of the second screen, you still have a Switch widget to turn the Wifi off if you need to.

Now the question is : how did they do it ? Moreover, how can I do the same in my own preferences ? This is what we're gonna learn in this article. In a fictive video game, here's the preference screens we wan't to see. I'll limit the article to those two screens, others can be done easily after that. This code will only work with API level 14 or higher, so if you're targeting lower devices, be carefull and keep an old fashioned preference screen

The main preferences screen The sound preferences screen

For this article, I dug into the source code of Android's Settings application, so most of this is from Google. The probleme can be divided in two parts. First having the Switch widget at the top of the screen in the second screenshot. This is simply done by using a Switch view in the Custom View of the ActionBar. Then, there is the trickier part of having a preference linking to the second screen and having a Switch widget. You can usually have one or the other, using a PreferenceScreen or a SwitchPreference tag in your xml file. Here, we'll use the Header class introduce in ICS, with a custom adapter (basically almost like having a custom ListView, but with predefined behavior).

To create these screens, we need severall files, so instead of copy pasting all the code in here, I bundled a sample project available for download here.

Here's a list of the files you'll find in the archive, and a little description of what they do :

my_prefs_headers.xml
this file describes the headers of the first screen, meaning each row which links to another PreferenceScreen. Each header can have a title of course, an icon, an id, and a fragment class, defining the PreferenceFragment which will be loaded to display the PreferenceScreen.
MyPrefsActivity.java
the main PreferenceActivity, which loads first our headers, then will load a fragment when a header is clicked.
MyPrefsHeaderAdapter.java
this is the ArraAdapter which will load the Layouts we want for our headers. If you've already worked with ListViews, this should look familiar. This adapter uses 3 different layouts, one for categories sections, one for common header and one for switch header. The "magic" happens in the getHeaderType() method, in which we define which layout should be used with each header.
SoundEnabler.java
this class is mostly a utility we use to bind a Switch widget with the corresponding shared preference. It is linked with the switch defined in the ArrayAdapter described above.
SoundsPreferencesFragment.java
a PreferenceFragment which is used by our Sound header. Nothing much goes here, we mostly load the PreferenceScreen description from an XML file. Also, as described earlier, we create a Switch widget which we had to the ActionBar, and link it to a SoundEnabler instance.

Now, you have a preference screen with a Switch widget, and what's nice is that this trick can work with any other widget than a switch. You just need to create the layout, add it in the Array Adapter and Voila ! If you have any question on this article, please contact me

Download the sample code