I have two edittext input box. First one is for username and the second one is for password. I want put hint on the inputbox. I have wrote hint with settex() method. In addition, I have wrote
in onclick(view v)
caseR.id.editText_1:
text1.setText("");
break;
in oncreate
text1 = findViewById(R.id.editText_1);
text1.setText("username");
whenever I have click on the input box, hint should dissappear. But, hint is not dissappeared. Now, is the way I am doing wrong? Or I have missed something?
Use
text1.setHint("MyHintString");
instead. setHint() provides the hint that disappears when the user types something. You can also use a String resource ID or an xml based approach, but in your case, using the String method will suffice.
Only use setText() when you want to put text in the EditText that the user can modify.
the correct way to do this is to use setHint(int resource) with a string resource containing your hint string.
so your onCreate should have : text1.setHint(R.String.yourhint);
and your res->values->strings.xml file should contain
<string name="yourhint">This Hint Will Disappear</string>
Use setHint(String hint) to specify the hint in an EditText.
text1.setHint("This is the hint...");
Or, in your XML layout file, you can use the attribute android:hint="This is the Hint" for the corresponding EditText.
You can use editTextExample.setHint("example") to achieve what you want.
OR, in XML, add this to your EditText: android:hint="example"
If you want to set it in xml file of the activity you can do it like this too.
in your xml file android:hint="#string/yourhint"
and your res->values->strings.xml file should contain
<string name="yourhint">This Hint Will Disappear</string>
Related
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.
How do I put text into empty TextView so it goes away when user starts typing? Just like here in StackOverflow under "Tags" field.
Same approach used in GMail client. That will save me some space - no need to create labels..
Throw an android:hint="hint text" into your EditText xml tag.
As haphazard said or programatically you can use...
setHint(CharSequence hint) or setHint(int resId).
The equivalent to the resource id in xml would be android:hint="#string/hint_text" (for example).
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
hi
i want to restrict the special characters (!,#,#,$ etc.) from entering in to Edit Text field in android. how to do this please any body help..
thnx.
You need to use input filter and set it on Edit Text with setFilters method
You need to look at the reference. EditTest inherits from TextView:
http://developer.android.com/reference/android/widget/TextView.html
There you can add an TextChangedListener:
addTextChangedListener(TextWatcher watcher)
Then you test for your special characters and remove them if they are found
I have an edit text and I'd like to put the following constrains (in the XML code if possible):
Disable all the capital Letters (the inverse of android:capitalize or the same fonction than toLowerCase())
Block to EditText to 1 line max. (for instance avoid that when I press enter the editText
get bigger to create a new line)
In fact , my editText is a Search Field (but in my case I don't want to use the special Search Widget).
Thanks
To make the edittext single line add this to its tag:
android:singleLine="true"
Also android:capitalize is depreciated and the following should be used instead
android:inputType="textCapCharacters"
However there seems to be no matching lowercase inputType, so you may have to implement it manually using a setOnKeyListener.
use android:lines="1" in the android xml page and then see the layout.