Custom Toast on Android: a Simple Example

Custom toast on Android: a simple example

Use the below code of a custom Toast. It may help you.

toast.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout_root"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:background="#DAAA" >

<ImageView android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="10dp" />

<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textColor="#FFF" />

</LinearLayout>

MainActivity.java

LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_layout,
(ViewGroup) findViewById(R.id.toast_layout_root));

ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.android);
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello! This is a custom toast!");

Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();

And check out the below links also for a custom Toast.

Custom Toast with Analog Clock

YouTube: Creating Custom Toast With Button in Android Studio

How to create a custom toast

You need to do it this way:

private void showToast() {
View view = getLayoutInflater().inflate(R.layout.ctoast_view,null);
toastTextView = (TextView) view.findViewById(R.id.textView1);
toastTextView.setText("message!!");

Toast mToast = new Toast(getApplicationContext());
mToast.setView(view);
mToast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
mToast.setDuration(Toast.LENGTH_LONG);
mToast.show();
}

Inflate the toast content view and get the TextView from it when you create the toast.

Customview like Toast

Its really easy you can do it by extending Toast class

Let say My Custom Toast calss is NexoolCustomToast which extends Toast class. Below is the code of custom Toast Class.

   import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

/**
* Created by Abhishek on 24-03-2017.
*/

/**
* By Default shows Error i.e. Fail toast
* */
public class NexoolCustomToast extends Toast {

private Context mContext;

private View mView;

private LayoutInflater mLayoutInflater;

private TextView mTitleTextView, mMessageTextView;

/**
* Construct an empty Toast object. You must call {@link #setView} before you
* can call {@link #show}.
*
* @param context The context to use. Usually your {@link Application}
* or {@link Activity} object.
*/
public NexoolCustomToast(Context context) {
super(context);
mContext = context;
mLayoutInflater = LayoutInflater.from(context);
mView = mLayoutInflater.inflate(R.layout.nexool_fail_custom_toast, null);
initialiseView(mView);
setView(mView);
setGravity(Gravity.TOP | Gravity.END, 0, 0);
}

public NexoolCustomToast(Context context, boolean state) {
super(context);
mContext = context;
mLayoutInflater = LayoutInflater.from(context);
if (state) {
mView = mLayoutInflater.inflate(R.layout.nexool_success_custom_toast, null);
} else {
mView = mLayoutInflater.inflate(R.layout.nexool_fail_custom_toast, null);
}
initialiseView(mView);
setView(mView);
setGravity(Gravity.TOP | Gravity.END, 0, 0);
}

private void initialiseView(View mView) {

mTitleTextView = (TextView) mView.findViewById(R.id.titleTextView);

mMessageTextView = (TextView) mView.findViewById(R.id.messageTextView);

}

public void setTitle(String title) {

if (title != null && title.length() != 0) {

mTitleTextView.setText(title);

} else {

mTitleTextView.setVisibility(View.GONE);

}

}

public void setMessage(String message) {

if (message != null && message.length() != 0) {

mMessageTextView.setText(message);

} else {

mMessageTextView.setVisibility(View.GONE);

}

}

@Override
public void show() {
super.show();
}

@Override
public void cancel() {
super.cancel();
}

public static NexoolCustomToast makeText(Context mContext, String mTitle, String mMessage) {
NexoolCustomToast mNexoolCustomToast = new NexoolCustomToast(mContext);

mNexoolCustomToast.setTitle(mTitle);

mNexoolCustomToast.setMessage(mMessage);

return mNexoolCustomToast;
}

public static NexoolCustomToast makeText(Context mContext, String mTitle, String mMessage, boolean state) {
NexoolCustomToast mNexoolCustomToast = new NexoolCustomToast(mContext, state);

mNexoolCustomToast.setTitle(mTitle);

mNexoolCustomToast.setMessage(mMessage);

return mNexoolCustomToast;
}
}

In this class I am using xml layout from nexool_fail_custom_toast.xml, nexool_success_custom_toast.xml with four layout screen size(layout-large, layout-normal, layout-small, layout-xlarge).

      //layout-large/nexool_fail_custom_toast.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@android:color/white"
android:layout_alignParentEnd="true"
android:layout_marginTop="?android:attr/actionBarSize"
android:layout_marginBottom="15dp"
android:layout_marginRight="15dp"
android:elevation="5dp">

<ImageButton
android:id="@+id/cancelToastImageButton"
android:layout_width="25dp"
android:layout_height="match_parent"
android:background="@android:color/holo_red_dark"
android:scaleType="centerInside"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="15dp">

<TextView
android:id="@+id/titleTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Error Title"
android:minWidth="300sp"
android:textColor="@android:color/holo_red_dark"
android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
android:id="@+id/messageTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Error Message"
android:minWidth="300sp"
android:paddingTop="10dp"
android:textColor="@android:color/black"
android:textAppearance="?android:attr/textAppearanceSmall"/>

</LinearLayout>


</LinearLayout>

</RelativeLayout>







//layout-large/nexool_success_custom_toast.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@android:color/white"
android:layout_alignParentEnd="true"
android:layout_marginTop="?android:attr/actionBarSize"
android:layout_marginBottom="15dp"
android:layout_marginRight="15dp"
android:elevation="5dp">

<ImageButton
android:id="@+id/cancelToastImageButton"
android:layout_width="25dp"
android:layout_height="match_parent"
android:background="@android:color/holo_green_light"
android:scaleType="centerInside"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="15dp">

<TextView
android:id="@+id/titleTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Error Title"
android:minWidth="300sp"
android:textColor="@android:color/holo_green_light"
android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
android:id="@+id/messageTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Error Message"
android:minWidth="300sp"
android:paddingTop="10dp"
android:textColor="@android:color/black"
android:textAppearance="?android:attr/textAppearanceSmall"/>

</LinearLayout>


</LinearLayout>

</RelativeLayout>

Now make above two xml files for layout-small, layout-normal, layout-xlarge by changing text size or width and height of views which are inside of these files.

** How to use **

For green success layout

    NexoolCustomToast.makeText(mContext, "Success", "Success Message", true).show();

For red failure layout

    NexoolCustomToast.makeText(mContext, "Fail", "Fail Message", false).show();

//OR

NexoolCustomToast.makeText(mContext, "Fail", "Fail Message").show();

Custom Toast

How to display Toast in Android?

In order to display Toast in your application, try this:

Toast.makeText(getActivity(), (String)data.result, 
Toast.LENGTH_LONG).show();

Another example:

Toast.makeText(getActivity(), "This is my Toast message!",
Toast.LENGTH_LONG).show();

We can define two constants for duration:

int LENGTH_LONG Show the view or text notification for a long period
of time.

int LENGTH_SHORT Show the view or text notification for a short period
of time.

Customizing your toast

LayoutInflater myInflater = LayoutInflater.from(this);
View view = myInflater.inflate(R.layout.your_custom_layout, null);
Toast mytoast = new Toast(this);
mytoast.setView(view);
mytoast.setDuration(Toast.LENGTH_LONG);
mytoast.show();

Apply default Toast style to custom Toast

It was really simple, I solved it.

Creating the Toast as normal with Toast.makeText(activity, message, length) I just grabbed the view, rotated it and reset it.

Toast toast = Toast.makeText(activity, msg, length);
View view = toast.getView();
view.setRotation(180);
toast.setView(view);

return toast;

Custom toast message in all screens?

change your method

from

showToastMessage(String message)

to

showToastMessage(Context context,String message);

it seems context problem to me

your function will look like this

public static void showToastMessage(Context context,String message){

LayoutInflater inflater = context.getLayoutInflater();

View layout = inflater.inflate(R.layout.custom_toast,
(ViewGroup) ((Activity) context).findViewById(R.id.customToast));
// set a message
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText(message);

// Toast...
Toast toast = new Toast(context);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}

How do you display a Toast using Kotlin on Android?

It can be an extension function for Context:

fun Context.toast(message: CharSequence) = 
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()

You can place this anywhere in your project, where exactly is up to you. For example, you can define a file mypackage.util.ContextExtensions.kt and put it there as a top level function.

Whenever you have access to a Context instance, you can import this function and use it:

import mypackage.util.ContextExtensions.toast

fun myFun(context: Context) {
context.toast("Hello world!")
}

Creating a toast in android and specifying the location

From https://developer.android.com/reference/android/widget/Toast#Toast(android.content.Context) :

Construct an empty Toast object. You must call setView(View) before you can call show().

Your code crashes because now you are using the Toast() constructor. And you need to setView(View) before show(). While this is not necessary with makeText().

As I understand, makeText() creates a View for you setting the text from the second parameter. But the constructor doesn't, and the setText() method does not either. setText() only update the an existing text:

From https://developer.android.com/reference/android/widget/Toast#setText(int) :

Update the text in a Toast that was previously created using one of the makeText() methods.

Here is an example of how to do that:

Custom toast on Android: a simple example

Edit

In any case, you can create the Toast with your initial code, without calling show().

public void onClick(View V) {

Toast t = Toast.makeText(QuizActivity.this,
R.string.correct_toast,
Toast.LENGTH_SHORT);


t.setGravity(Gravity.TOP|Gravity.LEFT,0,0);
t.show();

}

Edit 2

Now you have edited your code, but you are passing the Button View.



Related Topics



Leave a reply



Submit