Image icon within Toast maketext message - android

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

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

Display unicode character, Android back arrow, in toast message

I'm trying to display this Unicode character in a Toast message:
http://graphemica.com/%E2%AE%8C
It is embedded in a Xml file that contains all the strings of the Android application, like this
<string name="back_press">Click again ⮌ to exit</string>
The message shown from the Toast is the one in the image
toast message
Can you help me to solve this problem?
Thank you in advance.
You should use the Unicode character, in your case U+2B8C and replace U+ by 1x, but you should look for another icon because that one doesn't exist, search one you like in here.
For this example I've used the back arrow:
int unicode = 0x2B05;
String textIcon = new String(Character.toChars(unicode));
Toast.makeText(getApplicationContext(), "Your text here" + textIcon, Toast.LENGTH_LONG).show();
And that should view your icon.

Animating a toast in 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.

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.

How to check which image is set to the activity's view?

How can I check what image is on my activity (programmatically)? I want to make a condition: if I have lets say img1, then I want to show a Toast "img1", and if I have img2, then I want to show a Toast "img2". I know there is something like setImageDrawable(getResources().getDrawable(R.drawable.img2), which does setting an image.
you can use the setTag function associated with a view to hold this information, as in view.setTag("img1") and you can get back the value as in (String)view.getTag()
you want to test if your imageView contains an image, then u want to set another
use this if it helps
if(imgView.getDrawable().equals(this.getResources().getDrawable(R.id.img1))) {
//affiche your Toast Here ... Toast ( "img1 displayed ..").show();
Toast.makeText(this,"image 1 dispayed ...",3000).show();
}
else if(imgView.getDrawable().equals(this.getResources().getDrawable(R.id.img2))) {
//affiche your Toast Here ... Toast ( "img2 displayed ..").show();
Toast.makeText(this,"image 2 dispayed ...",3000).show();
}
is that what you want to do ??

Categories

Resources