Welcome to XDA

Search to go directly to your device's forum

Register an account

Unlock full posting privileges

Ask a question

No registration required
Post Reply

[TUT] Extend your HelloWorld App Step by Step with a Toast Message

OP coolsandie

20th June 2012, 11:28 PM   |  #1  
coolsandie's Avatar
OP Recognized Contributor
Flag Kochi, Kerala
Thanks Meter: 1,011
 
1,953 posts
Join Date:Joined: Oct 2011
More
Hi guys, 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 do this (Create a HelloWorld app) : [TUT] Making a HelloWorld App Step by Step w/pictures. - Tutorial created by rezo609

After you've created your first HelloWorld app, its time for some additional tasks!
(NOTE:- Make sure you've set your AVD already)

I know some of you guys are wondering what a Toast is, well here's the answer: Click Me!


Starting development :


Quote:

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!.


Quote:

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.



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.


Quote:

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 in the below picture.




I'll also give it as CODE, but don't just copy-paste. 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.

Code:
package com.example.helloworld;

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


public class HelloWorldActivity extends Activity {

EditText helloName;
    
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
        
// Capture our button from layout
Button button = (Button)findViewById(R.id.go);
        
// Register the onClickListener with the implementation above
button.setOnClickListener(maddListener);
}
    
    // Create an anonymous implementation of OnClickListener
    private OnClickListener maddListener = new OnClickListener() {
    	public void onClick(View v) {
    		long id = 0;
    		// Do something when the button is clicked
    		try {
    		
    		helloName = (EditText)findViewById(R.id.helloName);
    		Context context = getApplicationContext();
    		CharSequence text = "Hello " + helloName.getText() +"!";
    		int duration = Toast.LENGTH_LONG;
    		Toast toast = Toast.makeText(context, text, duration);
    		toast.show();
    		}
    		
    		catch(Exception e) {
    		
    		Context context = getApplicationContext();
    		CharSequence text = e.toString() + "ID = " + id;
    		int duration = Toast.LENGTH_LONG;
    		Toast toast = Toast.makeText(context, text, duration);
    		toast.show();
    		}
    	}
    };
}


Quote:

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:

And that's it
I hope you enjoyed this tutorial. Its made as simple as possible and omitted some theories from the Original source. You can get to it, and see the xml parts explained.
Last edited by coolsandie; 20th August 2012 at 09:49 PM. Reason: Title change
The Following 11 Users Say Thank You to coolsandie For This Useful Post: [ View ]
20th June 2012, 11:29 PM   |  #2  
coolsandie's Avatar
OP Recognized Contributor
Flag Kochi, Kerala
Thanks Meter: 1,011
 
1,953 posts
Join Date:Joined: Oct 2011
More
After you have succeeded in this app, head over to next Tutorial : Create your First Widget Step by Step
Last edited by coolsandie; 26th June 2012 at 11:11 PM. Reason: Link added
The Following User Says Thank You to coolsandie For This Useful Post: [ View ]
21st June 2012, 04:31 PM   |  #3  
Senior Member
Flag Canmore, AB
Thanks Meter: 84
 
259 posts
Join Date:Joined: Nov 2009
More
Thanks for this.
21st June 2012, 11:11 PM   |  #4  
rezo609's Avatar
Senior Member
Flag Dallas
Thanks Meter: 473
 
363 posts
Join Date:Joined: May 2012
More
This is great!!

Sent from my HTC Wildfire S using xda premium
22nd June 2012, 01:32 PM   |  #5  
coolsandie's Avatar
OP Recognized Contributor
Flag Kochi, Kerala
Thanks Meter: 1,011
 
1,953 posts
Join Date:Joined: Oct 2011
More
Welcome guys, hope you guys tried/will try and get successful.
24th June 2012, 06:20 PM   |  #6  
coolsandie's Avatar
OP Recognized Contributor
Flag Kochi, Kerala
Thanks Meter: 1,011
 
1,953 posts
Join Date:Joined: Oct 2011
More
Tutorial now featured at XDA-Portal : Here

Thanks to the Author.
25th June 2012, 03:39 AM   |  #7  
blazingwolf's Avatar
Senior Member
Thanks Meter: 402
 
2,100 posts
Join Date:Joined: Nov 2006
Donate to Me
More
So I decided to look at this. I've got everything as you have above but I have errors.

Current errors are in the following lines:

Code:
Button button = (Button)findViewById(R.id.go);
Code:
helloName = (EditText)findViewById(R.id.helloName);
The error states: id cannot be resolved or is not a field

If I follow the listed fixes it places lines in the R.java. However, I then get errors on go and helloName for which there are no listed fixes.

Still looking to see if I can find it myself but wanted to tell you about this to see if it was just me (probably) or a missing section in the info above.

EDIT: Sigh. It's amazing what missing one line will do to you. This was my fault. Forgot to add the Press me string and it created these errors. Working great now.
Last edited by blazingwolf; 25th June 2012 at 03:58 AM.
25th June 2012, 11:37 AM   |  #8  
Member
Flag Pune
Thanks Meter: 0
 
39 posts
Join Date:Joined: Oct 2010
More
Quote:
Originally Posted by blazingwolf

So I decided to look at this. I've got everything as you have above but I have errors.

Current errors are in the following lines:

Code:
Button button = (Button)findViewById(R.id.go);
Code:
helloName = (EditText)findViewById(R.id.helloName);
The error states: id cannot be resolved or is not a field

If I follow the listed fixes it places lines in the R.java. However, I then get errors on go and helloName for which there are no listed fixes.

Still looking to see if I can find it myself but wanted to tell you about this to see if it was just me (probably) or a missing section in the info above.

EDIT: Sigh. It's amazing what missing one line will do to you. This was my fault. Forgot to add the Press me string and it created these errors. Working great now.


please check the imports ...
if you find line import android.R; ... remove it and then clean build....
25th June 2012, 02:41 PM   |  #9  
coolsandie's Avatar
OP Recognized Contributor
Flag Kochi, Kerala
Thanks Meter: 1,011
 
1,953 posts
Join Date:Joined: Oct 2011
More
Quote:
Originally Posted by blazingwolf

EDIT: Sigh. It's amazing what missing one line will do to you. This was my fault. Forgot to add the Press me string and it created these errors. Working great now.

There you are
Actually we should edit all the XML files first like android:id="@+id/go" and it will show error in XML file for sure (Because id can't be found anywhere) but finally when you code the java file, and when the id is referenced, all errors will be gone

Anyways the R.java file can't be modified manually. It will revert back to original state if you do so, that is even if you apply the suggested fixes by Eclipse.
25th June 2012, 02:44 PM   |  #10  
coolsandie's Avatar
OP Recognized Contributor
Flag Kochi, Kerala
Thanks Meter: 1,011
 
1,953 posts
Join Date:Joined: Oct 2011
More
Quote:
Originally Posted by pmapma1

please check the imports ...
if you find line import android.R; ... remove it and then clean build....

Actually unnecessary imports will not cause the application to malfunction. It will only use more resources based on program. Eclipse will give a warning to remove unused imports, as its not used anywhere.

Post Reply Subscribe to Thread

Tags
android, app, application, development, toast
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes