✔ Customize Toast Massage 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.j4va 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 menentukan Toast", Toast.LENGTH_SHORT).show(); break; case R.id.exitBtn: exit(); break; } } private void munculListDialog() { tamat 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 obrolan = builder.create(); dialog.show(); } }
Explanation:
For coding Toast is like the following.
Toast.makeText(this, "Kamu menentukan 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 Sumber http://scqq.blogspot.com
0 Response to "✔ Customize Toast Massage On Android"
Posting Komentar