How to determine if there is any toast being displayed in android - android

Is there a way to check if there is a toast message being displayed in android?
I am writing an idlingResource and I want to make sure no toast is currently being displayed before I return the resource as being idle.

Create Custom toast.
private Toast myToast = nuul; // Create class Level Object to
Add this code in the method
String message = "Your Message";
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
the (ViewGroup) findViewById(R.id.custom_toast_container));
TextView text = (TextView) layout.findViewById(R.id.custom_toast_text);
text.setText(Message);
myToast = new Toast(getApplicationContext());
myToast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
myToast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
myToast.setDuration(Toast.LENGTH_LONG);
myToast.setView(layout);
myToast.show();
custom_toast.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/custom_toast_container"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="8dp"
android:background="#80000000">
<TextView
android:id="#+id/custom_toast_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:enabled="true"
android:textSize="16dp"
/>
</LinearLayout>
Dismiss the Toast, if it is not null
if (myToast != null)
myToast.cancel(); // Dismiss the toast

Related

Dynamically modify layout of a toast Android

I have a trouble creating my app in Android Studio, I explain myself
I created a layout for my toast, the XML looks like this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/elemento_correcto_s" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" android:background="#afff45"
android:weightSum="1">
<ImageView android:layout_height="100dp" android:layout_width="100dp" android:src="#drawable/base" android:padding="5dip" android:id="#+id/ok"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="false">
</ImageView>
<TextView android:layout_height="50dp" android:id="#+id/tv" android:layout_width="match_parent" android:text="¡CORRECTO!" android:textColor="#FFF" android:gravity="center_vertical|center_horizontal"
android:layout_gravity="center_vertical"
android:layout_toRightOf="#+id/ok"
android:layout_marginTop="26dp"
>
</TextView>
It works fine when I inflate the layout
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(
R.layout.elemento_correcto_s
, (ViewGroup) findViewById(R.id.elemento_correcto_s));
this.elementoCorrecto = new Toast(this);
this.elementoCorrecto.setGravity(Gravity.TOP, 0, 0);
this.elementoCorrecto.setDuration(Toast.LENGTH_LONG);
this.elementoCorrecto.setView(layout);
this.elementoCorrecto.show();
But the problem is that I want to change dynamically the text for the TextView, I already tried just calling the TextView and changing the text, but it doesn't work, so I hope you can help me
This is my code
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(
R.layout.elemento_correcto_xl
, (ViewGroup) findViewById(R.id.elemento_correcto_xl));
TextView tvCombo = (TextView) findViewById(R.id.tv);
if(combo > 1) {
tvCombo.setText("¡" + combo + " VECES SEGUIDAS!");
}
else
tvCombo.setText("¡CORRECTO!");
this.elementoCorrecto = new Toast(this);
this.elementoCorrecto.setGravity(Gravity.TOP, 0, 0);
this.elementoCorrecto.setDuration(Toast.LENGTH_LONG);
this.elementoCorrecto.setView(layout);
this.elementoCorrecto.show();
You can use this method to as i passing the string to show..
public static void makeToast(Context c, String msgToShow){
LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v= inflater.inflate(R.layout.view_toast,null); //your layout to show
TextView text = (TextView) v.findViewById(R.id.textViewToast);
text.setText(msgToShow);
Toast toast = new Toast(c);
toast.setGravity(Gravity.BOTTOM, 10, 25);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(v);
toast.show();
}
Call it wherever you want in the Activities.
Is there any special reason to show a toast using TextView ? Otherwise you can follow below method to show toast dynamically -
String toastText = "";
if(combo > 1) {
toastText = "¡" + combo + " VECES SEGUIDAS!";
}
else
toastText = "¡CORRECTO!";
Toast.makeText(activity, toastText, Toast.LENGTH_SHORT).show();

How to define a Toast style in style.xml?

Can a Toast be styled in style.xml like we do for Activity themes?
I want to style the following:
Text color
Text Size
Text font
Background color/opacity
Background Radius of corners and sizes
I can't find anything that relates to Toast on the web or in the style.xml
I have solved it by making an StyleableToast class which you can easily use to style your Toasts in almost any way! See answer here: https://stackoverflow.com/a/39591755/5366495
Since there was not an easy and a non messy way (layouts, inflating etc) to style a Toast, I decided then to make a complete Styleable Toast class with a lot of styling possibilities!
I will keep improving the Styleable Toast class and make it feature rich and release it in the jCenter() so it can be added as an dependency
Here is it. Just a single class you put in your project: https://github.com/Muddz/StyleableToast
Examples of toasts made with StyleableToast:
All feedback and feature requests is welcome!
why you dont try to make your own toast layout :
LayoutInflater inflater = getLayoutInflater();
View customToastroot = inflater.inflate(R.layout.custom_toast, null);
TextView msg = (TextView) customToastroot.findViewById(R.id.toastMsg);
msg.setText("Speed up !");
msg.setTypeface(tf);
Toast customtoast = new Toast(getApplicationContext());
customtoast.setView(customToastroot);
customtoast.setDuration(Toast.LENGTH_SHORT);
customtoast.show();
and here is the custom_toast.xml :
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/game_on">
<TextView
android:id="#+id/toastMsg"
android:layout_margin="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dp"
android:text="Your text"
android:textSize="16dp"
android:layout_gravity="center_vertical"/>
</LinearLayout>
Hope this will help you .
I think you should stop using Toast and look up SnackBar. This is the new standard in Material Design guideline way of displaying Toast-type messages.
You can use it similar to a toast but you can also set a layout for how content should be displayed.
Snackbar snackbar = Snackbar
.make(coordinatorLayout, "Welcome to AndroidHive", Snackbar.LENGTH_LONG);
snackbar.show();
Not only that, you can also set custom interactions like button clicks in the SnackBar. Eg:
Snackbar snackbar = Snackbar
.make(coordinatorLayout, "Message is deleted", Snackbar.LENGTH_LONG)
.setAction("UNDO", new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar snackbar1 = Snackbar.make(coordinatorLayout, "Message is restored!", Snackbar.LENGTH_SHORT);
snackbar1.show();
}
});
snackbar.show();
Here is some links to help you apart from documentation.
1) Material Design guidelines for SnackBar
2) SnackBar examples
I think this is the best way to go as it allows you to do everything that you have asked in your question.
I created my own class for that. Because it prevents me from trouble I experienced from other peoples solutions.
This is how it looks:
You can show a Toast simple in a single row:
MyToast.showShort(context, getString(R.string.verworfen));
MyToast.showLong(context, getString(R.string.verworfen));
//Code
public class MyToast{
private static Toast currentToast;
public static void showShort(Context context, String message){
if(currentToast != null) currentToast.cancel();
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast, (ViewGroup) ((Activity) context).findViewById(R.id.root));
TextView text = (TextView) layout.findViewById(R.id.message);
text.setText(message);
Toast toast = new Toast(context);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(layout);
toast.show();
currentToast = toast;
}
public static void showLong(Context context, String message){
if(currentToast != null) currentToast.cancel();
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast, (ViewGroup) ((Activity) context).findViewById(R.id.root));
TextView text = (TextView) layout.findViewById(R.id.message);
text.setText(message);
Toast toast = new Toast(context);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
currentToast = toast;
}
public static Toast getCurrentToast(){
return currentToast;
}
}
//Layout
<LinearLayout
android:id="#+id/root"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/custom_toast">
<TextView
android:id="#id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="12dp"
android:layout_gravity="center_horizontal"
android:textColor="#color/white"
android:textSize="14sp" />
</LinearLayout>
//Drawable
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid
android:color="#color/primary_dark" >
</solid>
<stroke
android:width="2dp"
android:color="#color/primary_light" >
</stroke>
<padding
android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp" >
</padding>
<corners
android:radius="11dp" >
</corners>
</shape>
Styling Toast, or more specifically setView() was deprecated in Android 11 (API 30). Google does not want every app to have different toast style.
If you want a custom toast, you will have to implement and show your custom view, that looks like a toast.
Imo, you shouldn't do it and use default toast without any custom style, like it was intended by Google.

Change the color of Toast

How I can change the color of the message Toast?
Here my code:
public void checkButton(View view) {
if(count < 0){
Toast.makeText(getApplicationContext(), "Incorreto!",
Toast.LENGTH_SHORT).show();
}
else if(count == 0){
Toast.makeText(getApplicationContext(), "Correto",
Toast.LENGTH_SHORT).show();
}
}
}
Toast toast = Toast.makeText(getApplicationContext(), "Correto!",
Toast.LENGTH_SHORT);
TextView toastMessage = (TextView) toast.getView().findViewById(android.R.id.message);
toastMessage.setTextColor(Color.RED);
toast.show();
The method to change the color, position and background color of toast is:
Toast toast=Toast.makeText(getApplicationContext(),"This is advanced toast",Toast.LENGTH_LONG);
toast.setGravity(Gravity.BOTTOM | Gravity.RIGHT,0,0);
View view=toast.getView();
TextView view1=(TextView)view.findViewById(android.R.id.message);
view1.setTextColor(Color.YELLOW);
view.setBackgroundResource(R.color.colorPrimary);
toast.show();
For line by line explanation: https://www.youtube.com/watch?v=5bzhGd1HZOc
Create a custom Toast layout, such as correct_toast.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toast_layout_root"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="8dp"
android:background="#DAAA">
<TextView android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFF"
/>
</LinearLayout>
Then in the java code, construct the toast with this view:
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.correct_toast,
(ViewGroup) findViewById(R.id.toast_layout_root));
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("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();
This way, you are able to change the color of the background, and/or change the color of the text.

Android TextView Visibility in AlertDialog does not change

I am using an AlertDialog created using android.suport.v7.app.AlertDialog and I am having a custom layout for the Dialog with a few EditTexts and a few TextViews.
The TextViews that are used are initially having their visibility set to GONE.
I am using the TextViews mainly to prompt the user for incorrect inputs in the EditText fields.
So when I try to toggle the visibility of the TextView to VISIBLE for showing an error message, the visibility does not change.
My XML File for layoutS is:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/contactName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:hint="#string/sampark_naam"
android:layout_marginEnd="5dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_marginRight="5dp">
<requestFocus />
</EditText>
<TextView
android:id="#+id/noName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/no_name_error"
android:textColor="#android:color/holo_red_dark"
android:layout_marginEnd="5dp"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginRight="5dp"
android:visibility="gone"/>
</LinearLayout>
The AlertDialog used is:
final Context context = view.getContext();
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View aDrishya = layoutInflater.inflate(R.layout.layoutS, null, false);
vNaam = (EditText) aDrishya.findViewById(R.id.contactName);
nameError = (TextView) aDrishya.findViewById(R.id.noName);
sSanddok = new AlertDialog.Builder(context)
.setView(aDrishya)
.setTitle("Add A New Contact")
.setPositiveButton("Add +", new PositiveButton(context))
.setNegativeButton("Pick", new null);
sSanddok.show();
In the positive button I check for the contactName entered as:
if(vNaam.getText().toString().equals("")) {
Toast.makeText(this, "No Name", Toast.LENGTH_SHORT).show();
nameError.setVisibility(View.VISIBLE);
} else {
nameError.setVisibility(View.GONE);
}
It is showing the Toast but it's not showing the TextView.
Any idea what's wrong here and how to fix it?
Hi can you try to change and run this code, See if it works..
Alert dialog changes :
sSanddok = new AlertDialog.Builder(context)
.setView(R.layout.layoutS)
.setTitle("Add A New Contact")
.setPositiveButton("Add +", null)
.setNegativeButton("Pick",null);
sSanddok.show();
sSanddok.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//if edit text value is empty do this
TextView tv = (TextView)sSanddok.findViewById(R.id.noName);
tv.setVisibility(View.VISIBLE);
}
});
TextView visibility in xml is set to gone..
Hi this is working for me...Try this code pls

Making a Color Toast in Android

I have seen sometimes a Blue toast showing up when i connect my Samsung phone with wifi network. Can anyone help for customising the colour of the toast.
For example:
Try This:-
SpannableString text = new SpannableString("Please Wait !!!!! ");
text.setSpan(new ForegroundColorSpan(Color.BLUE), 0, 41, 0);
Toast.makeText(c.getApplicationContext(),text , Toast.LENGTH_LONG).show();
Another Way:-
Make an xml / customToast.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toast_layout"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="8dp"
android:background="#DAAA"
>
<ImageView android:src="#drawable/img1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
/>
<TextView android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFF"
/>
</LinearLayout>
In Activity:-
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.customToast,
(ViewGroup) findViewById(R.id.toast_layout));
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("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();
Like this way:
LayoutInflater inflater = youractivity.this.getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
null);
TextView text = (TextView) layout.findViewById(R.id.tvtoast);
text.setText("No Internet Connection");
text.setTextColor(Color.BLACK);
Toast toast = new Toast(getActivity());
toast.setView(layout);
toast.setDuration(100);
toast.show();
Make your own layout custom_toast.xml and set Color to TextView Text as per your need
Output:
You can create a custom Toast view to suit your requirements. See the section titled "Creating a Custom Toast View" at http://developer.android.com/guide/topics/ui/notifiers/toasts.html

Categories

Resources