Customize Toast Massage on Android

how to customize widget Toast on Android.

Customize Toast Massage on Android

In this discussion we will discuss the customization of Toast messages on Android, Toast is an event where if we click on a button or another in our android application, a message will appear on our screen as below.


Let's just go into the discussion. First, first make your android project named TOAST. After that, create an activity with type Empty Activity with the name MainActivity. After that, open activity_main.xml and enter the following code.


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

<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/toastBtn"
        android:layout_width="170dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:layout_marginTop="15dp"
        android:background="@drawable/drawable"
        android:text="@string/toasTextBtn" />

    <Button
        android:layout_width="170dp"
        android:layout_height="wrap_content"
        android:id="@+id/listDialogBtn"
        android:text="@string/listdialogTextBtn"
        android:layout_marginBottom="20dp"
        android:background="@drawable/drawable"/>

    <Button
        android:layout_width="170dp"
        android:layout_height="wrap_content"
        android:id="@+id/exitBtn"
        android:text="@string/exitTextBtn"
        android:layout_marginBottom="20dp"
        android:background="@drawable/drawable"/>
</LinearLayout>



Then open MainActivity.java and enter the following code in it.


package com.inspirasijones.trojan.buttonexit;


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.DialogInterface;
import android.app.AlertDialog;
import android.app.Activity;
import android.widget.Button;
import android.widget.Toast;
import android.view.View;

public class MainActivity extends Activity implements View.OnClickListener {
    Button pesanToast;
    Button keluar;
    Button tampilList;


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



        pesanToast = (Button)findViewById(R.id.toastBtn);
        pesanToast.setOnClickListener(this);

        keluar = (Button) findViewById(R.id.exitBtn);
        keluar.setOnClickListener(this);
        tampilList = (Button)

                findViewById(R.id.listDialogBtn);
        tampilList.setOnClickListener(this);
    }

      public void onClick(View clicked) {
        switch (clicked.getId()) {
            case R.id.listDialogBtn:
                munculListDialog();
                break;
                case R.id.toastBtn:
                    Toast.makeText(this, "Kamu memilih Toast",
                            Toast.LENGTH_SHORT).show();
                    break;
                    case R.id.exitBtn:
                        exit();
                        break;
        }
    }
     private void munculListDialog() {
         final CharSequence[] items = { "Es Teh", "Es Jeruk",
                 "Lemon Squash","Soft Drink" };

           AlertDialog.Builder kk = new AlertDialog.Builder(this);
           kk.setTitle("Pilih Minuman");
           kk.setItems(items, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int item) {
                   Toast.makeText(getApplicationContext(), items[item],
                           Toast.LENGTH_SHORT).show();
               }
           }).show();
    }
    private void exit() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setMessage("Apakah Kamu Benar-Benar ingin keluar?")
                .setNegativeButton("Tidak", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .setPositiveButton("Ya", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        finish();
                    }
                });

            AlertDialog dialog = builder.create();
            dialog.show();
    }
}


Explanation:
For coding Toast is like the following.


Toast.makeText(this, "Kamu memilih Toast",
                            Toast.LENGTH_SHORT).show();

And the results will be as follows.






Please learn and practice it to better understand. thank you.
Don't forget to visit our channel on Youtube. @Harison Matondang.

Author : Nur Muhammad Erji Ridho Lubis
Website : www.portalcoding.com
Theme : Programming tutorials

COMMENTS

Next Recomended


Feel free to code it up and send us a pull request.

Hi everyone, let's me know how much this lesson can help your work. Please Subscribe and Follow Our Social Media 'kodeajaib[dot]com' to get Latest tutorials and will be send to your email everyday for free!, Just hit a comment if you have confused. Nice to meet you and Happy coding :) all ^^



Follow by E-Mail


Name

ADO.NET,3,Ajax,6,Android,9,AngularJS,4,ASP.NET,4,Blogger Tutorials,7,Bootstrap,7,C++,1,Codeigniter,2,Cplusplus,6,Crystal Report,6,CSharp,25,Ebook Java,2,FlyExam,1,FSharp,3,Game Development,2,Java,35,JDBC,2,Laravel,84,Lumen,2,MariaDB,2,Ms Access,3,MySQL,31,ODBC,6,OleDB,1,PHP,14,PHP Framework,5,PHP MYSQLI,9,PHP OOP,5,Python,8,Python 3,4,SQL Server,4,SQLite,4,Uncategorized,5,Vb 6,2,Vb.Net,89,Video,48,Vue Js,4,WPF,2,Yii,3,
ltr
item
KODE AJAIB: Customize Toast Massage on Android
Customize Toast Massage on Android
how to customize widget Toast on Android.
https://3.bp.blogspot.com/-37otgU4h5WE/W8G_Q0MNFFI/AAAAAAAAAC4/d1FQuFJy2aI_IV75phCTQ1Kbk9aDL4_5QCLcBGAs/s320/Customize-Toast-Massage-on-Android-1.PNG
https://3.bp.blogspot.com/-37otgU4h5WE/W8G_Q0MNFFI/AAAAAAAAAC4/d1FQuFJy2aI_IV75phCTQ1Kbk9aDL4_5QCLcBGAs/s72-c/Customize-Toast-Massage-on-Android-1.PNG
KODE AJAIB
https://www.kodeajaib.com/2018/10/customize-toast-massage-on-android.html
https://www.kodeajaib.com/
https://www.kodeajaib.com/
https://www.kodeajaib.com/2018/10/customize-toast-massage-on-android.html
true
3214704946184383982
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy