Animating a toast in android? - android

Am trying to animate my toast but have been unsuccessful so far. This is what I have in my onCreate():
ImageView image = new ImageView(this);
image.setImageResource(R.drawable.icon);
Animation move = new TranslateAnimation(0,100, 0, 100);
move.setDuration(2000);
move.setFillEnabled(true);
move.setFillAfter(true);
image.startAnimation(move);
Toast toast = new Toast(this);
toast.setView(image);
toast.show();
Have tried placing startAnimation() after displaying the toast, but that doesn't work either.

Toast notifications use a system template to determine how they are displayed. Short of modifying the Android source code, you can not create a special animation for your Toast message.
However, if possible you might be able to create a normal view and use that instead. One common method is to create a new Activity that has a transparent background with your modal toast message animated and displayed however you want.

Related

How to hide toast message from TalkBack for some reason

For accessibility demo purpose, I’m creating android example app.
By the way, during creating bad example accessibility, I have a question about toast issue. By default, every time toast is showing, TalkBack read the toast and it is very good.
But sometimes I want to hide toast from TalkBack so that TalkBack won’t read the toast message. Of course TalkBack must read all toast messages in order to give same information with none screen reader users. But sometimes in some apps, too many toast messages are appeared on screen and even the same message is stayed on screen.
So in that case TalkBack says too much and even TalkBack won’t read the toast, blind users can read the message that toasted through swiping.
Also the toast message is not alert text. So in some cases, I think hiding toast from TalkBack is needed.
But I don’t know how to do this. I set one view in java and added toast message. And then I set importantForAccessibility to NO, but it doesn’t work.
My code is below.
Lastly, I referred as the stack that customizing TalkBack toast.
Thank you.
imgClick2 = (ImageView)findViewById(R.id.imageView2);
imgClick2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast toast = new Toast(MainActivity.this);
TextView messageView = new TextView(MainActivity.this);
messageView.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
messageView.setText("visible text");
toast.setView(messageView);
toast.show();
}
});

Image icon within Toast maketext message

I am beginning learning some android mobile development and have created a notepad app through some tutorials and am now wanting to customise it a little.
I currently have a Toast maketext message that displays when a user saves a new note. The code is as follows:
if(Utilities.saveNote(this, new Note(mNoteCreationTime, title, content)))
Toast.makeText(this, "Swag Note has been saved", Toast.LENGTH_SHORT).show();
What I am wanting to do is add a small icon at both ends of this toast message.
Is there a relatively simple way of achieving this?
Toasts cannot have icons. You can create a custom Toast with ImageViews in it (example). However, there might be an unicode symbol that suits your purpose: in that case, you can just paste it.
Edit:
To make the linked example work, you'd just have to add a View for the image with id "toast_image", which will be invoked this way:
ImageView image = (ImageView) layout.findViewById(R.id.toast_image);
Show Toast as you do it now:
Toast.makeText(this, "Swag Note has been saved", Toast.LENGTH_SHORT).show();
Show MyToast from linked example:
MyToast.show(this, "Swag Note has been saved", false);

Android avoid closing Toast when click on it

This is how i show my Toast message.
Toast.makeText(getActivity(), "This is my Toast message!",Toast.LENGTH_LONG).show();
When i click on Toast it will disappear.
How can I avoid sudden disappear of Toast when i click on it.
Toast doesn't disappear for sudden clicks . , it will disappear on with duration u set
I suggest you to use Snackbar.
Snackbar has LENGTH_INDEFINITE attribute on the duration so I think it is pretty much what you wanted.
Toasts doesn't close on interactions it depends on the duration you set which are
LENGTH_SHORT and LENGTH_LONG are 0 and 1. If you want something that stays longer use actionbar notifications
Andorid Toast can be used to display information for the short period of time. A toast contains message to be displayed quickly and disappears after sometime.
You can also create custom toast as well
//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();
OR ..
Toast toast=Toast.makeText(getApplicationContext(),"Hello Javatpoint",Toast.LENGTH_SHORT);
toast.setMargin(50,50);
toast.show();
You can use the for loops for duration

Android App - How to Set OpenFeint Notification Position

I've Integrated OpenFeint in my android game but the score-post and achievement unlock notification is shown at bottom of the screen automatically.
how can i manually set the location of the that notification on my screen.? I want to show it to the top of the screen.
The OpenFeint sample code uses the Toast Class to display those notifications. You can easily adjust that code to almost anything you want.
edit: I realize that you might mean the notification that comes from the bottom, in which case you will need to write a class extending com.openfeint.api.Notification.Delegate and display your own design. Unfortunately OpenFeint doesnt provide parameters on the default notification class.
You can easily show it at the top of the screen by editing the NotificationBase.java file. It is located in com.openfeint.internal.notifications. Just look for the showToast() method, and edit it. Change Gravity.BOTTOM to Gravity.TOP. Check it out below:
protected void showToast() {
OpenFeintInternal.getInstance().runOnUiThread(new Runnable() {
#Override
public void run() {
Context appContext = OpenFeintInternal.getInstance().getContext();
toast = new Toast(appContext);
toast.setGravity(Gravity.TOP, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(displayView);
toast.show();
}
});
}

Android -- Is there a way to rotate a toast 90 degrees?

Can't think of any more info to provide. Is there a way?
As hackbod said, you would have to have a custom view to display the toast.
I found a few classes for you that rotates the label for you: VerticalLabelView and CustomTextView
I chose to use the latter, and had this code working in my own app:
// Creating a new toast object
Toast myToast = new Toast(MyActivity.this);
// Creating our custom text view, and setting text/rotation
CustomTextView text = new CustomTextView(MyActivity.this);
text.SetText("Hello World!");
text.SetRotation(-90, 120, 90);
myToast.setView(text);
// Setting duration and displaying the toast
myToast.setDuration(Toast.LENGTH_SHORT);
myToast.show();
Not an easy way. You can supply your own view to display the toast, so you can make a view that rotates its content.

Categories

Resources