Develop Android Apps, how to code a framelayout
Next Tutorial
Coding Android Resource Identifiers

Prev Page
View All Tutorials


ANDROID - TOAST MESSAGE

For this tutorial, we are going to learn how to code a 'Toast Message'.

We are going to create a new app for this tutorial, and name is - AndroididToast



Where Code is Coded

This app will also have some text added(wording) so we will use our strings.xml file, for the text we add, and we also keep the 'app name' string. The app name is at the top of your screen next to the little green icon.
All string names are coded in the strings.xml file at res/values of the AppProject, and reference to the string name is made in the TextView element.

We will be adding some code to our layout page, main.xml, which is at res/layout/main.xml, our Button element, and a TextView element.

We will be adding some code to our java activity page, which is mainactivity.java which is at src/com/acw/learntoast/mainactivity.java

Creating Our New App
Create a Android app, choose java xml programming language

Open AIDE, Select New Project, from the left menu, or center menu hierachy of files; and do the following:

Choose HELLO WORLD example app template(java/xml);if your using an older version of AIDE, choose NEW ANDROID APP(java/xml) if your using the newest version of AIDE.
For app name; type AndroididToast

For app package name type: com.acw.learntoast

Click Create


Now your new app has been created with the name 'AndroididToast'.

This name will show on the left menu hierachy in AppProjects within AIDE.

To open your app just click on the app name(AndroididToast) on the left menu; then browse to, the androidmanifest.xml file at bottom of files, double click to open it. The file now shows in the AIDE coding editor.

To view A FILE do either: from right menu choose FILES or GOTO, this will show the apps files along the top of editor, dropdown, and left side menu. After editing a file, SAVE it, right side menu, choose SAVE.

Just double click a file to open it in the coding editor. Once file is opened they also show as tabs at top of editor. Just click file name to open it in the code editor.

view files in coding editor AIDE

To return to prev folder, just click on ORANGE colored FOLDER at top of files hierachy left side menu; to return to AppProjects (where you can see all apps you created) just click orange folder till you see AppProject, or from dropdown menu right side, or middle editor, choose, Show - View All Projects.

To build your app choose the arrow at top of AIDE editor; older version select right side menu (dots) from selections,Choose RUN.

Code the Strings.xml

We will add one line of text
"Use Toast Method To Display Text". We will name our string, learntoast, and type the text as shown.
coding a toast android, strings code

Our strings.xml file also has another string, our app_name,AndroididToast, which we will leave in the strings.xml as this is the name that shows at top of view and is required.

Once the text is added to the strings.xml file, save the file.
We will make reference to our string in our TextView element as shown here.

How To Implement a Toast Message Using XML(main.xml) and Java Code(MainActivity.java)
image of app, coding a toast message
In our strings.xml file we code our text for the app view as shown in the code in this image.
The code for the button using onClick, GoButtonClicked, is how we link our Button element to our java code.

coding a toast android, main.xml code

At the top of our MainActivity.java code you see our app package name.

We link our xml page to our java code using the setContentView code as seen in the code in this image. R.layout.main; R. means (the resource) layout (means the layout.xml page) and main means: main.xml.
coding a toast android, java code

After copy pasting the code into the appropriate pages: strings.xml, main.xml, and mainActivity.java Save each page.

CODING TIPS
In this tutorial, we learned how to code a toast message in our apps first java class page (MainActivity.java). We know that when we click on the button the toast message will show near the bottom of the app screen.

The duration of the toast message(time it remains visible in the view) is coded with one of two attribute values; either the Long which we used, and the other value you can use is Short.

You can also have the Toast Message appear anywhere within your app's view, by addng some additional code to our Toast code. Learn about coding these and additional Reading on 'Toasts", at the Android Developer website.
View Toasts Code At Android Developer Website

COPY/PASTE CODE

You can get the code from this tutorial from our EXAMPLE CODE section on this page. You can add the code easily to your apps coded pages using copy/paste. Just delete the code that's in your main.xml, MainActivity.java, and strings.xml files and replace it with the code from this tutorial. Save each page after pasting the code.

We also included the androidmanifest.xml file, however, this page only needs to be coded when adding certain functionalites or themes to your app. For this tutorial we did not need to change or add code to it.


Additional Reading on Creating a Toast Message at Android Developer website
Gohere and Reading on android:id value, and Accessing Resources in Code
Coding Resources android:id in layout file
How Tos - Accessing Resources


EXAMPLE CODE - Code For This Tutorial.

JUST copy the code for this tutorial App Name is 'AndroididToast - located at AppProjects/AndroididToast in AIDE, open the AIDE app, then open App Projects, THEN select the 'AndroididToast', app from your files hierachy(left menu); then click on any file from the app, like strings.xml Make sure at top of page, the app name 'AndroididToast' is there next to AppProjects like so: AppProjects/androidIdToast, then you know you have the proper App.

Once a file is in the editor, you can edit, save it, then RUN your APP, install, Open your app.


Make sure you SAVED EACH PAGE, before you RUN your code. SAVE, RUN, INSTALL, OPEN, your AndroididToast App. Once it has been installed on your tablet, you can open it Simply by Clicking on the Android little green App Icon,with the wording 'AndroididToast' on your tablet. It will be with your other installed apps.

If you make further changes to this App, you need to SAVE, RUN, UPDATE, INSTALL, OPEN the app.

Example Code:
Use Mouse To Copy Paste the Code




This free script provided by
JavaScript Kit

Share This Page


AIDE for creating Android Apps on your tablet or cell phone(android).
Free and Paid.
GoTo Aide

ANDROID STUDIO - for creating Android Apps on your desktop computer.
Free to Install and Use
Learn more - Android Studio

The Java XML files we code(example codes) and the Android Apps we create in our Free Tutorials are compatible with Android Studio and AIDE. Just choose Java XML as your Programming Language.


You May Like:
Make Sushi Rolls Taste World Wines Flavours and Aromas


AndroidAppCoding.com, All Rights Reserved