how to check if edittext is single line android - android-edittext

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

Related

Vaadin : set value to TextField "from server" doesn't trigger CustomField::generateModelValue

I have implemented CustomField which contains two simple textField.
public class MyCustomField extends CustomField<MyCustomPojo> {
private TextField freeTextField;
private TextField searchTextField;
private Binder<MyCustomPojo> binder;
private MyCustomPojo myCustomPojo;
public MyCustomField(){
this.binder = new Binder<>(MyCustomPojo.class);
this.myCustomPojo = new MyCustomPojo();
freeTextField = new TextField();
searchTextField = new TextField();
searchTextField.addFocusListener(event -> showSearchPopup());
}
public void handleSearchResult(String searchResult){
searchTextField.setValue(searchResult);
this.updateValue();
}
#Override
protected MyCustomPojo generateModelValue() {
binder.writeBeanIfValid(this.customPojo);
return this.customPojo;
}
#Override
protected void setPresentationValue(MyCustomPojo customPojo) {
if(oriasModel != null) {
this.customPojo= customPojo;
binder.readBean(this.customPojo);
}
}
}
The first textField can be freely filled with any user input.
The second textField is a little bit special because when the user focus on it a search dialog is open and the user have to search for a data and then the textfield value is set with Textfield::setValue operation.
My issue is that the operation "generateModelValue" is never call when I set the textfield value from search result. But when I first search for fill "searchTextField" and then input manually the
"freeTextField", then "generateModelValue" is called, and the resulting bean is populated.

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

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

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

Move Focus From PasswordEditField to Next PasswordEditField in blackberry

I am new to BlackBerry and Java. I am trying to figure out but not getting any correct way to implemnt my task. I want to enter 16 digit password. So for that, I have four passwordEditField in HorizontalFieldManager, each passwordEditField allows max 4 digits. When I enter 4 digits in first leftmost passwordEditField, I want to set focus automatically to the following next passwordEditField without any keypress. I used,
passwordEditField = new PasswordEditField("","",4,0){
protected void onUnfocus()
{
this.setFocus(passwordEditField.getIndex()+1,0,0);
}
};
I tried moveFocus(), setFocus(), setCursorPosition() but not getting the focus on to next element. Is there any way i can impelement this task in blackberry.
passwordEditField.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
String text = passwordEditField.getText().toString();
if (text.length() == 4) {
nextPasswordEditField.setFocus();
}
}
});

Resources