Android alert message with Toast

When you create Android applications, you will need to show alert messages to the user. In this tip, I will shows you how to display an alert message using Toast class. The Toast class is able to show an alert message dialog in a duration of time. Then the message disappears.


Now to see the Toast message dialog in action, you have to create a new Android project. Then in the onStart method of the MainActivity class write the single line of code as shown below.

      public void onStart(){
   super.onStart();
   Toast toast=Toast.makeText(this,"This is a message shown by Toast", Toast.LENGTH_LONG);
toast.show();
}

The makeText() method of the Toast class is used to create the alert message. It has three arguments: the current activity, message, and duration. Don't forget to call the show() method to display the message.



If you want to customize the color of the text or add an image to the alert message, you need to add custom view to an instance of Toast. There are two ways to achieve the goal. For one way, you can add the custom view programmatically to the instance of Toast. In the example code below, a LinearLayout layout is created. Then an ImageView and TextView are added to the layout. The ImageView shows an info icon that is stored in the res/drawable folder. The TextView shows the text "Toast custom message".

  public void onStart(){
   super.onStart();
   LinearLayout layout=new LinearLayout(this);
   layout.setBackgroundColor(Color.WHITE);
   ImageView image=new ImageView(this);
   image.setImageDrawable(this.getResources().getDrawable(R.drawable.info));
   TextView view=new TextView(this);
   view.setText("Toast custom message");
   view.setTextColor(Color.RED);
   layout.addView(image);
   layout.addView(view);
   Toast toast=new Toast(this);
   toast.setDuration(Toast.LENGTH_LONG);
   toast.setView(layout);
   toast.show();
}

Alternatively, you can define a LinearLayou and its elements (ImageView and TextView) in a separate xml file as shown below. This layout file is called alert_layout.xml.

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:id="@+id/toast_layout_root"
         android:orientation="horizontal"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:padding="8dp"
         android:background="#ffffff"
         >
  
   <ImageView android:id="@+id/image"
       android:src="@drawable/info"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginRight="8dp"
          
          />
      
   <TextView android:id="@+id/txtmessage"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:textColor="#FF0000"
         android:text="Toast custom message"
         />
</LinearLayout>


Then in the onStart method of the MainActivity class, you can use the layout to customize the message of Toast. In the example code below, you use the getLayoutInflater() to get an LayoutInflater object. Then you will use the inflate() method to inflate the layout. The inflate() method has two arguments: the layout file resource and the id of the layout root(LinearLayout).

      public void onStart(){
   super.onStart();
   LayoutInflater inflater = getLayoutInflater();
   View layout = inflater.inflate(R.layout.alert_layout,(ViewGroup) findViewById(R.id.toast_layout_root));    
   Toast toast=new Toast(this);
   toast.setDuration(Toast.LENGTH_LONG);
   toast.setView(layout);
   toast.show();
}

Android-alert-message-with-Toast

Posted by: Dara | post date: 05-14-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.