Normal error displayed using setError() method:
Problem:
Okay so I have another EditText in same dialog having an OnClickListener for showing the DatePicker dialog. When I setError() it shows the red alert icon and when I click on that icon, the event is still handled by OnClick on EditText and DatePicker pops up, hence I cannot view the error message.
What I want : If I click on the icon, it must show the error message, and if I click outside the icon it should show the DatePicker.
Oh man, I literally had this problem 2 days ago. I found no way to make it both HAVE focus (in order to display the message, AND also create a pop-up of the date picker. What I ended up doing is wrapping EditText into a TextInputLayout like this:
<android.support.design.widget.TextInputLayout
android:id="#+id/birthDateInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/input_birth_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/birth_date_hint"
android:inputType="date" />
</android.support.design.widget.TextInputLayout>
And then instead of setting the error on the edit text, I set the error on the TextInputLayout instead like this:
birthDateInputLayout.setErrorEnabled(true);
birthDateInputLayout.setError("This field cannot be empty.");
NOTE: It does not look exactly the same as the regular way of setting error on EditText, but it does look nice enough and solves the problem a bit differently.
Here's a screenshot of how it looks:
A simple solution is to check whether error is null inside onclickListener.
ie,
if(((EditText)view).getError() == null) {
//Handle your click for showing picker
}
you can use this following code according to your code
when complete all field and second date edit text set error to null.any query you can ask
mEditText.setError(null);//removes error
mEditText.clearFocus(); //clear focus from edit text
Try this ,
onClick(View v){
if(editText.getError() != null ){
editText.requestFocus(); // to show error message
}else{
// open date picker dialog here or do you stuff here
}
hope it helps
Related
enter image description here
I made an android app by using Textwatcher and Filter.
Whenever I search some text, this screen made me interrupted.. T_T
Why this box which is included in text is shown up?
Please help me T_T
Actually it gives suggestion when you type.
To remove suggestion box add this property to EditText in xml layout
android:inputType="text|textNoSuggestions"
To remove mic icon from box add below property to EditText in xml layout
android:privateImeOptions="nm" // "nm" : No Mic
I have a form with some textview and text field after clicking on button I will get an error messages, I want to show the error messages on top of the form, would you please let me know how can I implement like a below picture?
instead of setError I want to show the error on top!
Appreciated any hints, documentation or sample code!
thanks in advance!
What you need to do is add the TextView above your TextFields and set its Visibility to GONE. And after you hit the button and recieve the error set the TextView's Visibility to VISIBLE.
add this top of your TextFields:
<TextView
android:id="#+id/your_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"/>
sample:
your_textview.setVisibility(View.VISIBLE); //to set textview visible
Also you need to set the text of your textView if name is not filled just set the text for name error.
error:
set the text of your TextView
1 error:
your_textview.setText("Name: Please fill this field")
2 error
your_textview.setText("Name: Please fill this field\nComments: Please fill this field")
just use the line break delimeter "\n" after each error
You need to check each of the field if it is empty:
String error = "";
if(field1.getText().equals(""))
error += "field 1 fill the form\n";
if(field2.getText().equals(""))
error += "field 2 fill the form\n";
.
.
.
and so on.
your_textview.setText(error);
I have a edit text in my app. When the user touches the edit text the whole text should be selected and when he starts typing text should be cleared. An example is browser address bar. Is there any way to do this?Please help me.
You can select all text in EditText by using
android:selectAllOnFocus and also setSelectAllOnFocus(boolean)
Call EditText.setSelectAllOnFocus(boolean selectAllOnFocus) to select all text on focus.
Set a click listener to your EditText and in onClick call edittext.selectAll();
Add attribute to your main.xml file:
android:selectAllOnFocus="true"
Then all text will be selected and when user type something that will remove it.
You can use property android:hint instead of android:text and you get what you want wihout special code.
You can select all text in EditText by using android:selectAllOnFocus or setSelectAllOnFocus(boolean).
Set a flag when all text is selected. Then detect a text change by using the addTextChangedListener method on your EditText and make your class implement or define an inner class implementing the TextWatcher class.
In this watcher class method, check the flag you set to indicate whether all text is selected. if true, then do TextView.setText(""). This will clear the text. Now set the flag to false again so that subsequent text changes will not cause the text to be cleared.
This is working , all text will be selected and when user type something that will remove it.
editText.setSelectAllOnFocus(true);
editText.requestFocus();
editText.postDelayed(new Runnable() {
#Override
public void run() {
InputMethodManager keyboard = (InputMethodManager) catalougeJobDetailFragment
.getActivity().getSystemService(
Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(commentEt, 0);
}
}, 20);
I am creating a dialog box where there is a edit box I want to set the hint of editbox how can I do it.
please help me
thank you
Every EditText is also TextView and the TextView defines a method setHint():
final void setHint(int resid)
Doesn't this work?
EDIT Btw by edit box you mean EditText right?
I am not sure whether this works but might help you,
You could have initialized your Dialog with something like this,
Dialog dialog = null;
And now to Initialize your EditText you should use something like this,
edittext = (EditText) dialog. findViewById(R.id.editbox);
Now use the below method. It should do the trick,
edittext.setHint("Hint");
The same you can provide in the XML file also by adding :
android:hint="Text For Hint"
tag.
For dynamically adding it (in your code and not in XML), Boris & Androi have aldready mentioned it, so wont repeat it.
If you wont be changing once set, then I guess adding in XML is preferred rather than in code.
working on an application
i need to take an EditText.
but i need as in the phone :-
The EditText already contains blurred text such as "Your Name" when you start typing the text vanishes and you text is shown. I hope you've seen this effect/property..
I'm not getting what this property is called to be searched on google...
Hint
http://developer.android.com/reference/android/widget/TextView.html#attr_android:hint
Use the following in your layout.xml file of edittext:
android:hint="Here your hint text"