Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add style, to readme. and also added the properties i just added of h… #31

Open
wants to merge 31 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
97f27bf
add style, to readme. and also added the properties i just added of h…
Noitidart Feb 22, 2018
5d68e7f
Update RNPromptFragment.java
Noitidart Feb 22, 2018
4d870b7
Update RNPromptModule.java
Noitidart Feb 22, 2018
9b16846
RNPromptFragment.java updated for buttonColor and cust style name
Noitidart Mar 9, 2018
f9ab93b
RNPromptModule.java update for buttonColor
Noitidart Mar 9, 2018
cddf659
index.android.js for buttonColor
Noitidart Mar 9, 2018
8d4b760
Delete prompt_color.xml
Noitidart Mar 9, 2018
97f5166
Create colors.xml
Noitidart Mar 9, 2018
51a4b24
Update style.xml
Noitidart Mar 9, 2018
1dd31e5
Create cust_edit_text.xml
Noitidart Mar 9, 2018
7ee8ae5
how to use cust and also added buttonColor
Noitidart Mar 9, 2018
1293f23
Update index.android.js
Noitidart Mar 9, 2018
0a6c21b
Update index.android.js
Noitidart Mar 9, 2018
9b13e6c
Update RNPromptFragment.java
Noitidart Mar 9, 2018
8a3b945
added manual linking instructions to readme
Noitidart Mar 9, 2018
37f3e0c
Update README.md
Noitidart Mar 9, 2018
aa95548
Update README.md
Noitidart Mar 9, 2018
e8092ab
Update README.md
Noitidart Mar 9, 2018
a08841b
Fixes https://github.com/shimohq/react-native-prompt-android/issues/24
Noitidart Mar 9, 2018
dd428a8
Fixes https://github.com/shimohq/react-native-prompt-android/issues/26
Noitidart Mar 10, 2018
1aac796
fix typo disableFullscreenUI
Noitidart Mar 13, 2018
37ceddd
allow null buttonColor
Noitidart Mar 13, 2018
a3c1199
add screenshot with android options
Noitidart Mar 13, 2018
a1ab4db
add screenshot to readme
Noitidart Mar 13, 2018
3e2e89f
readme
Noitidart Mar 13, 2018
47c5cba
support the full color format (named, rgba, etc) thanks @jeanregisser!
Noitidart Mar 13, 2018
dcc7a0b
remove Color.parseColor as we do processColor now
Noitidart Mar 13, 2018
515e307
swithced to Integer type for colors - tested and it compiles
Noitidart Mar 13, 2018
e724872
better readme
Noitidart Mar 13, 2018
88ea270
added android options of onDismiss and onAny - closes https://github.…
Noitidart Mar 23, 2018
2f428e6
fixed readme typo highlightColor AARRGGBB to rgba
Noitidart Mar 23, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Example/android-options.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
91 changes: 81 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,48 @@ You can use react-native-cli:
react-native link react-native-prompt-android
```

Or rnpm:
```bash
rnpm link react-native-prompt-android
#### Manual Linking
In case `react-native link` fails you can follow this manual linking.

1. Include this module in `android/settings.gradle`:

```
...
include ':react-native-prompt-android' // Add this
project(':react-native-prompt-android').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-prompt-android/android') // Add this
...
include ':app'
```

2. Add a dependency to your app build in `android/app/build.gradle`:

```
dependencies {
...
compile project(':react-native-prompt-android') // Add this
}
```

3. Change your main application to "import" and "add" a new package, in `android/app/src/main/.../MainApplication.java`:

```java
import im.shimo.react.prompt.RNPromptPackage; // Add new import

public class MainApplication extends Application implements ReactApplication {
...

@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new RNPromptPackage() // Add the package here
);
}
}
```

4. Re-compile application using `react-native run-android`

### Usage

```
Expand All @@ -42,17 +79,51 @@ prompt(
);
```

## Props
### Options
The third argument is an object. It can have any of these keys:

| Key | Description | Type | Default |
|---------------------|-----------------------------------------------------------------------------------|---------|------------------------------------------------------|
| type | Text input type: `'numeric', 'secure-text', 'phone-pad', 'email-address'` | String | 'default' |
| cancelable | | Boolean | true |
| defaultValue | Default input value | String | |
| placeholder | | String | |
| style | `'default', 'shimo', 'cust'` | String | 'default' |
| disableFullscreenUI | When in landscape mode, don't use fullscreen | Boolean | false |
| highlightColor | Color of text selection | String | ![colorString](#colorstring) or #RRGGBB or #AARRGGBB |
| placeholderColor | Color of the placeholder | String | ![colorString](#colorstring) or #RRGGBB or #AARRGGBB |
| color | Color of the text | String | ![colorString](#colorstring) or #RRGGBB or #AARRGGBB |
| buttonColor | Color of the buttons | String | ![colorString](#colorstring) or #RRGGBB or #AARRGGBB |

name | description | type | default
:-------------------- |:------------------------------------------- | --------:|:------------------
type | Text input type: `'numeric', 'secure-text', 'phone-pad', 'email-address'` | String | 'default'
cancelable | | Boolean |
defaultValue | Default input value | String | ''
placeholder | | String | ''
##### colorString
Is one of the following: `'red', 'blue', 'green', 'black', 'white', 'gray', 'cyan', 'magenta', 'yellow', 'lightgray', 'darkgray', 'grey', 'lightgrey', 'darkgrey', 'aqua', 'fuchsia', 'lime', 'maroon', 'navy', 'olive', 'purple', 'silver', and 'teal'`
Copy link

@jeanregisser jeanregisser Mar 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could actually support the full color format (named, rgb, rgba, hsl, etc) available in React Native by processing the color passed to the components like this:

import processColor from 'react-native/Libraries/StyleSheet/processColor';

highlightColor: processColor(options.highlightColor),
placeholderColor: processColor(options.placeholderColor),
color: processColor(options.color),
buttonColor: processColor(options.buttonColor)

Then just don't call Color.parseColor in Java but just take the Int value.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sweet! Is it possible to process color on native side? Like RCTConvert in ios?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, RCTConvert on iOS just transforms the int to a UIColor, so it can be conveniently used.
But Android native views can directly deal with Int.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update to support full color format! Super awesome!! would be awesome to learn how to do this on native side too.


##### "cust" Style (change underline, cursor, and handle color)
If you set this style, you can adjust the color of the "underline", "cursor", and "handles" of the input field. The default custom color is a reddish color of "#F34336". You can change this by going to `./node_modules/react-native-prompt-android/android/src/main/res/values/colors.xml` and changing the value of the `custUnderlineAndCursorAndHandleColor` field.

### Screenshots

![Android Screen Shoot](./Example/android.png)

![Android Screen Shoot](./Example/ios.png)

#### Android with options

prompt('Edit Memo', undefined,
[
{ text:'Cancel', style:'cancel' },
{ text:'OK', onPress: text => dispatch(patchEvent(id, { memo:text })) },
],
{
highlightColor: '#80F34336',
color: '#212121',
buttonColor: '#000000',
defaultValue: memo,
style: 'cust'
}
)

And to get the red colors, the field `custUnderlineAndCursorAndHandleColor` in `./node_modules/react-native-prompt-android/android/src/main/res/values/colors.xml` was updated to `#F34336`


![Android Screen Shoot](./Example/android-options.png)
100 changes: 93 additions & 7 deletions android/src/main/java/im/shimo/react/prompt/RNPromptFragment.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package im.shimo.react.prompt;

import android.support.v7.app.AlertDialog;
import android.os.Bundle;
import android.graphics.Color;
import android.content.Context;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.view.inputmethod.InputMethodManager;
import android.text.InputType;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import javax.annotation.Nullable;

Expand All @@ -25,9 +29,16 @@ public class RNPromptFragment extends DialogFragment implements DialogInterface.
/* package */ static final String ARG_STYLE = "style";
/* package */ static final String ARG_DEFAULT_VALUE = "defaultValue";
/* package */ static final String ARG_PLACEHOLDER = "placeholder";
/* package */ static final String ARG_PLACEHOLDER_COLOR = "placeholderColor";
/* package */ static final String ARG_DISABLE_FULL_SCREEN_UI = "disableFullscreenUI";
/* package */ static final String ARG_HIGHLIGHT_COLOR = "highlightColor";
/* package */ static final String ARG_COLOR = "color";
/* package */ static final String ARG_BUTTON_COLOR = "buttonColor";

private EditText mInputText;

private String mButtonColor;

public enum PromptTypes {
TYPE_DEFAULT("default"),
PLAIN_TEXT("plain-text"),
Expand Down Expand Up @@ -95,7 +106,16 @@ public Dialog createDialog(Context activityContext, Bundle arguments) {
builder.setItems(arguments.getCharSequenceArray(ARG_ITEMS), this);
}

AlertDialog alertDialog = builder.create();
if (arguments.containsKey(ARG_BUTTON_COLOR)) {
mButtonColor = arguments.getString(ARG_BUTTON_COLOR);
if (mButtonColor == null) {
mButtonColor = "";
}
} else {
mButtonColor = "";
}

final AlertDialog alertDialog = builder.create();

// input style
LayoutInflater inflater = LayoutInflater.from(activityContext);
Expand All @@ -104,10 +124,15 @@ public Dialog createDialog(Context activityContext, Bundle arguments) {
case "shimo":
input = (EditText) inflater.inflate(R.layout.edit_text, null);
break;
case "cust":
input = (EditText) inflater.inflate(R.layout.cust_edit_text, null);
break;
default:
input = new EditText(activityContext);
}



// input type
int type = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS;
if (arguments.containsKey(ARG_TYPE)) {
Expand All @@ -134,6 +159,21 @@ public Dialog createDialog(Context activityContext, Bundle arguments) {
}
input.setInputType(type);

if (arguments.containsKey(ARG_HIGHLIGHT_COLOR)) {
String highlightColor = arguments.getString(ARG_HIGHLIGHT_COLOR);
if (highlightColor != null) {
input.setHighlightColor(Color.parseColor(highlightColor));
}
}

if (arguments.containsKey(ARG_DISABLE_FULL_SCREEN_UI)) {
boolean disableFullscreenUI = arguments.getBoolean(ARG_DISABLE_FULL_SCREEN_UI);
if (disableFullscreenUI) {
int imeOptions = input.getImeOptions();
input.setImeOptions(imeOptions | EditorInfo.IME_FLAG_NO_EXTRACT_UI);
}
}

if (arguments.containsKey(ARG_DEFAULT_VALUE)) {
String defaultValue = arguments.getString(ARG_DEFAULT_VALUE);
if (defaultValue != null) {
Expand All @@ -143,22 +183,68 @@ public Dialog createDialog(Context activityContext, Bundle arguments) {
}
}


if (arguments.containsKey(ARG_COLOR)) {
String color = arguments.getString(ARG_COLOR);
if (color != null) {
input.setTextColor(Color.parseColor(color));
}
}

if (arguments.containsKey(ARG_PLACEHOLDER)) {
input.setHint(arguments.getString(ARG_PLACEHOLDER));
if (arguments.containsKey(ARG_PLACEHOLDER_COLOR)) {
String placeholderColor = arguments.getString(ARG_PLACEHOLDER_COLOR);
if (placeholderColor != null) {
input.setHintTextColor(Color.parseColor(arguments.getString(ARG_PLACEHOLDER_COLOR)));
}
}
}
alertDialog.setView(input, 50, 15, 50, 0);

mInputText = input;

alertDialog.setOnShowListener(new DialogInterface.OnShowListener()
{
@Override
public void onShow(final DialogInterface dialog)
{
input.requestFocus();
((InputMethodManager) alertDialog.getContext().getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(input, 0);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe below code behave better:
InputMethodManager imm = (InputMethodManager) alertDialog.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
input.requestFocus();
imm.showSoftInput(view, 0);
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there chance that imm is null? If it is, is there a way to wait for it to not be null? Because we always want to show keyboard.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wsong910 - are you waiting on me to make this change before accepting this PR? Or are you researching when imm can be null so we can force get imm in that situation to ensure keyboard shows?

}
});


input.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).performClick();
}
return false;
}
});

return alertDialog;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = this.createDialog(getActivity(), getArguments());
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
return dialog;
}

@Override
public void onStart() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not put code after to line 113?

Copy link
Author

@Noitidart Noitidart Mar 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to. I actually tried to put it on line 91 - https://github.com/Noitidart/react-native-prompt-android/blob/dd428a864880209114ff9708f76e21e9d1937aa5/android/src/main/java/im/shimo/react/prompt/RNPromptFragment.java#L91 - where we initially work with postive button. However d.getButton returns null until after dialog shows. :(

This person here also encountered same issue - https://tassioauad.com/2016/06/02/dialogfragmentalertdialog-dismiss-automatically-on-click-button/

He says:

You will need to set this in the onResume() method because getButton() will return null until after the dialog has been shown! This should cause your custom action method to only be called once, and the dialog won’t be dismissed by default.

I also tried to ask on Stackoverflow, and other people have the same issue:

https://stackoverflow.com/questions/27520967/how-to-change-the-colour-of-positive-and-negative-button-in-custom-alert-dialog/27521470#comment85378535_27521470

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@greedbell @wsong910 would you rather i make the change before accpeting PR? or would you like to find null cases and work around it?

super.onStart();

if (mButtonColor != null && !mButtonColor.isEmpty()) {
AlertDialog d = (AlertDialog) getDialog();
d.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.parseColor(mButtonColor));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you now use processColor on the JS side, you need to remove all Color.parseColor and just take the Int value from the arguments.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

d.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(Color.parseColor(mButtonColor));
d.getButton(AlertDialog.BUTTON_NEUTRAL).setTextColor(Color.parseColor(mButtonColor));
}
}

@Override
public void onClick(DialogInterface dialog, int which) {
if (mListener != null) {
Expand Down
20 changes: 20 additions & 0 deletions android/src/main/java/im/shimo/react/prompt/RNPromptModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public class RNPromptModule extends ReactContextBaseJavaModule implements Lifecy
/* package */ static final String KEY_STYLE = "style";
/* package */ static final String KEY_DEFAULT_VALUE = "defaultValue";
/* package */ static final String KEY_PLACEHOLDER = "placeholder";
/* package */ static final String KEY_PLACEHOLDER_COLOR = "placeholderColor";
/* package */ static final String KEY_DISABLE_FULL_SCREEN_UI = "disableFullscreenUI";
/* package */ static final String KEY_HIGHLIGHT_COLOR = "highlightColor";
/* package */ static final String KEY_COLOR = "color";
/* package */ static final String KEY_BUTTON_COLOR = "buttonColor";

/* package */ static final Map<String, Object> CONSTANTS = MapBuilder.<String, Object>of(
ACTION_BUTTON_CLICKED, ACTION_BUTTON_CLICKED,
Expand Down Expand Up @@ -127,6 +132,15 @@ public void promptWithArgs(ReadableMap options, final Callback callback) {
if (options.hasKey(KEY_CANCELABLE)) {
args.putBoolean(KEY_CANCELABLE, options.getBoolean(KEY_CANCELABLE));
}
if (options.hasKey(KEY_DISABLE_FULL_SCREEN_UI)) {
args.putBoolean(KEY_DISABLE_FULL_SCREEN_UI, options.getBoolean(KEY_DISABLE_FULL_SCREEN_UI));
}
if (options.hasKey(KEY_HIGHLIGHT_COLOR)) {
args.putString(KEY_HIGHLIGHT_COLOR, options.getString(KEY_HIGHLIGHT_COLOR));
}
if (options.hasKey(KEY_COLOR)) {
args.putString(KEY_COLOR, options.getString(KEY_COLOR));
}
if (options.hasKey(KEY_TYPE)) {
args.putString(KEY_TYPE, options.getString(KEY_TYPE));
}
Expand All @@ -139,6 +153,12 @@ public void promptWithArgs(ReadableMap options, final Callback callback) {
if (options.hasKey(KEY_PLACEHOLDER)) {
args.putString(KEY_PLACEHOLDER, options.getString(KEY_PLACEHOLDER));
}
if (options.hasKey(KEY_PLACEHOLDER_COLOR)) {
args.putString(KEY_PLACEHOLDER_COLOR, options.getString(KEY_PLACEHOLDER_COLOR));
}
if (options.hasKey(KEY_BUTTON_COLOR)) {
args.putString(KEY_BUTTON_COLOR, options.getString(KEY_BUTTON_COLOR));
}
fragmentManagerHelper.showNewAlert(mIsInForeground, args, callback);
}

Expand Down
4 changes: 0 additions & 4 deletions android/src/main/res/color/prompt_color.xml

This file was deleted.

5 changes: 5 additions & 0 deletions android/src/main/res/layout/cust_edit_text.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/CustEditTextStyle"/>
5 changes: 5 additions & 0 deletions android/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="custUnderlineAndCursorAndHandleColor">#F34336</color>
<color name="prompt_color">#41464b</color>
</resources>
4 changes: 4 additions & 0 deletions android/src/main/res/values/style.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@
<item name="colorControlActivated">@color/prompt_color</item>
<item name="colorControlHighlight">@color/prompt_color</item>
</style>

<style name="CustEditTextStyle" parent="ThemeOverlay.AppCompat.Light">
<item name="colorAccent">@color/custUnderlineAndCursorAndHandleColor</item>
</style>
</resources>
Loading