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

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;
}
});

Related

Android: Restrict editText entry after certain length

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

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;
}
});

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);
}
}
});

Textbox value converts to asterisk symbols like password edit field

I took one custom text box image, in that text box I want to show "Enter password" and when I focus on to that text box field and enter any charectors it converts to asterisk symbols like password edit field.
I want like this below Image:
I think it is possible by only overriding paint(..) of the Editfield. May be following steps can achieve the desired behaviour:
Store original text to a local variable.
Generate fake text depending on focus status, original text etc.
Call super.paint(..), it will paint the field with fake text.
Use setText(..) to set original text back.
Draft implementation:
class MyEditField extends EditField {
private final static String INIT_TEXT = "Enter Password";
protected void paint(Graphics graphics) {
String originalText = getText();
String fakeText = originalText;
if (originalText.equalsIgnoreCase("")) {
fakeText = isFocus() ? originalText : INIT_TEXT;
} else {
fakeText = "";
for (int i=0;i<originalText.length();i++) {
fakeText += '*';
}
}
setText(fakeText);
super.paint(graphics);
setText(originalText);
}
}
Above code doesn't paint a border.
We need to set a monochrome font for the problem you have mentioned in comment. Here is a sample code:
MyEditField me = new MyEditField();
try {
Font font = null;
FontFamily fontFamily[] = FontFamily.getFontFamilies();
for (int i=0;i<fontFamily.length; i++) {
if (fontFamily[i].getName().equalsIgnoreCase("Courier New")) {
font = fontFamily[i].getFont(FontFamily.SCALABLE_FONT,
Font.getDefault().getHeight());
me.setFont(font);
break;
}
}
} catch (Exception e) {
}
If I understand you correctly, you need to use a PasswordEditField.
Implement it just like any LabelField, and it will do what you need.
I see you wanted the background text too - I would use drawText() from within the paint() method to draw that. The other answer that shows a full implementation by using EditField is also good.

Resources