Aplicação Android

Creating an Android App – Hello World

The market is increasingly immersed in mobile programming, and the Android application is one of the best viewed today, so it is very advantageous to know it.

Building the first Android App

With Android Studio running, go to file > new project

At Application Name, put the name of the application, in this case, let’s put TotalCrossEnsina. Done that just click next until you reach the Activity type, then select Empty Activity, then click next and Finish.

Soon, your environment will be ready. As you can see, there are two ways to build your interface, through the XML itself and through the feature offered by the AndroidStudio, the design part, where you will assemble your activity. For the proposed example, we will need just a Button, drag it to where you want to appear to the user, done that, change the id to btnHello.

In MainActivity.java, create a Button (private Button btnHello) and inside the method onCreate() copy and paste the following commands:

btnHello = (Button) findViewById(R.id.btnHello);
btnHello.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
        alert.setTitle("Totalcross Ensina!");
        alert.setMessage("Hello World");        
        alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int i) {

And voilà! There is your first Android App.

Generating the APK

To run on your device, just connect it to your computer and conclude the following steps:

  1. At Tool Bar, put your mouse over Build and click on Build Apk.
  2. And wait, when it generates will show up a blue link in the lower right corner, written “show in exploring“, click it to go to the place where the apk file was generated.
  3. Put the generated apk on your device.
  4. Install the app on your device.
  5. Complete your first Android App!

Another way to see your first application running in your device without plug it at your computer is through the website Diawi, where you can do the upload of your apk and it will be available to download during 10 days.
Then you will have access to it on your device by download or by the QR Code.

Aplicação android

An Alternative: Cross Platform

Aplicação android-TC

One of the biggest problems of programming uses a native platform, so, your Android App just will running on devices whose the operating system is Android. An alternative to bypass this question would use a Cross-platform, in this way, You will program it once and it will work on any operating system.

Totalcross, being a Cross tool that uses Java, can generate applications for various operating systems and is an easy and accessible language, so let’s now create an example using it to better understand these differences.

Creating an Android Application with TotalCross

The Totalcross is a JAR, then download the SDK from the site and after this import it at your project using an IDE (Netbeans, Eclipse and etc) use which you prefer.

After configuring your environment, let’s create a class HelloTC. This will be our main class, so it should inherit from totalcross.ui.MainWindow:

public class HelloWorld extends MainWindow {

Create too a Button called btnHello, a Bar entitled barTC and a Label lbl:

private Button btnHello;
private final Bar barTC;
private Label lbl;

Inside the constructor class, we should specify some information, like the style of the interface, background color and border type and, in this case, must attribute the characteristics of the Bar, like the size, the font color, and the background color:

public HelloWorld()throws Throwable {
   super("", BORDER_LOWERED);
   Font f = Font.getFont(true,Font.NORMAL_SIZE+4);
   barTC = new Bar("TotalCross Ensina");
   barTC.canSelectTitle = true;
   setBackForeColors(Color.getRGB(240, 240, 240), Color.getRGB(50, 50, 50));
   Settings.uiAdjustmentsBasedOnFontHeight = true;

And to instantiate the elements of the screen, we use the initUI() method:

public void initUI() {
   add(lbl = new Label("TotalCross Ensina"), CENTER+20, TOP+650, PARENTSIZE+63, PARENTSIZE+22);
   add(btnHello = new Button("Hello"), CENTER, CENTER+10, PARENTSIZE+38, PARENTSIZE+8);

Finally, to create the event that will be triggered when the user clicks on the Button, we use onEvent():

public void onEvent(Event e) {
   if(e.type == ControlEvent.PRESSED && e.target == btnHello){
       MessageBox mp = new MessageBox("TotalCross Ensina\n", "Hellow World!");

Aplicação android

And it’s ready an application that will run on any device, be IOS, Android, Windows Desktop, Windows CE or Linux.

To do the complete download of the example, just click here. And to see more samples with the TotalCross, just go to GitHub.

Check it out! Totalcross it’s simple, easy, beautiful and still saves time!

Aplicação Android

Esse artigo foi útil?
[Total: 0 Average: 0]
Share on FacebookTweet about this on TwitterShare on LinkedInShare on Google+Email this to someone