Android: Restrict editText entry after certain length - android-edittext

Hi I am using a text watcher on edit text
I want to restrict the number after certain length
suppose i want restrict length for 3
It should not allow 1234
but it should allow 123.(except dot(.) it should not allow any other character)
How can i do with TextWatcher.
Also tried to restrict all key entries except dot but not working
insideEdit.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == 46) // dot key code is 46
{
return false;
}else {
return true;
}
}
});

Use
android:maxLength="4"
inside <EditText tag

Related

Vaadin can't get combo box field value at 0 position

I'm trying to set field at index 0 in Vaadin combo box to default value, so I could avoid error message if user doesen't select anything. So I would like that instead of blank field I have populated field at index 0.
I have tried to set it and managed it with this:
field.setNullSelectionAllowed(true);
field.setNullSelectionItemId(container.getIdByIndex(0));
So I don't have blank value at index 0, instead my previous value of index 1 is now at index 0. And that is exactly what I want and need and in combo box looks just as I want.
But, unfortunately, when I submit my form, value is not passed. Only values after index 0 are passed. It's so frustrating, can somebody help me? Value passed to setNullSelectionItemId exists 100%.
How can I grab value from index at place 0 in combo box?
p.s. here is my code:
public Field<?> buildAndBindComboBox(final String caption, final BeanItemContainer<?> container,
final Object propertyId, final String title, final ValueChangeListener listener, final boolean nullAllowed,
final boolean required, final boolean enabled, final boolean visible) {
#SuppressWarnings("serial")
ComboBox field = new ComboBox(caption, container) {
// http://dev.vaadin.com/ticket/10544
// - typing in ComboBox causes Internal Error
private boolean inFilterMode;
#Override
public void containerItemSetChange(com.vaadin.data.Container.ItemSetChangeEvent event) {
if (inFilterMode) {
super.containerItemSetChange(event);
}
}
#Override
protected List<?> getOptionsWithFilter(boolean needNullSelectOption) {
try {
inFilterMode = true;
return super.getOptionsWithFilter(needNullSelectOption);
} finally {
inFilterMode = false;
}
}
};
field.setStyleName("comboBox");
field.setInputPrompt("Select");
if(defaultValue == true){
field.setNullSelectionAllowed(false);
field.setNullSelectionItemId(container.getIdByIndex(0).toString());
//field.select(container.getIdByIndex(0));
//field.setValue(container.getIdByIndex(0));
//field.setRequired(false);
defaultValue = false;
} else {
field.setNullSelectionAllowed(nullAllowed);
field.setRequired(required);
}
field.setImmediate(true);
field.setNewItemsAllowed(false);
field.setFilteringMode(FilteringMode.CONTAINS);
if (title != null) {
field.setItemCaptionPropertyId(title);
}
//field.setNullSelectionAllowed(nullAllowed);
//field.setRequired(required);
field.setVisible(visible);
if (listener != null) {
field.addValueChangeListener(listener);
}
this.bind(field, propertyId);
field.setEnabled(enabled);
return field;
}
public void setDefaultValueFirstItem(boolean def){
defaultValue = def;
}
It is binded like this:
commitmentFeeBinder.setDefaultValueFirstItem(true);
commitmentFeeBinder.buildAndBindComboBox("No working day labels", noWorkingDays, "noWorkingDaysCF", "title", null, false, !transaCF, true, !transaCF);
If I understood your question correctly, Steffen Harbich is correct in suggesting that if you want the first item to be selected by default you should disable null selection and select the first item by default. E.g. this works:
ComboBox cb = new ComboBox("", Arrays.asList("First", "Second", "Third"));
cb.setNullSelectionAllowed(false);
cb.select("First");
Or alternatively with a BeanItemContainer:
List<MyBean> beans = Arrays.asList(new MyBean("First"), new MyBean("Second"), new MyBean("Third"));
BeanItemContainer<MyBean> bic = new BeanItemContainer<>(MyBean.class, beans);
ComboBox cb = new ComboBox("", bic);
cb.setNullSelectionAllowed(false);
cb.select(bic.getIdByIndex(0));
private void resetComboBoxToIndex(ComboBox combo, int index) {
BeanItemContainer<Bean_ComboBox> items_combo = (BeanItemContainer<Bean_ComboBox>)combo.getContainerDataSource();
if(items_combo != null && items_combo.size() > index) {
Bean_ComboBox primerItem = items_combo.getIdByIndex(index);
if(primerItem != null) {
combo.select(primerItem);
}
}
}

how to check if edittext is single line android

I am making a custom android keyboard. I want to make it sure that when user press enter key proper EditorAction took place. Therefore I want to know is there any way to check if edittext is single line or not? Or is there any way to check min and max lines of editext?
In your onStartInput(EditorInfo info) and onStartInputView(EditorInfo info) callbacks EditorInfo is passed in.
To check if it's a multiline you can do:
if (0 != (info.inputType & InputType.TYPE_TEXT_FLAG_MULTI_LINE)) {
//This is multiline text field
}
See TYPE_TEXT_FLAG_MULTI_LINE
You can implement it by setting a key listener and and checking getMaxLines() and getMinLines() properties. Note that it is applied for API > 15!
EditText editText = new EditText(this);
editText.setOnKeyListener(new View.OnKeyListener()
{
#Override
public boolean onKey(View view, int keyCode, KeyEvent keyEvent)
{
if (view instanceof EditText && ((EditText) view).getMaxLines() == 1 && ((EditText) view).getMinLines() == 1)
{
// this is a single line EditText view
}
return false;
}
});

How to make Numeric Edit Text default to blank and then use getText

Please Help!I have Edit Text field whose inputType is number. On the click of Submit button in the firstActivity I want to send the value via Bundle to next activity. I want to make the field compulsory. Before sending the value I am checking if the EditText is blank. if its blank I want to give a Toast message that "Please enter number". I am using below code line to get the numeric text
editTextValue= (EditText) findViewById(R.id.edit_attendance);
editTextValue.setText("0");
var1 = Integer.valueOf((editTextValue.getText().toString()));
For this code to work I have to first set the default value to 0
But due to this when user is entering value in this EditText, he has to first erase 0 and then enter number. I dont want to set it to 0 value. Is there any other way to make this work.
Below is my Code in XML:
<EditText
android:id="#+id/edit_1"
android:layout_marginLeft="5sp"
android:layout_width="100sp"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="0"
android:textColor="#android:color/white" />
FirstActivity.java
onCreate(){
editTextValue = (EditText) findViewById(R.id.edit_1);
editTextValue.setText("0");
btnSubmit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
var1 = Integer.valueOf((editTextValue.getText()
.toString()));
if (var1 == 0) // when no value is entered
Toast.makeText(getApplicationContext(),
"Please enter value",
Toast.LENGTH_SHORT).show();
else
{
Intent i = new Intent(getApplicationContext(),
SecondActivity.class);
dataBundle.putInt(SecondActivity.VAR1,var1);
i.putExtras(dataBundle);
startActivity(i);
}
}
The below code worked for me:
// editTextValue.setText("0");
editTextValue.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
// TODO Auto-generated method stub
if (actionId == EditorInfo.IME_ACTION_DONE) {
var1 = Integer.valueOf((editTextValue
.getText().toString()));
}
return false;
}
});

Comma separation in the Text Field in Blackberry

in my application i have a Custom text box with BasicEditField.FILTER_NUMERIC. When the user enter the value in the field the comma should be added to the Currency format .
EX:1,234,567,8.... like this.
In my code i tried like this.
protected boolean keyUp(int keycode, int time) {
String entireText = getText();
if (!entireText.equals(new String(""))) {
double val = Double.parseDouble(entireText);
String txt = Utile.formatNumber(val, 3, ",");// this will give the //comma separation format
setText(txt);// set the value in the text box
}
return super.keyUp(keycode, time);
}
it will give the correct number format... when i set the value in the text box it will through the IllegalArgumentException. I know BasicEditField.FILTER_NUMERIC will not allow the charector like comma(,)..
How can i achieve this?
I tried this way and it works fine...
public class MyTextfilter extends TextFilter {
private static TextFilter _tf = TextFilter.get(TextFilter.REAL_NUMERIC);
public char convert(char character, int status) {
char c = 0;
c = _tf.convert(character, status);
if (c != 0) {
return c;
}
return 0;
}
public boolean validate(char character) {
if (character == Characters.COMMA) {
return true;
}
boolean b = _tf.validate(character);
if (b) {
return true;
}
return false;
}
}
and call like this
editField.setFilter(new MyTextfilter());

set text(number) to Edit Field through its setchangelistener

Hi I want to accept number in edit field up to two decimal.So I am setting listener to it Then I
am checking whether number is two decimal or more and if it is more than two decimal then i am
truncating the number and again trying to set the truncated number.But it showing 104 error at this place interestRate.setText(text).My code is
interestRate=new EditField();
interestRate.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
String text=interestRate.getText().toString();
code here--
interestRate.setText(text);
}
};
So my question is whether text can be set from its listener or not
Looks like you just need to use some condition check in order not to get in an infinite loop:
interestRate=new EditField();
interestRate.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
String text = interestRate.getText().toString();
// code here to create a truncated text
if (!truncated.equals(text)) {
// next time we will not get here
// because truncated will be equal to text
interestRate.setText(text);
}
}
});

Resources