[Tuto] Create an Android UI programmatically

Introduction

Sometimes we need to create an interface programmatically to computerize some processing like create a form dynamically. It’s the subject of this post. You can found above an example.

Let’s Go !

Objective

We begin with a simple XML file that contain a RelativeLayout and we add programmatically a button at the center.

We will finish with a result that equal to this :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:id="myRelativeLayout">
    <Button android:id="myButton" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />
</RelativeLayout>

How ?

Like that :

public class MaClasseIHM{
    private RelativeLayout myRelativeLayout;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        myRelativeLayout = (RelativeLayout)findViewById(R.id.myRelativeLayout);

        // We create a button
        Button myButton = new Button(this);

        // The LayoutParams class permit to setting our element (the button)
        // Here the height and length
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        // Here we center myButton
        layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);

        // And to finish, we add the button at our RelativeLayout with layoutParams
        myRelativeLayout.addView(myButton, layoutParams);
    }
}

You have now all elements to create a more complex UI.

To go further

You know now how to create a button. It’s not very usefull but it can become usefull if you need to create a lot (one hundred for example) through the loops ! :)

If you need to create a more complex interface like a form, you can create a class that extends RelativeLayout. After it, you can create easyly a lot a of this new element by instanciate it.

If you have any problem, don’t forget the android documentation or comment this post :)

CrackScreen

CrackScreen is an application  that simulate a broken screen. A very classic application like a lot on the Android Market.

At the beginning I have not create this app to publish it, it was just a simple technic challenge to learn new things on Android. My main interrogation was to know how obtain a translucid background to simulate the broken screen. When I have aquired the answer, I have abort that application. Few week later, I have continued the development to share that app with you. Now, CrackScreen has a lot of downloading and I think it will exceed ICE in 4 or 5 month.

To download it : https://market.android.com/details?id=com.alexyu.android.crack

Hangman

A good surprise…

This game is a classic Hangman like you played in your childhood. Add a polished design, a few thousand of words, and we have something quite compelling.

Currently, I have published this game since more or less 1 month on the Android Market and this is an agreable suprise. More than 1000 download for the first day, it is a good beginning. To be honnest, I never thought that Hangman have this success. Today, it has between 7000 and 10000 downloads per day and a total of 130 000 downloads in 1 month. I think this is impressive statistics.

Today, because of Hangman, I’ll have my own company in january. But be carefull, all is not won, I have again a lot of work. Wait and see :)

To play Hangman : https://market.android.com/details?id=com.alexyu.android.pendu

Escape Square

Update 12/10/2011 : I stopped the escape square development for this moment.

Escape Square is my first Android Game. It was for me a first attempt , a means to launch me in the « game world ». I thinks the result is rather convincing and it showed to me that I could create a lot of things !

In mind, Escape Square is very near from Tilt To Live (iPhone), graphically it’s like Geometry Wars. Add to it my ideas, we have Escape Square !

Your goal in Escape Square is to survive as long as possible by eliminating your ennemies. Thus, you gain more and more points throughout of the game.

Be carefull, your ennemies will be more and more numerous to kill you.

Currently in the game:

  • 2 game modes
  • Save your scores online
  • View others scores online
  • Three bonus attack and protect

Coming soon :

  • 6 new bonus with combo of existing bonus
  • Save your pseudo online
  • Patterns of ennemies
  • A third game mode
  • A lot of things…

To download the free version it’s here, to the full version it’s here.

Have fun !

Alexyu

ICE : Emergency Contacts

I will present my second application, something more serious than « La classe américaine » and it can be really usefull. this is my step-brother that gave me this idea. Today, I have just reach 7500 downloads (Update 12/10/2011 : 46000) in just over a month. I take this opportunity to do some ads :p

This application use the ICE program (In case of emergency), that permit to call rescues and contacts in one touchscreen « In case of emergency ». Hope that you never need to use my application ! Below, the Android Market description :

« ICE: Emergency Contact » is an application that helps people who have problem or who are victim of accidents. It is based on the ICE program that consists in saving a contact who is named « ICE » in your contact list. This contact has medical informations about you.

In this application, you can save useful informations for rescue workers (allergy, medication, condition, identity, organ donor, blood type …) and contact people « in case of emergency ».

Thus, with only one click on your screen, send SMS alerts to all your saved contacts and call rescue workers immediately ! Your contacts will know where you are and will be able to help you !

Place a widget on your homescreen to alert your circle of contacts (compatible with alternative lockscreens like ScreenLocker).

This application was developped from the advice of an ambulance driver.

List of permissions :
- Send SMS to contact your saved contacts when you need it
- Access your contact list to choose contacts
- Get GPS coordinates to help your saved contacts to find you in case of emergency

Private informations saved in your application belong exclusively to you and are only accessible by you on your Androphone.

This application exists in two versions :

  • Free version, limited at two contacts (here)
  • Paid version, free of ads and without limitations (here)

For this applciation I have used a lot of news features that I have never used :

  • Use GPS
  • Send SMS
  • Access to user contacts
  • Call contact
  • Create widget

If you need help for a feature from this application, post a comment, I’d do a tutorial about it’s implementation.

Alexyu

La classe américaine

In this article I’ll present quickly my first Android application. This apps named « La classe américaine » is a free app only for french people that play sound of a french movie.

It’s not very impressive, but it’s only a joke with my friends. Thanks to this joke, I have begin personnal development for Android Market (Before, I developed only in professional workplace).

With this project I have discovered some concepts that I have never used before:

  • Different way to manage sound
  • Use of the accelerometer to detect « shake » of the androphone

May be I’ll write an article to explain how to use this two concept. If you are interested, please let me know.

Alexyu

Opening of the blog, my life, etc.

What this blog ?

This is for full of stuff, it’s to expose my word, my projects, but it will be surely a place where I write about what goes through my head. Finally, may be I’m not a good pedagogue but I could share my knowledge and experience in programming, mainly about Android platform by Google.

 

Who am I ?

Since a few time, I have a Master MIAGE, I am graduated at Evry College (France). I am specialize in mobile technologies and I developed a lot of applications on these platforms :

  • iOS
  • Android
  • Windows Phone 7

I like particulary Android and hope it’s because of Android that I can create my own compagny.

Occasionnaly, I use php and asp.NET/C# languages.

I would do my best to make this blog interesting.

To follow me, go to http://twitter.RAlexyu.

 

Good reading,

Alexyu