Expose offset parameters for ToastAndroid

Reviewed By: brosenfeld

Differential Revision: D5560628

fbshipit-source-id: b1457493e8429958fbd7bc9c490cffaa33b4a95a
This commit is contained in:
Maxime Goovaerts 2017-08-04 08:54:49 -07:00 committed by Facebook Github Bot
parent f3feca91fc
commit 546a43bda0
3 changed files with 62 additions and 3 deletions

View File

@ -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;

View File

@ -82,6 +82,34 @@ class ToastExample extends React.Component {
<Text style={styles.text}>Click me.</Text>
</TouchableWithoutFeedback>
</RNTesterBlock>
<RNTesterBlock title="Toast with x offset">
<TouchableWithoutFeedback
onPress={() =>
ToastAndroid.showWithGravityAndOffset(
'This is a toast with x offset',
ToastAndroid.SHORT,
ToastAndroid.CENTER,
50,
0,
)
}>
<Text style={styles.text}>Click me.</Text>
</TouchableWithoutFeedback>
</RNTesterBlock>
<RNTesterBlock title="Toast with y offset">
<TouchableWithoutFeedback
onPress={() =>
ToastAndroid.showWithGravityAndOffset(
'This is a toast with y offset',
ToastAndroid.SHORT,
ToastAndroid.BOTTOM,
0,
50,
)
}>
<Text style={styles.text}>Click me.</Text>
</TouchableWithoutFeedback>
</RNTesterBlock>
</RNTesterPage>
);
}

View File

@ -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();
}
});
}
}