#36 control the vertical position (added Android support)

This commit is contained in:
eddyverbruggen@gmail.com 2015-06-11 19:39:49 +02:00
parent 6208c60449
commit 2cb29b217a
2 changed files with 5 additions and 4 deletions

View File

@ -150,7 +150,7 @@ You can copy-paste these lines of code for a quick test:
<button onclick="window.plugins.toast.show('Hello there!', 'long', 'center', function(a){console.log('toast success: ' + a)}, function(b){alert('toast error: ' + b)})">Toast show long center</button>
```
#### Tweaking the vertical position (iOS only for now)
#### Tweaking the vertical position (iOS and Android)
Since 2.1.0 you can add pixels to move the toast up or down.
Note that `showWithOptions` can be used instead of the functions above, but it's not useful unless you want to pass `addPixelsY`.
```js

View File

@ -47,6 +47,7 @@ public class Toast extends CordovaPlugin {
final String message = options.getString("message");
final String duration = options.getString("duration");
final String position = options.getString("position");
final int addPixelsY = options.has("addPixelsY") ? options.getInt("addPixelsY") : 0;
cordova.getActivity().runOnUiThread(new Runnable() {
public void run() {
@ -56,11 +57,11 @@ public class Toast extends CordovaPlugin {
"short".equals(duration) ? android.widget.Toast.LENGTH_SHORT : android.widget.Toast.LENGTH_LONG);
if ("top".equals(position)) {
toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 20);
toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 20 + addPixelsY);
} else if ("bottom".equals(position)) {
toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 20);
toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 20 - addPixelsY);
} else if ("center".equals(position)) {
toast.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL, 0, 0);
toast.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL, 0, addPixelsY);
} else {
callbackContext.error("invalid position. valid options are 'top', 'center' and 'bottom'");
return;