Android App Development – Toast

Hello nerds, This tutorial is mainly intended for looking into some other concepts like GUI of Android development. The concept of “Toast” would be actually covered.

First you have to create HelloWorld App. After you’ve created your first HelloWorld app, its time for some additional tasks! (Make sure you’ve set your AVD already).

If some of you guys are wondering what a Toast is, well here’s the answer: Click!

Development :

Step 1: The first thing we are going to accomplish is changing the strings.xml (Path:- AppName > res > values > strings.xml) file to add another node under app_name. We will do this by copying the node above it and pasting the copied material directly under the last </string> element. Then we will change the name of the string to press and in between we will write Press Me!. Next we will alter the hello node and change the text to say Enter Your Name Here: instead of Hello Android, Hello World!.

Android App Development – Toast

Step 2: Next step, is to design the GUI (Graphical User Interface). To do this navigate to main.xml (Path:- AppName > res > layout > main.xml) and we are going to go over what everything does up to this point. Set your main.xml file as shown in the below picture.

Android Toast

Make sure you’ve set the Orientation as vertical, otherwise ie, if its horizontal maybe the GUI controls won’t be shown when the app is run.(in an HVGA Emulator, or maybe its me) Anyways you are free to toggle between vertical/horizontal and see what happens.

Note : Next step needs basic knowledge of java.

Step 3: Now this is a tricky step, and it includes Java code modifications. I suggest you to google to know exactly what all these codes means be it functions, classes, methods, objects or imports. You can refer the Wiki or the Oracle docs if you want to learn more about Java. Anyways for keeping this Tutorial simple, just modify the Java file (Path:- AppName > src > com.example.helloworld > HelloWorldActivity.java)  as shown below:

Code:

package com.example.HelloWorld;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class HelloWorldActivity extends Activity {
    private Button b;
    private EditText disp;
    private String getText;;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        b = (Button) findViewById(R.id.go);
        // To perform click operation on "Press me" Button
        b.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // TODO Auto-generated method stub
                disp = (EditText) findViewById(R.id.helloName);
                getText = disp.getText().toString();
                // to display toast
                Toast toast = Toast.makeText(getApplicationContext(), "Hello "
                        + getText + "!", Toast.LENGTH_SHORT);
                toast.show();
            }
        });

    }
}

don’t just copy-paste the respective code. If you run into massive errors or problems only, do that. Its better to type the codes by yourself and see what all AutoFill options/suggestions are given by Eclipse. Anyways try to correct the errors by yourself, it maybe only a spelling-mistake, but you have to identify it where.

Step 4: After doing all these above mentioned tasks, its time for the output. Be sure to click “Save All” (Ctrl+Shift+S) button in the Eclipse. Also make sure your Project is free from errors, otherwise it would not run. You can also clean your Project (Some errors maybe automatically fixed) by navigating to Project > Clean…

Right Click your Project > Run As > 1 Android Application
Your Emulator would start, and you’ll see in the Eclipse as apk installing, running etc..
If your Project is a Success, you’ll get the output as shown in the below picture:

toast Android

And that’s it
I hope you enjoyed this tutorial. Its made as simple as possible and omitted some theories from the Ori source. You can get to it, and see the xml parts explained.