I am currently trying to work with AlertDialogs.
Currently, I have an EditText in my AlertDialog, and want to restrict the input to only Integers. I have used a basic try and catch block to avoid the app crashing form a NumberFormatException.
However, I want to set it up so that when the user tries to press the button with the incorrect input, the input does not register and the Dialog is not cancelled.
UpperLimitDialog.setPositiveButton(R.string.Positive_Button, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int i) {
int RawInput = settings.getInt("UpperLimit", 12);
try {
RawInput = Integer.parseInt(input.getText().toString());
}catch (NumberFormatException se) {
Toast.makeText(SettingsMenu.this, "Non-Integer Value, No Input", Toast.LENGTH_SHORT).show();
//Here I want the app to not register the click and prevent the dialog box from closing.
}
editor.putInt("UpperLimit", RawInput);
editor.apply();
Toast.makeText(SettingsMenu.this, "Set Upper Limit to " + RawInput, Toast.LENGTH_SHORT).show();
}
});
What method can I use to achieve this?
The basic trick here is to use the dialog's setCancelable to false and call dismiss() only when the input has been validated.
More info here: AlertDialog with positive button and validating custom EditText
Use can acheive input to only Integers this by both Java or XML.
Java
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
XML
android:inputType = "numberSigned"
this may helps you in both way.
If you restrict user to allow only number then you can simply try with-
android:inputType="number"
and if you want to allow with floating number then try with-
android:inputType="numberDecimal"
and in any case if you want to disable dialog button then try this-
Button theButton = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
theButton.setEnabled(false);
will work for you :)
Simply use in EditText's xml code:
android:inputType="numberSigned"
This will let user input only integers. Hope this helps!
Related
Hi i'm using this code when user click on an edittext to clear it.
The Problem is that it doesnt work properly.I mean that i must click my EditText two times for clear it. In first click it opens Keyboard and in second click, it runs my code:
Here is my code:
var comments = FindViewById<EditText>(Resource.Id.txtExtrasComment);
comments.Click+=delegate
{
comments.Text = "";
};
How Can i fix this?
I found my answer with another way. i just added in my edittext
android:hint="comments..."
if you want edit in .cs file you use in onCreate()
...
editText.setOnClickListener(this);
...
public void onClick(View v)
{
editText.setText("");
}
other way in .cs
editText.getText().clear();
if you want edit axml page, you use placeholder properties. You have wrote already up.
android:hint="Enter number"
Try this:
From the forum
You can use a onTouchListener on the edittext to clear the data inside it.
myEditText.Click += (sender, eventArgs) => {
// do my business
// Add the line below
(sender as EditText).OnTouchEvent(eventArgs.Event);
};
I have an EditText and a Button.
I want if an EditText was empty when clicked on my Button. I want to show message as a toast, like "please enter a number".
You can do something like this:
boolean hasValue = editText.getText().length() > 0;
or
boolean hasValue = !editText.getText().toString().isEmpty();
or to make sure it doesn't contain only spaces:
boolean hasValue = !editText.getText().toString().trim().isEmpty();
The cleanest way to do this is TextUtils.isEmpty(editText.getText())
The reason I say this is the cleanest way is because:
You avoid pointless conversion between CharSequence and String. Which creates an object. editText.getText() returns Editable, calling toString() creates an additional object which is not good. This method will also never return null in my experience.
You get a null and a length check out of that. If you look at the code for TextUtils.isEmpty(), it basically checks if the CharSequence is null and length is zero.
It avoids code duplication and the same method can be used with Strings or CharSequence objects and Editable is an implementation of CharSequence.
It's provided as part of the Android framework.
If you want to check the length of the trimmed String. Then use:
TextUtils.isEmpty(editText.getText())
&& TextUtils.getTrimmedLength(editText.getText()) == 0
If you want, you can create your own utility method to do this so you don't have to add such a long condition in your code repeatedly.
I would attached an OnFocusChangeListener to your EditText to check the change in value or a TextWatcher or both depending on how critical your requirement is. If your field had focus and lost it, do your validation with the OnFocusChangeListener, if your field has focus and the user is typing and delete the content or the content is too short, use TextWatcher to let them know.
Use this on click of your button:
EditText editText = (EditText) view.findViewById(EditTextID);
if(editText.getText().toString().length()==0) {
Toast alert = Toast.makeText(context, toast_message, Toast.LENGTH_SHORT);
alert.show();
}
In the onClickListener() of the button:
int length = editText.getText().length();
if(length == 0)
{
Toast.makeText(getActivity(), "Please enter a number",Toast.LENGTH_LONG).show();
}
you probably already found your answer, but for the ones who came here hoping to find an answer here is how its done:
you have to make a String object or Int object first then in your button function Click write this:
#Override
public void onClick(View view) {
String numberValue;
numberValue = yourEditText.getText().toString();
if (emailEtValue.matches("")){
Snackbar sbEmptyValue = Snackbar.make(view, "You must enter an Integer Value", Snackbar.LENGTH_LONG);
sbEmptyValue.show();
} else {
//DO THE THING YOU WANT
}
}
you can also use Toast but i prefer Snackbar because its cooler than Toast.
I have certain edit text fields. I save the data entered in these fields to my database, which opens in another activity.
But I have a problem when I navigate back to the first activity (Using the back button on the hardware of the emulator) to add next record, the edit field data is retained.
I tried onPause() and myEditText.setText("") also. But the dat simple clears off the edit fields but as soon as I click the fields to enter data again the previous data reappears.
I also tried using finish() and everything works except I have to go through all the activities again to enter the data.
Try this one also.
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String uid=editText1.getText().toString();
String pwd=editText2.getText().toString();
editText1.setText("");
editText2.setText("");
}
});
}
Just to make sure that I understood you clearly. You enter text in EditText boxes. Then press a button which takes you to a new activity. But, when you go back to the old activity the text in the EditText boxes doesn't get clear. Try to do editText.setText(""); when you click the button. I know you said that you tried it, but did you try it inside the function which listens for the button click?
public EditText editText;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editText = (EditText)findViewById(R.id.editText1);
editText.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// As soon as button is clicked, set is as empty
editText.setText("");
}
Try and see if it makes a difference.
you can use this library to clear text by a clear icon
http://droidparts.org/widgets.html#clearableedittext
Use this:
editText.getText().clear();
after onclick of any action do below step
((EditText) findViewById(R.id.yoursXmlId)).setText("");
or else
write this in XML file
EditText
android:hint="Enter Name" />
i did this way.try
You can use:
myEditText.setText(null);
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");
}
}
So, today I decided to try out Android, so please understand that I am a beginner in it.
What I want to achieve right now is to have a EditText, and a set of buttons to be used to enter data into the EditText.
What I've done currently is stick a set of button widgets in the XML layout, and I use this code to make the buttons insert stuff into the EditText:
final EditText inputline = (EditText) findViewById(R.id.textentry);
final Button my_button = (Button) findViewById(R.id.my_btn);
my_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
inputline.append("a");
}
});
This kind of works, but I need help with a few issues:
it always appends the character at the end of the string, not at the current cursor position
similarly, when I call inputline.selectAll() and press my button, it inserts the text at the end of the string again; whereas I want it to delete the text first (as it's selected) and then insert the character
it seems tedious to write all that code for each of the buttons I have. Is there a better way to do this altogether?
Thanks for your help!
I have now pretty much solved by replacing inputline.append("a"); etc. with my custom function, lineInsert(), which you can see below.
public void lineInsert(CharSequence text) {
final EditText inputline = (EditText) findViewById(R.id.textentry);
int start = inputline.getSelectionStart();
int end = inputline.getSelectionEnd();
inputline.getText().replace(Math.min(start,end), Math.max(start,end), text, 0, text.length());
inputline.setSelection(inputline.getSelectionEnd());
}
This has the same behavior as the soft keyboard.