Monday, August 17, 2015

How to use Toast.makeText in Android

activity_main.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    tools:context=".MainActivity"
    android:background="#a2caa4"
    >
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Toast"
        />
    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Make Text Using String Resource"
        android:layout_toRightOf="@id/btn"
        />
</RelativeLayout>
res/values/strings.xml

<resources>
    <string name="app_name">Android Example : Toast.makeText</string>
    <string name="action_settings">Settings</string>

    <!-- String Resource for Toast message -->
    <string name="toast_formatted">
        <b>This</b> <i>is a \n multiline</i> <u>formatted Toast</u>
    </string>
</resources>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.os.Bundle;
import android.app.Activity;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get reference of widgets from XML layout
        final RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
        Button btn = (Button) findViewById(R.id.btn);
        Button btn2 = (Button) findViewById(R.id.btn2);


        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // Make the Toast message
                /*
                    public static Toast makeText (Context context, CharSequence text, int duration)
                        Make a standard toast that just contains a text view.

                    Parameters
                        context : The context to use. Usually your Application or Activity object.
                        text : The text to show. Can be formatted text.
                        duration : How long to display the message. Either LENGTH_SHORT or LENGTH_LONG

                 */
                final Toast toast = Toast.makeText(
                        getApplicationContext(), // Context
                        "Simple Toast", // Message
                        Toast.LENGTH_SHORT // Short Duration
                );
                // Show the Toast on center
                toast.setGravity(Gravity.CENTER,0,0);

                // Show the Toast on app interface
                toast.show();
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // Make the Toast message
                /*
                    public static Toast makeText (Context context, int resId, int duration)
                        Make a standard toast that just contains a text view with the text from a resource.

                    Parameters
                        context : The context to use. Usually your Application or Activity object.
                        resId : The resource id of the string resource to use. Can be formatted text.
                        duration : How long to display the message. Either LENGTH_SHORT or LENGTH_LONG

                    Throws : Resources.NotFoundException
                        if the resource can't be found.
                 */
                final Toast toast = Toast.makeText(
                        getApplicationContext(), // Context
                        R.string.toast_formatted, // Message from string resource
                        Toast.LENGTH_SHORT // Short Duration
                );
                // Show the Toast on center
                toast.setGravity(Gravity.CENTER,0,0);

                // Show the Toast on app interface
                toast.show();
            }
        });
   }
}


More android examples