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.
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.
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
Can I change text color of each item in a multiple-choice AlertDialog in Android? My AlertDialog popups with below items.
Apple
Banana
Mango
Grape
When displaying AlertDialog, default text color is black for all items. but I want to do as below.
When displaying AlertDialog,
text color of Apple should be Red.
text color of Banana should be Yellow.
text color of Mango should be Green.
text color of Grape should be Purple.
How can I do this?
you can achieve using textColorAlertDialogListItem property
<style name="MyDialogStyle2" parent="Theme.MaterialComponents.Dialog.Alert">
<item name="textColorAlertDialogListItem">#color/black</item>
</style>
and just pass this style while creating alert dialog
AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.MyDialogStyle);
You can do as below
builder.setMessage(Html.fromHtml("<font color='#FF0000'><b>John:</b></font>"+"How are you?"));
OR
<string name="Howtoplay"><![CDATA[html data here ]]>
</string>
builder.setMessage(Html.fromHtml(getString(R.string.HowtoPlay)));
OR
Use a custom dialog and then you can set the color of the text.
how to apply color in text message in alert dialog box in android
Edit:
You can split the word's by space and then set the spannable string foreground. Can also provide a custom color to each word in dialog.
you can also specify color from http://www.colorhunter.com/tag/android/1
main.xml
<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"
tools:context=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView1"
android:layout_alignRight="#+id/textView1"
android:layout_marginBottom="84dp"
android:layout_marginRight="18dp"
android:text="Button" />
dialog.xml custom layout to be inflated
<?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:orientation="vertical" >
<TextView
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="#+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="127dp"
android:text="CANCEL" />
</RelativeLayout>
Your MainActivity
public class MainActivity extends Activity {
String[] each;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b= (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
showpopup();
}
});
}
public void showpopup()
{
final Dialog d = new Dialog(MainActivity.this);
d.setTitle("my title");
d.setContentView(R.layout.dialog);
TextView tv= (TextView) d.findViewById(R.id.editText1);
String s="Apple Bannana Mango Orange";
tv.setText("");
for(int i=0;i<s.length();i++)
{
each = s.split("\\s+");
}
for(int i=0;i<each.length;i++)
{
SpannableString ss1= new SpannableString(each[i]);
if(each[i].equals("Apple"))
ss1.setSpan(new ForegroundColorSpan(Color.RED), 0, ss1.length(), 0);
else if(each[i].equals("Bannana"))
ss1.setSpan(new ForegroundColorSpan(Color.YELLOW), 0, ss1.length(), 0);
else if(each[i].equals("Mango"))
ss1.setSpan(new ForegroundColorSpan(Color.GREEN), 0, ss1.length(), 0);
else if(each[i].equals("Orange"))
ss1.setSpan(new ForegroundColorSpan(Color.GRAY), 0, ss1.length(), 0);
tv.append(ss1);
tv.append(" ");
tv.setMovementMethod(LinkMovementMethod.getInstance());
}
Button cancel = (Button) d.findViewById(R.id.cancel);
cancel.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
d.cancel();
}
});
d.show();
}
}
Resulting snapshot