I have to two EditTexts (editText1 and editText2 ) and I want the user to enter value inside editText1 first and then inside editText2, in another word I want to make editText2 not editable until the user enter something in editText1.
I tried but it didn't work:
EditText editText1 ;
EditText editText2 ;
String Str1 = editText1 .getText().toString();
if(Str1.matches(""))
{
editText2 .setKeyListener(null);
}
That might meet your requirements.
if(Str1.matches("")) {
editText2.setEnabled(false);
}else{
editText2.setEnabled(true); }
You should use setEnabled() method
if(Str1 == null || Str1.length() == 0)
{
editText2.setEnabled(false);
} else
{
editText2.setEnabled(true);
}
You should put that code into OnKeyListener for the first EditText. This will then enable and disable the second one based on the length of the text in the first one.
You can initially do editText2.setEnabled(false) and then when the user types something in the first editText, you can do editText.setEnabled(true).
You can disable any view element functionality using .setEnabled(false) method.
So, use
EditText.setEnabled(false);
Related
I am working on Android application in which I want to make my textfield editable and clickable. It has multiple TextFields and EditTexts on my screen. I have "EDIT" TextField for which I want to make it clickable and after clicking I want to make other field editable and enable. Without clicking edit Textfield all of them should not be enable.
My code snippet is given below:
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_profile);
fName = (TextView) findViewById(R.id.fnametxt);
lName = (TextView) findViewById(R.id.lastnameTxt);
mailText = (TextView) findViewById(R.id.mailTxt);
mobileText = (TextView) findViewById(R.id.mobileTxt);
dobText = (TextView) findViewById(R.id.dobTxt);
fName.setText(currentUserFirstName);
lName.setText(currentUserLastName);
dobText.setText("");
mobileText.setText(currentUserContactNumber);
mailText.setText(currentUserEmail);
}
You can't convert TextView to EditText, but you can rather use setEnabled property for EditText
You can use editText.setEnabled(true); to make the EditText editable.
Say you are having two edittexts as follows. And on entering data in first edittext, you need to make edittext2 editable, then you can do this:
EditText edittext1, edittext2;
//findViewByIds for both views
editText1.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if(s.length() != 0)
editText2.setEnabled(true);
else
editText2.setEnabled(false);
}
});
Hope this gives a clue how to use it.
EDIT:
Don't get confused between TextField, Button, EditText.
In Android, simple read only field is TextView. Editable textbox is called EditText, and Button is plain Button.
So as per what you are saying, you want tomake EditTexts editable upon clicking of a Button.
Use this:
EditText edittext1, edittext2;
Button button;
//findViewByIds for all views.
buton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
editText1.setEnabled(true);
editText2.setEnabled(true);
}
});
In my opinion, if you want to edit your textfield, then its better to go with EditText. The reason is as follows
The TextView's editable param does make it editable (with some restrictions).
If you set
android:editable="true"
you can access the TextView via the D-pad, or you could add
android:focusableInTouchMode="true"
to be able to gain focus on touch.
The problem is you cannot modify the existing text, and you cannot move the cursor. The text you write just gets added before the existing text.
I am trying to validate text entry in EditText fields. I have setErrors created when no text in entered in a field. The fields are validated when the Submit button is pressed.
If a setError is thrown for an empty field, then text is typed in the field, the setErrorwill disappear when Submit is pressed or when the enter key is pressed in the field - NOT when the user types text in the empty field, then manually taps to another field. The setError for the first field is still active, even though text has been entered.
I'm thinking EditText could be validated as text is typed to fix this issue.
Is this the correct way to do this?
EditText toText = (EditText) findViewById(R.id.to);
EditText subjectText = (EditText) findViewById(R.id.subject);
if (toText.getText().length() == 0) {
toText.setError("Please enter a valid recipient");
}
else {
toText.setError(null);
}
if (subjectText.getText().length() == 0) {
subjectText.setError("Please enter a subject");
}
else {
subjectText.setError(null);
}
So what I'm trying to achieve:
User forgets to enter text in toText
User clicks Submit and the setError message appears
User types text in toText
User taps subjectText
toText still displays the setError
I would like to remove setError in this scenario
toText will remove the setError if:
User clicks Submit again to revalidate the fields
User hits enter on the keyboard to advance to the next field
Actually I did not get you completely. Why dont you use something like this:
//EditText et;
if(et.getText().length() == 0) {
//the field is empty
}
else {
//perform the action
}
alternatively you can use Regular Expressions to validate the input
I have a quick question.
I have a screen with some numbers, when you click one of the numbers, the number gets appended to the end of the edittext.
input.append(number);
I also have a backbutton, when the user clicks this button I want to remove the last character.
At the moment I have the following :
Editable currentText = input.getText();
if (currentText.length() > 0) {
currentText.delete(currentText.length() - 1,
currentText.length());
input.setText(currentText);
}
Is there an easier way to do this ? Something in the line of input.remove()?
I realise this is an old question but it's still valid. If you trim the text yourself, the cursor will be reset to the start when you setText(). So instead (as mentioned by njzk2), send a fake delete key event and let the platform handle it for you...
//get a reference to both your backButton and editText field
EditText editText = (EditText) layout.findViewById(R.id.text);
ImageButton backButton = (ImageButton) layout.findViewById(R.id.back_button);
//then get a BaseInputConnection associated with the editText field
BaseInputConnection textFieldInputConnection = new BaseInputConnection(editText, true);
//then in the onClick listener for the backButton, send the fake delete key
backButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
textFieldInputConnection.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL));
}
});
try this out,
String str = yourEditText.getText().toString().trim();
if(str.length()!=0){
str = str.substring( 0, str.length() - 1 );
yourEditText.setText ( str );
}
I am making my first app in android that is a simple calculator.
When I press button '1' then it shows "1" in EditText, but when I press it again it doesn't show "11", it shows only "1".
How can I fix this?
Since you've offered no code I can only take a stab in the dark and guess that you're calling setText() on your EditText which would overwrite whatever was in there in the first place. What you would need to get is something like:
myEdit.setText(myEdit.getText + "1");
try this code on button click.use append method to add text number in editview as:
EditText editText = (EditText) findViewById(R.id.editText1);
editText.append("1");
and you can use:
editText.setText(editText.GetText() + title);
you are probably replacing the contents of the EditText completely, while what you want to do is append to it ... something like,
oneButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(editText.getText().toString() + "1");
}
}
I am trying to get a message to appear when a button is clicked to tell the user to fill in the blank field. Currently, if the field is blank, it crashes/force closes the app. I tried to do the following code and had zero success. Originally I didn't have the if/else in there, I just ran the calculator(); method and the following imm code.
Could someone point me into the right direction?
public void onClick(View v)
{
if ((EditText)findViewById(R.id.amount1)== null)
{
Context context = getApplicationContext();
CharSequence text = "Enter a number";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
else
{
calculator();
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}
Im pretty sure this is the bad code:
if ((EditText)findViewById(R.id.amount1)== null)
Just dont know how to word it the way I want.
Try checking the length of the text in the EditText widget
EditText e = (EditText)findViewById(R.id.amount1));
if(e.getText().length == 0){
//Show Toast
}else{
//continue your code
}
Use this code.
EditText et = (EditText)findViewById(R.id.amount1));
if(et1.getText().length() == 0){
//Display toast here
} else{
//Your code
}
EditText text = (EditText)findViewById(R.id.amount1);
if(TextUtils.isEmpty(text.toString())) {
// show toast
}
Even if the field is blank, the edittext is not null. Use:
EditText editText = (EditText)findViewById(R.id.amount1);
String text = new String(editText.getText());
if (test.equals("")) {
//...
((EditText)findViewById(R.id.amount1)== null is just getting a reference to the EditText with the id amount1, it is not checking to see if that EditText has a valid entry.
To see if the EditText has text, you can get the String it holds by via EditText#getText().toString()
To make this work, first store the reference to the EditText in a var, then perform your checks on the String:
EditText et = (EditText)findViewById(R.id.amount1);
String amount1 = et.getText().toString();
if (amount1.equals("")) {
// Do your stuff here
}
I'm using local variables and just assuming you want the string to have content. You will likely need to do other checks to handle all the cases (like malformed input). Some of this you can reduce by setting the inputType on the EditText. For example, you might set it to numberDecimal if you are trying to handle only decimal numbers.
You actually want to check if the contents of the EditText are null or an empty string.
The line in question should look something like this:
if("".equals(((EditText)findViewById(R.id.amount1)).getText().toString()))
Of course you may want to break that statement up into more lines to make it a bit more readable!