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);
Related
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
My button's layout_width set to match_parent.
In order to display multi lines on the button, I tried:
insert '\n' into the text on button
set Singleline false set Maxlines to 2 or 3
convert html from Html.fromHtml
Nothing worked. '\n' showed up as a small square on the button while showing single line of text.
Does anybody have any idea why this is happening and how I can fix this?
UPDATE: I just found out I was using custom button that has its own text drawing. That's the reason. Sorry for the confusion. I just punished myself by banging my head.
If you're trying to add a new line in a layout XML file:
Use
(new line)
android:text="Hi
Hello"
If you're trying to add a new line in code, just use '\n', same as in any other text.
If you can't see the second line, it may be that your Button doesn't have enough height. IE, in my case, the layout containing the button had a fixed height that just happened to make my button perfectly display one line of text.
I just tried and it worked:
1) Define in ../res/values/strings.xml:
<string name="multilines">Line1Line1\nLine2Line2</string>
2) Refer it in the layout file:
<Button
android:id="#+id/btn_multilines"
android:text="#string/multilines"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
</Button>
In case you want to do that programmaticaly you can use System.getProperty("line.separator") in the string to change lines.
Like this:
String mybuttontext=line1+System.getProperty("line.separator")+line2;
and then set this String as buttons text.
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"
I want to access text of particular line in a multi-line TextView. Any suggestions on how I could do this?
Fetch the Layout (android.text.Layout) of the TextView by calling mTextView.getLayout(); There you can for instance use the getLineStart or getLineEnd methods to get the offset of the text. That combined with getText and used using normal String operations should probably be enough for u!
By default, text view is multi line. Set the text with new line characters for ex. "First line \nSecond line".
Another option is listed here : https://stackoverflow.com/questions/2610280/how-to-get-multi-line-text-on-a-button-in-android
in my custom listview that Contain an image and EditText ,and in EditText i can comment the photo ,i can give text comment max length of 50 ,when i lost the focus in EditText i want to rearrange the text in EditText in following format
EG: suppose comment contain 40 char and user can at a time directly view only 20 char,then if i lost the focus text should be rearranged that at the end there should be 3 dot
Original Comment i wirte:eg-> eeeeeeeeeeeeeeeeeeeeeeeeeee
after i lost focus it should shown as-> eeeeeeeee...
it's importent that the nothing happens to original text because these text i want to send to server, and i directly take these values,and if am replace the text by new this type of text this will create problem, also when i gain focus i need to see full text also.
NB: i set android:singleline=true
Try android:ellipsize="end", or if you want to fade, try android:fadingEdge="horizontal"
Use the android:ellipsize="end" in you XML layout file. You can also show the scrolling text by using android:ellipsize="marquee".