Here you will learn how to create custom toast in android with some text and image.
Toast is used to show some information for specific time period. In android we use android.widget.Toast class to make toast message.
A normal toast message with some text can be created by following code.
//toast message for short time period Toast.makeText(getApplicationContext(), "Simple Toast example.", Toast.LENGTH_SHORT).show(); //toast message for long time period Toast.makeText(getApplicationContext(), "Simple Toast example.", Toast.LENGTH_LONG).show();
Android provide facility to customize toast message. Like if we want a toast message with some text and image. It can be done in following way.
Android Custom Toast Example
1. Create an android project with package name thecrazyprogrammer.androidexample
2. Add an image in res/drawable folder. In this example I have used logo.png.
The project has following structure.
Add following code in respective files.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Click Here To Open Custom Toast" android:onClick="clickAction" android:id="@+id/btn"/> </LinearLayout>
custom_toast.xml
It is layout file that contains the code for custom toast.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ImageView android:layout_width="200dp" android:layout_height="100dp" android:src="@drawable/logo" android:layout_gravity="center"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="The Crazy Programmer" android:gravity="center"/> </LinearLayout>
MainActivity.java
package thecrazyprogrammer.androidexample; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = (Button)findViewById(R.id.btn); } public void clickAction(View view){ LayoutInflater layoutInflater = getLayoutInflater(); View view1 = layoutInflater.inflate(R.layout.custom_toast,null); Toast toast = new Toast(getApplicationContext()); toast.setView(view1); toast.setGravity(Gravity.CENTER_VERTICAL,0,0); toast.setDuration(Toast.LENGTH_LONG); toast.show(); } }
Here we are simply inflating the custom toast xml into Toast view. When you click on button a custom toast with text and image will pop up for some time. You can also do further customization like changing the text size, color, background, etc.
Output
If you are facing any difficulty then you can ask it in comment section.
can i get the basics of android programming
You can find he tutorials at below link:
http://thecrazyprogrammer.com/learn-android-programming