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
Related
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
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();
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.
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.
wise people!
This is my first question here. I'm stuck with a problem that seemed pretty simple to solve to me. I am not able to show a custom Toast message in my Android app.
There are two custom layouts I've created
my_toast.xml - layout file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toast_layout_root"
android:layout_width="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_height="wrap_content">
<TextView
android:id = "#+id/text"
android:background="#color/yumist_red"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:textColor="#fff"
android:text = "ToastText"
android:gravity = "center"
/>
</LinearLayout>
my_toast_up.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toast_layout_root"
android:layout_width="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_height="wrap_content">
<ImageView
android:layout_width="30dp"
android:layout_height="10dp"
android:src="#drawable/chat_arrow"
android:rotation="180"
/>
<TextView
android:id = "#+id/text"
android:background="#color/yumist_red"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:textColor="#fff"
android:text = "ToastText"
android:gravity = "center"
/>
</LinearLayout>
The only difference in the two is an imageview containing an arrow image. I'm trying to create text-bubble-style Toast.
I am well able to show and use the first one in my app. But, when I use the second layout with the image, all I see is the Image and an empty TextView of the ImageView's width and standard height. I've tried a lot of posts and existing questions online, but I cannot find a solution to this.
Any help?
[java code for showing Toasts]
public static void showToast(String message, int type)
{ //'message' is the text to display, 'type' determines which of the three toasts is shown
/*
* There are three fixed toasts:
* 1. One pointing upwards (with an angle) and placed near the top. (upperNotification)
* 2. Placed in the default position, to show general messages. (no issues with this | mToast)
* 3. Placed to point at bar at the bottom. (lowerNotification)
*/
TextView textView = null ; //this will refer to the message TextView
//corresponding to the toast selected
Toast toast = null; //to refer to the toast to display
switch(type)
{
case MyToast.ARROW_DOWN:textView = lowerToastText; toast = lowerNotification; break;
case MyToast.ARROW_UP:textView = upperToastText; toast= upperNotification; break;
case MyToast.ARROW_NONE:textView = toastText; toast = mToast; break;
}
if(textView != null && toast != null)
{
System.out.println(message);
textView.setText(message);
toast.show();
}
else
{
System.out.println("NULL!");
}
}
//Below is the code I used to init these.
public static void setUpToast(Activity activity, LayoutInflater inflater)
{
mToast = new Toast(activity);
upperNotification = new Toast(activity);
lowerNotification = new Toast(activity);
View layout = inflater.inflate(R.layout.my_toast, null);
toastText = (TextView) layout.findViewById(R.id.text);
mToast.setView(layout);
mToast.setDuration(Toast.LENGTH_LONG);
layout = inflater.inflate(R.layout.my_toast_up, null);
upperToastText = (TextView) layout.findViewById(R.id.text);
upperNotification.setView(layout);
upperNotification.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP, 0, Dish.dpToPx(96, activity));
upperNotification.setDuration(Toast.LENGTH_LONG);
layout = inflater.inflate(R.layout.my_toast_down, null);
lowerToastText = (TextView) layout.findViewById(R.id.text);
lowerNotification.setView(layout);
lowerNotification.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, Dish.dpToPx(48, activity));
lowerNotification.setDuration(Toast.LENGTH_LONG);
}
I figured out a fix by trial-and-error myself.
In my layout, I had kept my TextViews' width to match its parent. That was causing it to shrink down the ImageView's width for some reason.
I changed it to WRAP_CONTENT and of the parent view to MATCH_PARENT and it fixed my problem.
I am, however, still curious if there's a way to show the text with the parent's width.