From 12ec213c0d1610ebc6c0bd233818e387aa1ad4b9 Mon Sep 17 00:00:00 2001 From: Don Yu Date: Wed, 20 Jul 2016 08:07:06 -0700 Subject: [PATCH] Add support for layout gravity to ToastAndroid Summary: Add support for top, bottom, and center layout gravity to ToastAndroid Reviewed By: AaaChiuuu Differential Revision: D3590224 fbshipit-source-id: 84dbbcfbe4133f291d62723c5c261acd7b32b46e --- .../js/ToastAndroidExample.android.js | 38 ++++++++++++++++++- .../ToastAndroid/ToastAndroid.android.js | 16 ++++++++ .../react/modules/toast/ToastModule.java | 23 ++++++++++- 3 files changed, 74 insertions(+), 3 deletions(-) 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(); + } + }); + } }