This tutorial is based on http://developer.android.com/guide/topics/ui/notifiers/toasts.html .
A toast provides simple feedback about an operation in a small popup. It only fills the amount of space required for the message and the current activity remains visible and interactive. For example, navigating away from an email before you send it triggers a "Draft saved" toast to let you know that you can continue editing later. Toasts automatically disappear after a timeout.
The following steps is moderated from http://android-apps-blog.blogspot.com/2011/04/how-to-display-simple-toast-notificatin.html .
1) Run Eclipse.
2) Create a new Android Application Project. Type a name "SimpleToast". Click Next.
3. Accept Default Project Configuration. Click Next.
4. Accept Default Launcher settings. Click Next.
5. Select New Blank Activity. Click Next.
6. Accept Default Activity and Layout name. Click Finish.
7. Edit {project root}/srccom.example.simpletoast/MainActivity.java.
package com.example.simpletoast; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { private Button button; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.buttonToast); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Toast.makeText(getApplicationContext(), "Button is clicked", Toast.LENGTH_LONG).show(); } }); } }
8. Edit {project root}/res/layout/activity_main.xml .
Note: You may get warning message "[I18N] Hardcoded string "Show Toast", should use @string resource" for the statement "android:text="Show Toast" />". At this moment, ignore it.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/buttonToast" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show Toast" /> </LinearLayout>
No comments:
Post a Comment