Create/Show custom toast message in android with Layout using XML

How to make new style fully customizable toast message with background color change,font change, text color change.

In this tutorial we are showing completely fully customize toast message which style format is changed by external toast message style XML layout file. We are simply creating toast message in this tutorial on button click event inside that we are setting up or custom made toast_message_style.xml file as toast message background. So here is the complete step by step tutorial for Create/Show custom toast message in android with Layout using XML.

android-project-download-code-button

How to Create/Show custom toast message in android with Layout using XML.

Code for MainActivity.java file.

 package com.customtoastmessage_android_examples.com;

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

public class MainActivity extends Activity {

 Button button;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 button = (Button)findViewById(R.id.button1);
 
 button.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 
 Toast toast = Toast.makeText(MainActivity.this,"Custom Toast Message",Toast.LENGTH_LONG);
 
 View toastView = toast.getView();
 
 toastView.setBackgroundResource(R.layout.toast_message_style);
 
 toast.show();
 
 }
 });
 
 }
}

Code for activity_main.xml layout file.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.customtoastmessage_android_examples.com.MainActivity" >

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="Click here to Show custom toast message in android" />

</RelativeLayout>

Code for toast_message_style.xml file.

 <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
 <shape android:shape="rectangle">
 <solid android:color="#009973"/>
 <corners android:radius="20dp" />
 <stroke
 android:width="4dp"
 android:color="#01ffc0"
 />
 </shape>
</item>
</selector>

Screenshot:

Create/Show custom toast message in android with Layout using XML

Click here to download Create/Show custom toast message in android with Layout using XML project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *