Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,732,170 Members 43,631 Now Online
XDA Developers Android and Mobile Development Forum

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

Tip us?
 
coolsandie
Old
(Last edited by coolsandie; 20th August 2012 at 08:49 PM.) Reason: Title change
#1  
coolsandie's Avatar
Recognized Contributor - OP
Thanks Meter 974
Posts: 1,948
Join Date: Oct 2011
Location: Kochi, Kerala
Tutorial [TUT] Extend your HelloWorld App Step by Step with a Toast Message

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.
The Following 10 Users Say Thank You to coolsandie For This Useful Post: [ Click to Expand ]
 
coolsandie
Old
(Last edited by coolsandie; 26th June 2012 at 10:11 PM.) Reason: Link added
#2  
coolsandie's Avatar
Recognized Contributor - OP
Thanks Meter 974
Posts: 1,948
Join Date: Oct 2011
Location: Kochi, Kerala
After you have succeeded in this app, head over to next Tutorial : Create your First Widget Step by Step
The Following User Says Thank You to coolsandie For This Useful Post: [ Click to Expand ]
 
rete
Old
#3  
Senior Member
Thanks Meter 84
Posts: 258
Join Date: Nov 2009
Location: Canmore, AB
Thanks for this.
Tablets:
Nook Tablet (CM11 Kit Kat nightly)
iPad 3

Phones:
Blackberry Z10 (OS 10.2.1)
Google Nexus 4 (AOSP Kit Kat)
Samsung Galaxy SII (CM11 Kit Kat) ~ Backup phone
 
rezo609
Old
#4  
rezo609's Avatar
Senior Member
Thanks Meter 457
Posts: 357
Join Date: May 2012
Location: Dallas
This is great!!

Sent from my HTC Wildfire S using xda premium
 
coolsandie
Old
#5  
coolsandie's Avatar
Recognized Contributor - OP
Thanks Meter 974
Posts: 1,948
Join Date: Oct 2011
Location: Kochi, Kerala
Welcome guys, hope you guys tried/will try and get successful.
 
coolsandie
Old
#6  
coolsandie's Avatar
Recognized Contributor - OP
Thanks Meter 974
Posts: 1,948
Join Date: Oct 2011
Location: Kochi, Kerala
Tutorial now featured at XDA-Portal : Here

Thanks to the Author.
 
blazingwolf
Old
(Last edited by blazingwolf; 25th June 2012 at 02:58 AM.)
#7  
blazingwolf's Avatar
Senior Member
Thanks Meter 401
Posts: 2,101
Join Date: Nov 2006

 
DONATE TO ME
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.
Verizon Samsung Galaxy S4 (Rooted NC5), Nook HD (CM11)

In fire and EMS? Check out my apps on Google Play Pumper Calc and Code Log.
 
pmapma1
Old
#8  
Member
Thanks Meter 0
Posts: 39
Join Date: Oct 2010
Location: Pune
Quote:
Originally Posted by blazingwolf View Post
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....
Nokia 1100 >> SE K300i >> SE K550i >> SE W580i >> Samsung Galaxy 3 >> Samsung Galaxy S II
 
coolsandie
Old
#9  
coolsandie's Avatar
Recognized Contributor - OP
Thanks Meter 974
Posts: 1,948
Join Date: Oct 2011
Location: Kochi, Kerala
Quote:
Originally Posted by blazingwolf View Post
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.
 
coolsandie
Old
#10  
coolsandie's Avatar
Recognized Contributor - OP
Thanks Meter 974
Posts: 1,948
Join Date: Oct 2011
Location: Kochi, Kerala
Quote:
Originally Posted by pmapma1 View Post
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.

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

Advanced Search
Display Modes


XDA PORTAL POSTS

Run, Jump, and Shoot in Tales of Ciaxia

There are certain types of games that have become paramount of the portable gaming world. As we have … more

Samsung Gear Live Stock Firmware Image Pulled, Temp Root

Just yesterday,XDA Recognized Developer and TV ProducerAdamOutlertore apart … more

Draw a Line and Let Your Device Do the Rest with LineitApp

Over the years, since evenbefore Android entered the scene, we have striven to … more

[APK] Chrome Beta 37 Brings First Traces of Material Design

With Android L on the horizon, it’s no surprise that Google’s … more