How to anchor a toast to a view, without overlapping them? - android

Background
Popups can be anchored to views, but the Toast is missing this API.
What I'd like to know, is how to correctly position a toast near a view (suppose prefer to put below if on upper half of screen, and above if on bottom half), so that they won't overlap each other, no matter the position&size of the view (unless really impossible because the view takes too much space, of course).
The problem
I'm aware there are various questions to this question, but all set the toast to be shown below the view, or manually set its position, without regard to the size of both of them.
What I've found
Currently I've found the next solutions:
here, it doesn't take the size of the toast into consideration
here, it lets you set the position of the toast manually, so they can still overlap each other.
It could be, however, that the toast that appears from long clicking an action item is anchored to the action item's view, but I didn't find out how it works there.
I've also tried many libraries from Android-Arsenal website, but all of them are either for styling the toasts, or make things that aren't toasts and sometimes don't even work well.
The question
Is there a generic way to use the same method used for Popups, on Toasts, so that it will be anchored to a view, without overlapping it?

You can set gravity for Toast in android like this
Toast toast = Toast.makeText(test.this,"bbb", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
and rather play around giving int values. like
toast.setGravity(5,6,5);
and also try other values..
Another way is, you should create a custom toast like view under a layout
check this library
https://github.com/sephiroth74/android-target-tooltip

Related

How to use ActionMode.setCustomView so that it takes as much space as it needs?

Background
In my app, I have a custom view, which is a title (TextView) that can be clicked (and have the background of a spinner, in order to show it's clickable) , as such:
The problem
I tried to add another action bar menu item, but now it makes the custom view take less space, instead of showing all of it, so the text gets truncated
What I've tried
I tried the next solutions:
setandroid:uiOptions="splitActionBarWhenNarrow" for the activity. This works, but it also mean that the upper action bar rarely has any content, so it's a waste of space.
I tried setting the width and/or minWidth of the custom view, but it didn't do anything.
I tried to make the textView have a marquee effect or multiple rows but it looks bad. I prefer the text to take as much space as it needs, and then have the rest of the action items.
The question
How can I make the custom view have as much space as it needs, and then have the other action items being shown ?
In the end, I used my own library that auto-resizes text (here) and set the layout params so that the width would be MATCH_PARENT .
It's not the solution I was hoping for , but it's also ok...
It will be this way on the next version of my app.

Title of custom dialog always maxes out width

My problem is identical to: Android: Custom Dialog has wrong dimensions
The solution for that was to disable the dialog title, which does work. The problem is I want the dialog title. I am positive the mere presence of the title causes all the extra width: if I set all child views to a known width (say 100 dp) it is still far too wide. As soon as I remove the title it's just fine, but again, I want the title! This has to be a bug, right?
Any ideas? I'm aware I can create a fake title, but I'd rather not.
Thanks!
P.S. - In case it matters (and I don't think it does) I am subclassing android.app.Dialog, not DialogFragment. Subclassing vs using the builder has no effect.
I know you would like to figure this out without disabling the Dialog Title, but it's really not an issue if you do that. Just add another view to your dialog layout, at the top, for your title.
No shame Mark! No shame!

Remove fade animation from toast message

I am using a hack to display an overlay over a picture in Google TV using a constant toast message.
I would like to know if there is a way to further modify the toast to remove the fade in and fade out effect. Ideally I would like the overlay to appear instantly.
Any help or direction would be greatly appreciated.
For this , i suggest we can use a layout(FrameLayout).
and by using a new layout above your current layout and adding view that looks similar to TOAST to the top layer
After that do one thing by making object of View as v
eg:-
View v;
v.setVisiblity(GONE);
v.setVisibility(VISIBLE);
This will make your view appear or hide.
You can use a FrameLayout and create a new Layout layer above your current layout and add a view that looks like toast to the top layer.
Then you can use: View.setVisiblity(GONE); and View.setVisibility(VISIBLE);
to make your view appear and hide instantly.
View.setVisibility Documentation
FrameLayout example
I like a fade-in effect, but I was in need to cancel a toast message immediately without fade-out effect, since the toast message remained shown for a short moment even when the Activity was already exited.
I came to this solution to close (or actually hide) my toast message t immediately:
((TextView)t.getView().findViewById(android.R.id.message)).setTextColor(Color.TRANSPARENT);
t.setText("");
t.getView().setBackgroundColor(Color.TRANSPARENT);
t.cancel();
I assume to set colors transparent is the only way to hide the fade effect with standard Toast layout.

Custom animation for Toast enter/exit?

I want to have my Toast's entrance and exit animations customized. Specifically, I want to make it appear instantly (no fade-in) and disappear like normal (fade-out).
Is there a way I can use animations with a toast?
Toasts are displayed using a system template that can't be changed so short answer is no you cant change the toast animation.
however you can make your own view that resembles a toast and animate it however you want.
It is not possible to do this with the stock Android Toast class. The Toast class is limited to four system animations and will not accept animations from your project.
See: Android add custom animation to Toast

Appcelerator: Proper Way Automatically Dismiss Keyboard

I'm writing an app using Titanium. I want to be able to automatically dismiss the keyboard anytime something outside of the text field is clicked. I have yet to find an elegant solution for this issue.
Couple things that I've thought about, but am still looking for a better solution:
Assign event listeners to basically everything else present in the view, and dismiss the keyboard (using textField.blur()). I want to avoid this since it results in a LOT of code just to dismiss the keyboard. Also, if I end up adding anything else to the view, I'll have to add a click listener to that object as well, so it's not very maintainable.
Create a large transparent view, and have it take up the entire screen. Place it directly beneath the text field and add to it one click listener on that which will dismiss the keyboard. This is a better solution than #1, but still isn't great because I've had a lot of trouble getting zIndexes to work properly. It's also inefficient for my purposes because I've got views with a specific width and height that encapsulate text fields. I've used these for the sake of code simplicity and I re-use them throughout my application.
I've tried adding a listener for the "blur" event for the text field but that doesn't seem to get fired appropriately.
That's about it. I'm sort of at a loss. The zIndexing also behaves strangely on the iPhone, and I haven't tried on Android yet. Also, as I mentioned above, many of the text fields I use are encapsulated within small views with set widths/heights-- so I think that will affect the functionality of Z-indexes.
So the root question is: What's the best way to dismiss a keyboard whenever anything outside the text field that's in focus is clicked?
If I'm correct the click event propagates through all views and windows therefore your #1 option could be modified to check for clicks on the bottom most layer (view or window), check for its source then decide what to do.

Categories

Resources