diff --git a/Libraries/Components/ToastAndroid/ToastAndroid.android.js b/Libraries/Components/ToastAndroid/ToastAndroid.android.js index 36899ca30..befc396f9 100644 --- a/Libraries/Components/ToastAndroid/ToastAndroid.android.js +++ b/Libraries/Components/ToastAndroid/ToastAndroid.android.js @@ -7,6 +7,7 @@ * of patent rights can be found in the PATENTS file in the same directory. * * @providesModule ToastAndroid + * @flow */ 'use strict'; @@ -23,10 +24,14 @@ var RCTToastAndroid = require('NativeModules').ToastAndroid; * There is also a function `showWithGravity` to specify the layout gravity. May be * ToastAndroid.TOP, ToastAndroid.BOTTOM, ToastAndroid.CENTER. * + * The 'showWithGravityWithOffset' function adds on the ability to specify offset + * These offset values will translate to pixels. + * * Basic usage: * ```javascript * ToastAndroid.show('A pikachu appeared nearby !', ToastAndroid.SHORT); * ToastAndroid.showWithGravity('All Your Base Are Belong To Us', ToastAndroid.SHORT, ToastAndroid.CENTER); + * ToastAndroid.showWithGravityAndOffset('A wild toast appeared!', ToastAndroid.LONG, ToastAndroid.BOTTOM, 25, 50); * ``` */ @@ -55,6 +60,16 @@ var ToastAndroid = { ): void { RCTToastAndroid.showWithGravity(message, duration, gravity); }, + + showWithGravityAndOffset: function ( + message: string, + duration: number, + gravity: number, + xOffset: number, + yOffset: number, + ): void { + RCTToastAndroid.showWithGravityAndOffset(message, duration, gravity, xOffset, yOffset); + }, }; module.exports = ToastAndroid; diff --git a/RNTester/js/ToastAndroidExample.android.js b/RNTester/js/ToastAndroidExample.android.js index 34dc88e99..120d6d1a5 100644 --- a/RNTester/js/ToastAndroidExample.android.js +++ b/RNTester/js/ToastAndroidExample.android.js @@ -82,6 +82,34 @@ class ToastExample extends React.Component { Click me. + + + ToastAndroid.showWithGravityAndOffset( + 'This is a toast with x offset', + ToastAndroid.SHORT, + ToastAndroid.CENTER, + 50, + 0, + ) + }> + Click me. + + + + + ToastAndroid.showWithGravityAndOffset( + 'This is a toast with y offset', + ToastAndroid.SHORT, + ToastAndroid.BOTTOM, + 0, + 50, + ) + }> + Click me. + + ); } 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 1ffc334b7..6902df223 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 @@ -11,15 +11,13 @@ 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.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactMethod; -import com.facebook.react.common.MapBuilder; import com.facebook.react.bridge.UiThreadUtil; +import com.facebook.react.common.MapBuilder; import com.facebook.react.module.annotations.ReactModule; - import java.util.Map; /** @@ -76,4 +74,22 @@ public class ToastModule extends ReactContextBaseJavaModule { } }); } + + @ReactMethod + public void showWithGravityAndOffset( + final String message, + final int duration, + final int gravity, + final int xOffset, + final int yOffset) { + UiThreadUtil.runOnUiThread( + new Runnable() { + @Override + public void run() { + Toast toast = Toast.makeText(getReactApplicationContext(), message, duration); + toast.setGravity(gravity, xOffset, yOffset); + toast.show(); + } + }); + } }