Android: 2 toasts same time - android

Ok, I have 1 custom toast (xml layout) and it works great:
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast, (ViewGroup) findViewById(R.id.toast_layout));
ImageView image = (ImageView) layout.findViewById(R.id.logo);
image.setImageResource(R.drawable.logo);
title = (TextView) layout.findViewById(R.id.title);
txt = (TextView) layout.findViewById(R.id.text);
toast = new Toast(appContext);
toast.setGravity(Gravity.FILL_HORIZONTAL|Gravity.BOTTOM, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
But when I try to make 2nd one same way I get error "Source not found" which doesn't really tell me anything about what's wrong.
LayoutInflater infl = getLayoutInflater();
View lay = infl.inflate(R.layout.toast_arrows, (ViewGroup) findViewById(R.id.toast_lay));
toastarrows = new Toast(appContext);
toastarrows.setGravity(Gravity.FILL_HORIZONTAL|Gravity.CENTER, 0, 0);
toastarrows.setDuration(Toast.LENGTH_SHORT);
toastarrows.setView(lay);
toastarrows.show();
I'd like those 2 toasts to appear almost same time in different places of the screen.
Anyone can tell me please what's wrong with this code?

you are sure that you can show 2 Toast at the same time?
i'm not sure about this, i tried it but i can display only one Toast.
you have tried to show only the second one?

It seems that if you do really create two toasts at the sametime they will still be shown one after another in the same place. So I think your stuggling's vain.

Related

Issue with custom Toast with color

I trying to make a custom toast with the background "Orange color", however, in the attempt I am able to get it but the field also becomes white and the text is not visible, I have attached the image
You can see that the text is not visible.
here is my code:
String g= "+";
Toast toast = Toast.makeText(getActivity(), "Click and hold on '"+g+"' icon",
Toast.LENGTH_SHORT);
View view= toast.getView();
view.setBackgroundColor(Color.parseColor("#FF791B"));
View t = toast.getView().findViewById(android.R.id.message);
t.setBackgroundColor(Color.parseColor("#FFFFFF"));
toast.show();
I just want the text to be in white.
Is something I am missing in the code, I am not able to get it right.
Also, is there a way to make the "+" sign bold g
Just Change this line
t.setBackgroundColor(Color.parseColor("#FFFFFF"));
To
t.setTextColor(Color.parseColor("#FFFFFF"));
I have mode some changes here:
TextView t = toast.getView().findViewById(android.R.id.message);
t.setTextColor(Color.WHITE);
I have saved it as textView and I was able to get the setTextColor method and I was able to change the color
Try this - customttoast.xml is your custom toast xml and custom_toast_layout can be your activity layout xml
//Creating the LayoutInflater instance
LayoutInflater li = getLayoutInflater();
//Getting the View object as defined in the customtoast.xml file
View layout = li.inflate(R.layout.customtoast,(ViewGroup) findViewById(R.id.custom_toast_layout));
//Creating the Toast object
Toast toast = new Toast(getApplicationContext());
toast.setDuration(Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setView(layout);//setting the view of custom toast layout
toast.show();

Display text programmatically for TextView

I am looking to set a text programmatically, but for some reason, when I am doing so, no text appears on the screen. I have attached my code below.
LayoutInflater inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View lay = inflater.inflate(R.layout.fs, null);
lsc = (TextView) lay.findViewById(R.id.fst);
lsc.setText("XYZ performed on this day.");
Any ideas? "fs" is the xml file which has a textView component, whose id is "fst".

Create a Toast that Comes From Top of Screen

I am wondering how do we create a Custom toast in Android that shows on the top of screen like this?
I came from iOS background and now I have to create custom controls like this.
Any pointers please?
Thank you
Check this link: https://github.com/gfranks/GFMinimalNotifications, this is what you want i think and it is working fine for me.
output:
Or you can prepare custom toast like this:
View layout = getLayoutInflater().inflate(R.layout.customtoast,
(ViewGroup) findViewById(R.id.custom_toast_layout));
Toast toast = new Toast(getApplicationContext());
toast.setDuration(Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP, 0, 0);
toast.setView(layout);
toast.show();
you can use crouton for this purpose.
Description :
A Crouton will be displayed at the position the developer decides.
Standard will be the top of an application window. You can line up
multiple Croutons for display, that will be shown one after another
Create a Crouton for any CharSequence:
Crouton.makeText(Activity, CharSequence, Style).show();
more details on Crouton, a Context sensitive notifications for Android
there are many you can use for toast describe here
try this ::
Toast toast= Toast.makeText(getApplicationContext(),
"Your string here", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
add this line for position
toast.setGravity(Gravity.TOP, 0, 0);
creat customize toast layout, toast.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#color/yellow"
android:layout_width="match_parent"
android:padding="8dp"
android:gravity="center"
android:textSize="32sp"
android:layout_height="256dp"/>
create toast using the layout above:
static Toast t;
public static void show(Context c, String s) {
if (t == null) {
t = new Toast(c.getApplicationContext());
t.setGravity(Gravity.TOP | Gravity.FILL_HORIZONTAL, 0, 0);
LayoutInflater inflater = LayoutInflater.from(c);
TextView v = (TextView) inflater.inflate(R.layout.toast, null);
t.setView(v);
t.setDuration(Toast.LENGTH_LONG);
}
TextView v = (TextView) t.getView();
v.setText(s);
t.show();
}
As you can see, there are many solutions. But the easiest way is create a layout in your XML layout file, and make it invisible. When this view needs to be shown make it visible and start an animation.

Custom toast inflate root meaning

LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_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 is a lines of creating custom toast (from: http://developer.android.com/guide/topics/ui/notifiers/toasts.html)
It works fine, but I don't know why use the code
(ViewGroup) findViewById(R.id.toast_layout_root)
I think findViewById method runs find view "present" element,
so that method returns null,
That means, meaningless code, doesn't it?
Why I wonder that problem is I try to use create custom toast in Service
Service has not findViewById, you know,
so I just try to inflate(id, null) that lead to my layout broken,
I already read http://www.doubleencore.com/2013/05/layout-inflation-as-intended/ article.
How can I inflate properly without my layout parameter loss information

Android make Spanned containing link clickable in Toast

Through a webservice I get an Error message containing a link (e.g.
Click <a href='blablabla'>here</a>
). I use fromHtml to turn it into a spanned and then show it in a custom Toast.
Inside the toast the text is shown and the 'here' is underlined, just like a link. However when I click it, it doesn't do anything.
How should I solve this? Is there any way to deduct the link from the xml (e.g.
<ERROR>Click <a href='blabla'>here</a></ERROR>
) so I can create a button to push
which opens the link in a Webview?
Here's the relevant code:
My main activity
CommonCode.showToast(error, mContext, mViewGroup, true);
CommonCode
public static void showToast(Spanned toastString, Context context, View view, Boolean isLink) {
LayoutInflater inflater = LayoutInflater.from(context);
View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) view);
TextView text = (TextView) layout.findViewById(R.id.toastText);
if(isLink == true) {
text.setMovementMethod(LinkMovementMethod.getInstance());
}
text.setText(toastString);
Toast toast = new Toast(context);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}
Toasts were designed to place a small amount of information in front of the user for a short time. If you need buttons pressed try a dialog instead of a toast.

Categories

Resources