Skip to content

Commit 546a43b

Browse files
mgoovaerfacebook-github-bot
authored andcommitted
Expose offset parameters for ToastAndroid
Reviewed By: brosenfeld Differential Revision: D5560628 fbshipit-source-id: b1457493e8429958fbd7bc9c490cffaa33b4a95a
1 parent f3feca9 commit 546a43b

File tree

3 files changed

+62
-3
lines changed

3 files changed

+62
-3
lines changed

Libraries/Components/ToastAndroid/ToastAndroid.android.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* of patent rights can be found in the PATENTS file in the same directory.
88
*
99
* @providesModule ToastAndroid
10+
* @flow
1011
*/
1112

1213
'use strict';
@@ -23,10 +24,14 @@ var RCTToastAndroid = require('NativeModules').ToastAndroid;
2324
* There is also a function `showWithGravity` to specify the layout gravity. May be
2425
* ToastAndroid.TOP, ToastAndroid.BOTTOM, ToastAndroid.CENTER.
2526
*
27+
* The 'showWithGravityWithOffset' function adds on the ability to specify offset
28+
* These offset values will translate to pixels.
29+
*
2630
* Basic usage:
2731
* ```javascript
2832
* ToastAndroid.show('A pikachu appeared nearby !', ToastAndroid.SHORT);
2933
* ToastAndroid.showWithGravity('All Your Base Are Belong To Us', ToastAndroid.SHORT, ToastAndroid.CENTER);
34+
* ToastAndroid.showWithGravityAndOffset('A wild toast appeared!', ToastAndroid.LONG, ToastAndroid.BOTTOM, 25, 50);
3035
* ```
3136
*/
3237

@@ -55,6 +60,16 @@ var ToastAndroid = {
5560
): void {
5661
RCTToastAndroid.showWithGravity(message, duration, gravity);
5762
},
63+
64+
showWithGravityAndOffset: function (
65+
message: string,
66+
duration: number,
67+
gravity: number,
68+
xOffset: number,
69+
yOffset: number,
70+
): void {
71+
RCTToastAndroid.showWithGravityAndOffset(message, duration, gravity, xOffset, yOffset);
72+
},
5873
};
5974

6075
module.exports = ToastAndroid;

RNTester/js/ToastAndroidExample.android.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,34 @@ class ToastExample extends React.Component {
8282
<Text style={styles.text}>Click me.</Text>
8383
</TouchableWithoutFeedback>
8484
</RNTesterBlock>
85+
<RNTesterBlock title="Toast with x offset">
86+
<TouchableWithoutFeedback
87+
onPress={() =>
88+
ToastAndroid.showWithGravityAndOffset(
89+
'This is a toast with x offset',
90+
ToastAndroid.SHORT,
91+
ToastAndroid.CENTER,
92+
50,
93+
0,
94+
)
95+
}>
96+
<Text style={styles.text}>Click me.</Text>
97+
</TouchableWithoutFeedback>
98+
</RNTesterBlock>
99+
<RNTesterBlock title="Toast with y offset">
100+
<TouchableWithoutFeedback
101+
onPress={() =>
102+
ToastAndroid.showWithGravityAndOffset(
103+
'This is a toast with y offset',
104+
ToastAndroid.SHORT,
105+
ToastAndroid.BOTTOM,
106+
0,
107+
50,
108+
)
109+
}>
110+
<Text style={styles.text}>Click me.</Text>
111+
</TouchableWithoutFeedback>
112+
</RNTesterBlock>
85113
</RNTesterPage>
86114
);
87115
}

ReactAndroid/src/main/java/com/facebook/react/modules/toast/ToastModule.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@
1111

1212
import android.view.Gravity;
1313
import android.widget.Toast;
14-
1514
import com.facebook.react.bridge.NativeModule;
1615
import com.facebook.react.bridge.ReactApplicationContext;
1716
import com.facebook.react.bridge.ReactContextBaseJavaModule;
1817
import com.facebook.react.bridge.ReactMethod;
19-
import com.facebook.react.common.MapBuilder;
2018
import com.facebook.react.bridge.UiThreadUtil;
19+
import com.facebook.react.common.MapBuilder;
2120
import com.facebook.react.module.annotations.ReactModule;
22-
2321
import java.util.Map;
2422

2523
/**
@@ -76,4 +74,22 @@ public void run() {
7674
}
7775
});
7876
}
77+
78+
@ReactMethod
79+
public void showWithGravityAndOffset(
80+
final String message,
81+
final int duration,
82+
final int gravity,
83+
final int xOffset,
84+
final int yOffset) {
85+
UiThreadUtil.runOnUiThread(
86+
new Runnable() {
87+
@Override
88+
public void run() {
89+
Toast toast = Toast.makeText(getReactApplicationContext(), message, duration);
90+
toast.setGravity(gravity, xOffset, yOffset);
91+
toast.show();
92+
}
93+
});
94+
}
7995
}

0 commit comments

Comments
 (0)