I have an android phone using googles keyboard. On any EditText field in any other app if I use the swipe method to enter text in, it adds a space after each word. However, I have written my own app and when I use the swipe method to enter text on my EditText field it does NOT add a space sothewordsbleedtogether. This is very annoying.
I have an AlertDialog with a linear view added. On that linear view there is a text EditText. Here is my code to create the EditText and add it to the view:
final EditText titleBox = new EditText(this);
titleBox.setInputType(InputType.TYPE_TEXT_FLAG_AUTO_CORRECT |
InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES |
InputType.TYPE_TEXT_VARIATION_SHORT_MESSAGE);
titleBox.setHint("Title");
layout.addView(titleBox);
Any ideas why its not adding spaces in between my words?
This was marked as a possible duplicate, but that question was about not allowing the first character to be a space....Im asking about allowing a space after words that are entered via a keyboard swipe.
Update
Here is the entire method of similar page, its having the same issue, its slightly less complex then the initial page I was troubleshooting. This one doesn't even have a LinearLayout associated:
private void addBudget(final Budget budget) {
EditText taskEditText = new EditText(this);
taskEditText.setId(R.id.add_budget_text);
taskEditText.setInputType(InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
String dialogTitle = "Add budget";
final AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle(dialogTitle)
.setMessage("What do you want to call this budget?")
.setView(taskEditText)
.setPositiveButton("Save", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// final String task = ;
SQLiteDatabase db = mHelper.getWritableDatabase();
Budget lBudget = new Budget();
if (budget != null) {
lBudget = budget;
}
EditText taskEditText = (EditText) findViewById(R.id.add_budget_text);
lBudget.title = String.valueOf(taskEditText.getText());
// Init custom budget object //new Budget(){{ title=task; id = budgetID;}}
int retId = mHelper.saveBudget(db, lBudget);
db.close();
int retRow = updateUI(retId);
mTaskListView.smoothScrollToPosition(retRow);
}
})
.setNegativeButton("Cancel", null)
.create();
// Handle done on soft keyboard
taskEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
int result = actionId & EditorInfo.IME_MASK_ACTION;
if (result == EditorInfo.IME_ACTION_DONE) {
dialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick();
return true;
}
return false;
}
});
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
dialog.show();
}
I didnt know if you got solved, i just had the same problem today and found a way to solve it.
I saw a "extrange" performance of the swipe, sometimes it showed the "blankspace" and sometimes not.
The way i found to check if it was shown and if it didnt, add it, was this:
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
checkCancel();
int compare = count-before;
if(compare>1){
String text = editText.getText().toString();
String lastChar = (text.substring(text.length()-1,text.length()));
if(!lastChar.equals(" ")){
String plus = text+" ";
editText.setText(plus);
editText.setSelection(editText.getText().length());
}
}
}
#Override
public void afterTextChanged(Editable s) {
}
} );
You can see, onTextChanged can use the variables "before" and "count" and if the compare (difference between last word and current one) is more than 1, it's a word entered by Swipe. Then you can check if the "blankspace" is shown, and if not, just add it and perfom anything you want with it.
Hope it helps!
Could you try this? Add the filter into the editText. I used it for enter code on my app.
EditText et = (EditText) findViewById(R.id.edit_text);
et.setFilters(new InputFilter[] {
new InputFilter() {
#Override
public CharSequence filter(CharSequence charSequence, int start, int end, Spanned spanned, int start, int end {
if(charSequence.equals("")){
return charSequence;
}
if(charSequence.toString().matches("[a-zA-Z ]+")){
return charSequence;
}
return "";
}
}
});
So I uninstalled the google keyboard and reinstalled and I changed the build to release. One of those two things fixed it.
Related
I have 9 edittext. Each edittext is in the form of a square. I look if all edittext has values, then an alert message is displayed without click of any button.
I tried with this code but it does not run.
Any help would be appreciated.
public int Summ(int x, int y, int z) {
int sum = 0;
sum = x + y + z;
return sum;
}
private void alertDialogLost()
{
int a= Integer.parseInt(et1.getText().toString());
int b = Integer.parseInt(et2.getText().toString());
int c = Integer.parseInt(et3.getText().toString());
int d = Integer.parseInt(et4.getText().toString());
int e = Integer.parseInt(et5.getText().toString());
int f = Integer.parseInt(et6.getText().toString());
int g = Integer.parseInt(et7.getText().toString());
int h = Integer.parseInt(et8.getText().toString());
int k = Integer.parseInt(et9.getText().toString());
if ((Summ(a,b,c)== Solution)&&(Summ(d,e,f)== Solution)&&(Summ(g,h,k)==Solution)&&
(Summ(a,d,g)==Solution)&&(Summ(b,e,h)== Solution)&&(Summ(c,f,k)==Solution)
&&(Summ(a,e,k)==Solution)&&(Summ(c,e,g)==Solution))
{
AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity.this);
View view1 = LayoutInflater.from(MainActivity.this).inflate(R.layout.alertdiag, null);
TextView title = (TextView) view1.findViewById(R.id.title);
TextView message = (TextView) view1.findViewById(R.id.message);
ImageView icone = (ImageView) view1.findViewById(R.id.icone);
title.setText("Result");
icone.setImageResource(R.drawable.smilega);
message.setText("you have winner");
builder1.setPositiveButton("contenue", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
finish();
}
});
builder1.setView(view1);
builder1.setCancelable(false);
AlertDialog alertDialog1 = builder1.create();
alertDialog1.show();
}
}
If you just want to show an AlertDialog the moment all nine EditText fields have values in them, using a TextWatcher would probably do the trick.
First, let's start with making things easier on ourselves. We'll add each EditText to an ArrayList, so we can iterate through them with a forEach loop:
List<EditText> editTextArrayList= new ArrayList<>();;
editTextArrayList.add(et1);
editTextArrayList.add(et2);
editTextArrayList.add(et3);
editTextArrayList.add(et4);
editTextArrayList.add(et5);
editTextArrayList.add(et6);
editTextArrayList.add(et7);
editTextArrayList.add(et8);
editTextArrayList.add(et9);
Then, let's set up a method to iterate through all nine EditText fields, checking if each one has a value. If any of them do not, the AlertDialog will not show:
private void checkAllEditTexts() {
boolean allFilled = true;
for (EditText editText : editTextArrayList) {
if (editText.getText().toString().isEmpty()) {
allFilled = false;
break;
}
}
if (allFilled) {
// show your AlertDialog
}
}
Then we set up our TextWatcher, which will call the checkAllEditTexts() method if any text is changed on the EditText fields we'll be assigning it to:
private TextWatcher textWatcher = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
checkAllEditTexts();
}
};
And finally, just below where we added the EditText fields to the ArrayList, we set up a forEach loop to add the TextWatcher:
List<EditText> editTextArrayList= new ArrayList<>();;
editTextArrayList.add(et1);
editTextArrayList.add(et2);
editTextArrayList.add(et3);
editTextArrayList.add(et4);
editTextArrayList.add(et5);
editTextArrayList.add(et6);
editTextArrayList.add(et7);
editTextArrayList.add(et8);
editTextArrayList.add(et9);
for (EditText editText : editTextArrayList) {
editText.addTextChangedListener(textWatcher);
}
...and that should display your AlertDialog as soon as all nine text fields have a value.
I create a table with a switch and a edit text fields. What I want to happen is to set the switch to enable when I entry a certain text in the edit text field that is on the same tablerow So How to I set the switch in says row 8 to enable if I enter text in edit text row 8.
for (int i = 0; i< dbarray_id.size(); i++ )
{
CODE IS HERE TO CREATE A TABLEROW
//Now add a switch to the row
Switch switch1 = new Switch(getActivity());
switch1.setId(i);
switch1.setTag(i);
switch1.setSwitchMinWidth(50);
switch1.setEnabled(false)
//Add a edittext field to the row
final EditText txtaccesscode = new EditText(getActivity());
txtaccesscode.setId(i);
txtaccesscode.setTag(i);
txtaccesscode.setHint("CODE?");
txtaccesscode.setInputType(TYPE_TEXT_FLAG_NO_SUGGESTIONS);
txtaccesscode.addTextChangedListener(new TextWatcher()
{
#Override
public void beforeTextChanged(
....etc... for after change ...
repeat for the number of rows in the database.
So when I enter text into any row the corresponding switch is enabled.
Thanks
Encapsulate both EditText and Swith references in a class, then when textWatcher fires both references will be the expected.
public static class SwitchToggleListener {
public SwitchToggleListener(final Switch switchView, final EditText editText) {
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {
//Do nothing
}
#Override
public void onTextChanged(final CharSequence s, final int start, final int before, final int count) {
//Your own condition here
switchView.setEnabled(s.toString().equals("ok"));
}
#Override
public void afterTextChanged(final Editable s) {
//Do nothing
}
});
}
}
Then in your loop do this:
new SwitchToggleListener(switch1, editText1);
new SwitchToggleListener(switch2, editText2);
new SwitchToggleListener(switch3, editText3);
If you have too many rows maybe this is inneficient, there are other ways to do this, let me know if that is the case.
The switch need to be final or a field.
final Switch switch = new Switch(getActivity());
Then you can access it on your beforeTextChanged listener, in there you change like always.
txtaccesscode.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(){
switch.setEnable(true);
}
}
);
After the loop reaching the end you can't access the variable anymore but it still exist. And on the next iteration of the loop the switch you create even having the same name is a different switch, because it's a new scope.
I want to create a login page, I used EditText to insert user info. I want to check EditText to see if it is Empty Invisible login Button, when inserted any character with user visible Login Button.
I tried the code shown below, but it did not not work for me :
//Show Login Button
String login_phoneString = login_PhoneText.getText().toString().trim();
if (login_phoneString.isEmpty()) {
login_image.setVisibility(View.INVISIBLE);
} else {
login_image.setVisibility(View.VISIBLE);
}
When EditText is empty, the button is invisible, and when set character in EditText again the login button is not shown.
How can I fix this problem ?
You want to show/hide the Login button base on the text of EditText so you need to listen for changing in EditText by use TextWatcher.
Use this code inside onCreate() method
login_PhoneText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
String login_phoneString = login_PhoneText.getText().toString().trim();
if (login_phoneString.isEmpty()) {
login_image.setVisibility(View.INVISIBLE);
} else {
login_image.setVisibility(View.VISIBLE);
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
Using trim()
if(et.getText().toString().trim().length() == 0) //empty
Using TextUtils
if(TextUtils.isEmpty(et.getText().toString().trim()) //Empty
Using isEmpty()
if(et.getText().toString().isEmpty()) //Empty
EDIT
You can do this :
//Show Login Button
String login_phoneString = login_PhoneText.getText().toString().trim();
if (TextUtils.isEmpty(login_phoneString) {
login_image.setVisibility(View.INVISIBLE);
} else {
login_image.setVisibility(View.VISIBLE);
}
Try to check like this way
EditText edt = (EditText) findViewById(R.id.edittext);
String abc = edt.getText().toString();
if (abc.matches("")) {
Toast.makeText(this, "enter something", Toast.LENGTH_SHORT).show();
login_image.setVisibility(View.INVISIBLE);
return;
}
else
{
login_image.setVisibility(View.VISIBLE);
}
String login_phoneString = login_PhoneText.getText().toString().trim();
if (login_phoneString.equals("")) {
login_image.setVisibility(View.GONE);
} else {
login_image.setVisibility(View.VISIBLE);
}
Use View.GONE instead of INVISIBLE. when INVISIBLE it is still clickable.
Use TextUtils.isEmpty():
if (TextUtils.isEmpty(yourEditText.getText().toString())) {
//Empty
} else {
//Not empty
}
I have user keyboard input working on all android versions except on Android Lollipop (5.0).
I have used this to open software keyboard:
public static void OpenKeyBoard(){
MainActivity._Instance.runOnUiThread(new Runnable() {
public void run() {
InputMethodManager imm = (InputMethodManager) MainActivity._Instance.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(MainActivity._Instance.getWindow().getDecorView(), 0);
}
});
}
and i get user input by standard event :
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
I have this code working for all pre Lollipop versions of Android. When I use it on Lollipop, the software keyboard appears, but when I try to click on any letter/number, the keyboard disappears and the method "onKeyDown" doesn`t receive any keycode.
Did anyone had this problem? Any opinion how to solve this?
Thank you.
Try updating your google SDK to the latest, I did and this fixed any issues I have with the keyboard dismissing.
I has this problem too. I managed to solve this by overriding the onLayout, onFocusChanged & onCheckIsTextEditor methods inside WebView. Here's the code that made it work (I generate a webview programmatically):
this.webView = new WebView(context)
{
boolean layoutChangedOnce = false;
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b)
{
if (!layoutChangedOnce)
{
super.onLayout(changed, l, t, r, b);
layoutChangedOnce = true;
}
}
#Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect)
{
super.onFocusChanged(true, direction, previouslyFocusedRect);
}
#Override
public boolean onCheckIsTextEditor()
{
return true;
}
};
this.webView.setFocusable(true);
this.webView.setFocusableInTouchMode(true);
this.webView.requestFocus(View.FOCUS_DOWN);
addView(this.webView);
this issue is not limited to just android applications but also occurs when any keyboard related event is accessed using javascript or jquery within a phone browser.
eg. I have my website which asks a user to enter zip code(numeric characters) on a certain page. Now the problem is when up tap on the input box, the numeric keyboard appears but it allows you to paste alpha-numeric characters in it as well.
Reason searched so far,
Try accessing this through your desktop and also a mobile device running android OS 5.0+ and try different possible keystrokes.
Note:
When you enter any alpha-characters using mobile device with above
mentioned configuration it shows up '229' as the keycode.
When you enter any alpha-characters using a desktop it shows up the
appropriate keycode.
This works for me I have a SurfaceView implements SurfaceHolder.Callback with a thread to do drawing
#Override
public boolean onTouchEvent(MotionEvent event)
{
synchronized (this)
{
mouse.useEvent(event);
}
return true;
}
#Override
public InputConnection onCreateInputConnection(EditorInfo editorinfo) {
BaseInputConnection bic = new BaseInputConnection(this, false);
editorinfo.actionLabel = null;
editorinfo.inputType = InputType.TYPE_NULL;
editorinfo.imeOptions = EditorInfo.IME_FLAG_NO_FULLSCREEN;
bic.finishComposingText();
return bic;
}
#Override
public boolean dispatchKeyEvent(KeyEvent event)
{
input.useEvent(event);
return super.dispatchKeyEvent(event);
}
I have solved this using AlertDialog editText programmatically. Eg:
public void KeyboardTextField(int id){
if (id == 0){
final String title = getStringResourceByName("profile_title");
final String createP = getStringResourceByName("profile_confirm");
final String cancel = getStringResourceByName("profile_cancel");
MainActivity._Instance.ActivityWillBeShown = true;
MainActivity._Instance.runOnUiThread(new Runnable() {
public void run() {
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity._Instance);
alert.setCancelable(false);
alert.setTitle(title);
// alert.setMessage(""); // message
// Set an EditText view to get user input
final EditText input = new EditText(MainActivity._Instance);
final int maxLength = 12;
input.setFilters(new InputFilter[] {new InputFilter() {
#Override
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
if (source != null && blockCharacterSet.contains(("" + source))) {
return "";
}
// TODO Auto-generated method stub
return null;
}
} , new InputFilter.LengthFilter(maxLength)});
//input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_CLASS_NUMBER);
input.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS); //turn off txt auto complete
input.setInputType(InputType.TYPE_TEXT_VARIATION_POSTAL_ADDRESS); // type only text,numbers and some special char
alert.setView(input);
//input.setText("Player");
alert.setPositiveButton(createP, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString();
// Do something with value!
if (value.isEmpty()){
value = "Player";
}
nativeName(value);
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(input.getWindowToken(), 0);
}
});
alert.setNegativeButton(cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(input.getWindowToken(), 0);
}
});
alert.show();
};
});
}
And Whenever you need keyboard you just call this function. It works on all Android versions (4.x, 5.x, 6.0)
I know that there are quite some questions out here regarding this question. But non of them have the answer that I'm looking for.
I've got 7 ET inside a ScrollView. When I start the application no ET has focus because I've added the following two lines to my overall layout:
android:focusable="true"
android:focusableInTouchMode="true"
When I click on an ET the softkeyboard is shown, which I want, then I set the value (let say 20). I press a '2' followed by a '0' and then press the back button. At this point the keyboard disappears, but the focus stays. I would like to clear the focus too when pressing the back button to hide the keyboard.
Because when the focus is cleared the layout of the ET is set like I want.
They all have about the some code, which is:
// Right Cable
RightCable = (EditText) findViewById (R.id.RightCable);
RightCable.setRawInputType(Configuration.KEYBOARD_12KEY);
RightCable.setOnFocusChangeListener(FocusChanged);
RightCable.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
if(RightCable.isFocused()){
LengthRightCable = Double.parseDouble(RightCable.getText().toString());
Calculate();
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(s.toString().matches("")) {
RightCable.setText("0.00");
Selection.setSelection(RightCable.getText(), 0, 4);
}
}
});
I use a focus listener to change the input of the ET to a number like 5.00 instead of 0.
OnFocusChangeListener FocusChanged = new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
EditText et = (EditText) v;
et.setSelection(0, et.getText().length());
if(!hasFocus){
String userInput = et.getText().toString();
int dotPos = -1;
for (int i = 0; i < userInput.length(); i++) {
char c = userInput.charAt(i);
if (c == '.') {
dotPos = i;
}
}
if (dotPos == -1){
et.setText(userInput + ".00");
} else if(userInput.length() < 5) {
if ( userInput.length() - dotPos == 1 ) {
et.setText(userInput + "00");
} else if ( userInput.length() - dotPos == 2 ) {
et.setText(userInput + "0");
}
}
}
}
};
Just override `onBackPressed()`
method in your activity and check condition for Edittext
focus before run
super.onBackPressed();
#Override
public void onBackPressed() {
if( et_search.isFocused()){
et_search.clearFocus();
}else {
super.onBackPressed();
}
}
For this you have to take the onTouchListener on the parent layout of the Layout File. on the TouchListener you have to code to hide the Keyboard when click outside the EditText. Please follow following XML Layout and Java class to resolve this issue.
Please follow the follow the following url to resolve this issue http://amitthaperandroidquery.blogspot.com/2011/10/remove-keyboard-after-click-outside.html