diff --git a/Examples/UIExplorer/js/ToastAndroidExample.android.js b/Examples/UIExplorer/js/ToastAndroidExample.android.js
index 53cd3a95a85..86866b1c346 100644
--- a/Examples/UIExplorer/js/ToastAndroidExample.android.js
+++ b/Examples/UIExplorer/js/ToastAndroidExample.android.js
@@ -60,7 +60,43 @@ var ToastExample = React.createClass({
ToastAndroid.show('This is a toast with long duration', ToastAndroid.LONG)}>
- Click me too.
+ Click me.
+
+
+
+
+ ToastAndroid.showWithGravity(
+ 'This is a toast with top gravity',
+ ToastAndroid.SHORT,
+ ToastAndroid.TOP,
+ )
+ }>
+ Click me.
+
+
+
+
+ ToastAndroid.showWithGravity(
+ 'This is a toast with center gravity',
+ ToastAndroid.SHORT,
+ ToastAndroid.CENTER,
+ )
+ }>
+ Click me.
+
+
+
+
+ ToastAndroid.showWithGravity(
+ 'This is a toast with bottom gravity',
+ ToastAndroid.SHORT,
+ ToastAndroid.BOTTOM,
+ )
+ }>
+ Click me.
diff --git a/Libraries/Components/ToastAndroid/ToastAndroid.android.js b/Libraries/Components/ToastAndroid/ToastAndroid.android.js
index 1006b929387..ac03e62d480 100644
--- a/Libraries/Components/ToastAndroid/ToastAndroid.android.js
+++ b/Libraries/Components/ToastAndroid/ToastAndroid.android.js
@@ -19,13 +19,22 @@ var RCTToastAndroid = require('NativeModules').ToastAndroid;
*
* 1. String message: A string with the text to toast
* 2. int duration: The duration of the toast. May be ToastAndroid.SHORT or ToastAndroid.LONG
+ *
+ * There is also a function `showWithGravity` to specify the layout gravity. May be
+ * ToastAndroid.TOP, ToastAndroid.BOTTOM, ToastAndroid.CENTER
*/
var ToastAndroid = {
+ // Toast duration constants
SHORT: RCTToastAndroid.SHORT,
LONG: RCTToastAndroid.LONG,
+ // Toast gravity constants
+ TOP: RCTToastAndroid.TOP,
+ BOTTOM: RCTToastAndroid.BOTTOM,
+ CENTER: RCTToastAndroid.CENTER,
+
show: function (
message: string,
duration: number
@@ -33,6 +42,13 @@ var ToastAndroid = {
RCTToastAndroid.show(message, duration);
},
+ showWithGravity: function (
+ message: string,
+ duration: number,
+ gravity: number,
+ ): void {
+ RCTToastAndroid.showWithGravity(message, duration, gravity);
+ },
};
module.exports = ToastAndroid;
diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/toast/ToastModule.java b/ReactAndroid/src/main/java/com/facebook/react/modules/toast/ToastModule.java
index af128a96149..243c917a6fe 100644
--- a/ReactAndroid/src/main/java/com/facebook/react/modules/toast/ToastModule.java
+++ b/ReactAndroid/src/main/java/com/facebook/react/modules/toast/ToastModule.java
@@ -9,11 +9,11 @@
package com.facebook.react.modules.toast;
+import android.view.Gravity;
import android.widget.Toast;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
-import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.common.MapBuilder;
@@ -29,6 +29,10 @@ public class ToastModule extends ReactContextBaseJavaModule {
private static final String DURATION_SHORT_KEY = "SHORT";
private static final String DURATION_LONG_KEY = "LONG";
+ private static final String GRAVITY_TOP_KEY = "TOP";
+ private static final String GRAVITY_BOTTOM_KEY = "BOTTOM";
+ private static final String GRAVITY_CENTER = "CENTER";
+
public ToastModule(ReactApplicationContext reactContext) {
super(reactContext);
}
@@ -43,6 +47,9 @@ public class ToastModule extends ReactContextBaseJavaModule {
final Map constants = MapBuilder.newHashMap();
constants.put(DURATION_SHORT_KEY, Toast.LENGTH_SHORT);
constants.put(DURATION_LONG_KEY, Toast.LENGTH_LONG);
+ constants.put(GRAVITY_TOP_KEY, Gravity.TOP | Gravity.CENTER_HORIZONTAL);
+ constants.put(GRAVITY_BOTTOM_KEY, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
+ constants.put(GRAVITY_CENTER, Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
return constants;
}
@@ -50,9 +57,21 @@ public class ToastModule extends ReactContextBaseJavaModule {
public void show(final String message, final int duration) {
UiThreadUtil.runOnUiThread(new Runnable() {
@Override
- public void run(){
+ public void run() {
Toast.makeText(getReactApplicationContext(), message, duration).show();
}
});
}
+
+ @ReactMethod
+ public void showWithGravity(final String message, final int duration, final int gravity) {
+ UiThreadUtil.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ Toast toast = Toast.makeText(getReactApplicationContext(), message, duration);
+ toast.setGravity(gravity, 0, 0);
+ toast.show();
+ }
+ });
+ }
}