Android make Spanned containing link clickable in Toast - android

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.

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();

Showing a custom Toast positioned into ListView Item position

I'm trying to show a custom toast exactly into deleted ListView item position. This is all I could do:
Adapter:
private View.OnClickListener onDeleteListener(final int position, final ViewHolder holder) {
return new View.OnClickListener() {
#Override
public void onClick(View v) {
showCustomToast(position, position);
}
};
}
public void showCustomToast(int positionX, int positionY) {
LayoutInflater inflater = LayoutInflater.from(context);
View layout = inflater.inflate(R.layout.toast_layout, null, false);
ImageView icon = (ImageView) layout.findViewById(R.id.toast_image);
TextView message = (TextView) layout.findViewById(R.id.toast_text);
LinearLayout fundoMensagem = (LinearLayout) layout.findViewById(R.id.toast_root);
message.setTextColor(Color.BLACK);
fundoMensagem.setBackgroundResource(R.color.error_colors);
message.setText("Item Deleted!");
Toast toast = new Toast(context);
toast.setDuration(toast.LENGTH_LONG);
toast.setView(layout);
toast.setGravity(positionX, positionX, positionX);
toast.show();
}
But this isn't exactly what I need because the position variable of the onDeleteListener method only get the item index and not the screen position. Someone can help me to show the custom toast at the center of each listview item?
You could use View.getLocationOnScreen() and/or getLocationInWindow() to get the absolute coordinates of a view on the screen, but I don't think a Toast is the right things to be putting there, Toasts are system level notifications, not app level, so it would be a bit of a stretch to try to force it into a position in your app. I think you would be much better served by some sort of custom view.

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.

Toast not showing well when using AppCompatActivity

I am inherited "AppCompatActivity" activity in my custom activity as shown in code below :
public class MyHomeActivity : AppCompatActivity
{
Toast.MakeText(this, "Email & Message successfully sent at SOS contacts.", ToastLength.Long).Show();
}
The toast message is not showing well in this activity as shown in the picture below.. please help if anybody have any solution for it....
Create a custom view for your Toast with a TextView that support multiple lines, this will create a custom view Toast with text that fits in it.
View view = LayoutInflater.Inflate(Resource.Layout.custom_toast, null);
var txt = view.FindViewById<TextView>(Resource.Id.txtCustomToast);
txt.Text = "your toast message";
var toast = new Toast(this)
{
Duration = ToastLength.Short,
View = view
};
toast.Show();
Also inserting a new line character (i.e \n) into the toast message in your current code, will show the toast message in two lines and also the background will be proper.

Android: 2 toasts same time

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.

Categories

Resources