multiline Text box property android? - android

I've a multiline text box in my app and i want to set a fixed character length so that in one line not more than of that characters appear how to do this?
say if maxlegnth is 150 for textbox then maxlength for single line is to set 50. MAX LENGTH FOR EACH LINE

For TextView elements, or any subclsses of TextView(such as EditText), you can manage this with setFilters().
What you need to do is to create a class implementing InputFilter, for example
public class MyTestFilter implements InputFilter {
#Override
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
// do you checks, and only return what you wish to have replaced
return null;
}
}
and then use
youEditText.setFilters(new InputFilter[]{new MyTestFilter()});

Related

Open the keyboard as numeric without adding InputType.TYPE_CLASS_NUMBER

Have been trying to implement a way for the soft keyboard to open first the Numeric key without saying that the EditText is numeric.
Is this possible ? if yes how ?
The editText btw are added dynamic, so can't do changes on XML files
You can set input type programmatically.
EditText editText=new EditText(this);
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
Edit
You can use InputFilters
public class NumericInputFilter implements InputFilter {
String regEx = new String("^[0-9]+$")
#Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart,
int dend) {
if (source != null && !source.toString().matches(regEx) && !source.toString().equals( "")) {
return "";
}
return source;
}
and on your editText, editText.setFilters(new InputFilter[]{new NumericInputFilter()});
If we set the editText such as editTextField.setRawInputType(Configuration.KEYBOARD_QWERTY);
it will open a keyboard with numeric first and let me choose the normal one and input letters also.

EditText allow only alphabets, digits of all languages

I have an EditText in which i want to allow only alphabets and numbers of any language. I tried with different android:inputType and android:digits in XML.
I tried with set TextWatcher to edittext in which onTextChanged() is like
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
switch (et.getId()) {
case R.id.edtMultiLang: {
et.removeTextChangedListener(watcher2);
et.setText(s.toString().replaceAll("[^[:alpha:]0-9 ]", ""));
// et.setText(s.toString().replaceAll("[^A-Za-z0-9 ]", ""));
et.addTextChangedListener(watcher2);
break;
}
}
}
This is working fine. But whenever i tried to clear text, cursor is moving to start for every letter. Means when i clear a single letter, cursor moving to start.
If i use like android:digits="abcdefghijklmnopqrstuvwxyz1234567890 ", it allows me to type only alphabets and numbers of english. It is not allowing me to enter any other language text As i given only English related alphabets here. But my requirement is to allow copy/paste of other language's alphabets and letters also.
I hope we can do this by using Patterns, TextWatcher and InputFilter. But i didn't find better way.
Please let me know if there any way to do this.
The option you mention to resolve the problem is easy and fast, if you use a filter your code will be like this:
public static InputFilter filter = new InputFilter() {
#Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
String blockCharacterSet = "~#^|$%*!#/()-'\":;,?{}=!$^';,?×÷<>{}€£¥₩%~`¤♡♥_|《》¡¿°•○●□■◇◆♧♣▲▼▶◀↑↓←→☆★▪:-);-):-D:-(:'(:O 1234567890";
if (source != null && blockCharacterSet.contains(("" + source))) {
return "";
}
return null;
}
};
editText.setFilters(new InputFilter[] { filter });

How to make users not to delete suffix in EditText?

i create a form with an EditText that takes in input numberDecimal (i set android:inputType="numberDecimal" in related XML file) and i write the following activity that prevent inserting numbers with more than 2 decimal places (i used .setFilters()). In addition i set a suffix in the same EditText and i would avoid that users can delete this suffix, neither add some input after it. I mean that if users go at the end of the EditText and tries to press backspace button the cursor goes at the begin of the suffix " m" (including space).
How to do that?
public class InputForm extends Activity {
EditText inputField;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.input_form);
inputField = (EditText) findViewById(R.id.editInput);
inputField.setText(" m");
inputField.setSelection(0);
inputField.setFilters(new InputFilter[] {new DecimalDigitsInputFilter(2)});
}
public class DecimalDigitsInputFilter implements InputFilter {
Pattern mPattern;
public DecimalDigitsInputFilter(int digitsAfterZero) {
mPattern = Pattern.compile("[0-9]+((\\.[0-9]{0," + (digitsAfterZero-1) + "})?)||(\\.)?");
}
#Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
int input_length = dest.length() - 2; // lenght of the input without " m"
Matcher matcher = mPattern.matcher(dest.subSequence(0, input_length));
if(!matcher.matches())
return "";
return null;
}
}
}
One way that you can manage keeping the right side of your EditText would be to arrange the layout such that it is composed of 2 EditText views. One with a layout attribute to the right and another with a layout to the left of the suffix EditText filling up the remainder of the layout window.
Then make the second EditText unselectable. If you don't put borders around the EditText and force the text in the window on the left to type from right to left, then you will get the appearance you desire.
You could also keep track of the cursor and then after input you could change the position of the cursor if it is in the wrong location, or check the input for having the suffix that you are expecting and replace it as they type. This will have very little visually noticeable change. But I don't believe there is a built-in way to handle the approach you are requesting.

Edittext InputFilter and autosuggestions

By example, i have an input filter, which block any characters in edititext:
editText.setFilters(new InputFilter[]{new InputFilter() {
#Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
return "";
}
}});
But autosuggestions continue updating, so user can click on thems or press back button and edittext will be filled with filtered data.
Hоw to avoid this strange logic? (Do not change autosuggestions, when characher was filtered) Tested on huawei ascend p6.

Android EditText, detecting characters before user selection when inputing Chinese

Can i detect characters the user is typing before user's touch selection on the Chinese word?
iOS can achieve this.
This is Android, the EditText TextWatcher will get nothing before user selection.
Read the official doc here.
By using a TextWatcher, you'll have access to the afterTextChanged(), beforeTextChanged() and onTextChanged() methods. Now you can see what pinyin the user enters as he enters it and then change your hanzi suggestions as his input changes.
Try this way
InputFilter filter = new InputFilter() {
#Override
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
//source : latest character typed in edittext
//dest : all characters except source
// HERE YOU WILL GET CALLBACK EVERY TIME key ENTERED IN EDITTEXT
return null;
}
};
edtText.setFilters(new InputFilter[]{filter});

Categories

Resources