Skip to main content

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

Popular posts from this blog

Android Popup Window Example

activity_main.xml <?xml version="1.0" encoding="utf-8"?> <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="16dp" tools:context=".MainActivity" android:background="#f5f1e0" > <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show Popup Window" /> </RelativeLayout> res/layout/custom_layout.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rl_custom_layout" …

How to add a hint to Spinner 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="#60c4ff" > <Spinner android:id="@+id/spinner" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout> spinner_item.xml <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:textStyle="italic" …

How to create a round/circle Button in Android

activity_main.xml code snippet <Button android:id ="@+id/push_button" android:layout_width="150dp" android:layout_height="150dp" android:text="Round Button" android:background="@drawable/button_bg_round" android:padding="15dp" /> res/drawable/button_bg_round.xml <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="oval"> <stroke android:color="#1E90FF" android:width="5dp" /> <solid android:color="#87CEEB"/> <size android:width="150dp" android:height="150dp"/> </shape> </item> </selector> More android examples How to set Button fontHow to change Button font programmaticallyHow to set Button text style to bold and italic…