Permalink
Browse files

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
  • Loading branch information...
1 parent 768caf5 commit 12ec213c0d1610ebc6c0bd233818e387aa1ad4b9 @donyu donyu committed with Facebook Github Bot 4 Jul 20, 2016
@@ -60,7 +60,43 @@ var ToastExample = React.createClass({
<TouchableWithoutFeedback
onPress={() =>
ToastAndroid.show('This is a toast with long duration', ToastAndroid.LONG)}>
- <Text style={styles.text}>Click me too.</Text>
+ <Text style={styles.text}>Click me.</Text>
+ </TouchableWithoutFeedback>
+ </UIExplorerBlock>
+ <UIExplorerBlock title="Toast with top gravity">
+ <TouchableWithoutFeedback
+ onPress={() =>
+ ToastAndroid.showWithGravity(
+ 'This is a toast with top gravity',
+ ToastAndroid.SHORT,
+ ToastAndroid.TOP,
+ )
+ }>
+ <Text style={styles.text}>Click me.</Text>
+ </TouchableWithoutFeedback>
+ </UIExplorerBlock>
+ <UIExplorerBlock title="Toast with center gravity">
+ <TouchableWithoutFeedback
+ onPress={() =>
+ ToastAndroid.showWithGravity(
+ 'This is a toast with center gravity',
+ ToastAndroid.SHORT,
+ ToastAndroid.CENTER,
+ )
+ }>
+ <Text style={styles.text}>Click me.</Text>
+ </TouchableWithoutFeedback>
+ </UIExplorerBlock>
+ <UIExplorerBlock title="Toast with bottom gravity">
+ <TouchableWithoutFeedback
+ onPress={() =>
+ ToastAndroid.showWithGravity(
+ 'This is a toast with bottom gravity',
+ ToastAndroid.SHORT,
+ ToastAndroid.BOTTOM,
+ )
+ }>
+ <Text style={styles.text}>Click me.</Text>
</TouchableWithoutFeedback>
</UIExplorerBlock>
</UIExplorerPage>
@@ -19,20 +19,36 @@ 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
): void {
RCTToastAndroid.show(message, duration);
},
+ showWithGravity: function (
+ message: string,
+ duration: number,
+ gravity: number,
+ ): void {
+ RCTToastAndroid.showWithGravity(message, duration, gravity);
+ },
};
module.exports = ToastAndroid;
@@ -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 @@
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,16 +47,31 @@ public String getName() {
final Map<String, Object> 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;
}
@ReactMethod
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();
+ }
+ });
+ }
}

0 comments on commit 12ec213

Please sign in to comment.