I want the hint size of an EditText to be smaller than the real text.
At present I know two solutions:
using Html.fromHtml() programmatically, or
using <font size=""> in the XML.
However, I don't want to use the first solution because I want the hint to be directly written in my layout XML, while, regarding the second solution, I don't like using the font element because (if I'm not wrong) I cannot explicit the dp measure. Furthermore, the font element is deprecated in HTML5, so it belongs to the "old-style".
I've tried to use the span element with the font-size attribute (from CSS) in my XML, but it doesn't work. Is there any other up-to-date solution?
I am not aware of any way in which you can accomplish this through xml only (apart from sticking html tags in the hint text).
But there is another way that was not outlined above, that would allow you to keep the font size in sp and separate from the java code:
final int hintSize = <read_this_from_xml_resources_but_take_into_account_density>;
final int textSize = <read_this_from_xml_resources_but_take_into_account_density>;
editText.addTextChangedListener(new TextWatcher(){
#Override
public void afterTextChanged(Editable arg0) {
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
#Override
public void onTextChanged(CharSequence arg0, int start, int before, int count) {
editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, arg0.length() > 0 ? textSize : hintSize);
}
});
Related
I tried many combinations of inputType, singleLine and maxLines, but I just can't get the behavior that I want.
Basically, I want the EditText to be a single string with no new lines allowed, but also to expand vertically if the string is longer than it's initial size. (all text needs to be displayed on screen)
textMultiLine expands when the string is long, but it does not prevent the usage of the new line character. singleLine has no effect in this case and maxLines simply changes the size of the box while you can still create new lines.
Anything other than textMultiLine does not expand vertically and the text will simply scroll horizontally if it's too long to fit.
As I already mentioned in my comment, you listen for changes using TextWatcher. Unfortunately, there's (as of writing this answer) no other ways of doing this. As per your second comment though, here's an optimization suggestion.
Listeners can be classes, and they can be standalone classes. It doesn't have to be the current class or an anonymous inner class. For an instance, you can create a class like this:
public class SingleLineET implements TextWatcher {
EditText et;
public SingleLineET(EditText et){
this.et = et;
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String converted = s.toString();
if(converted.contains("\n")){
//There are occurences of the newline character
converted = converted.replace("\n", "");
et.setText(converted);
}
}
#Override
public void afterTextChanged(Editable s) {
}
}
And whereever you want to have a listener, you simply do:
EditText theET = ...;
theET.addTextChangedListener(new SingleLineET(theET));
And like that you cut down the amount of places you need the same boilerplate code.
There may be some alternative in a later release of Android (or someone creates a view that automates this) but until then, using a class instead of manually creating code for it every time at least cuts down some lines
I'm currently working on a calculator android app, and I want to keep all user expressions on a single line. To do this, I need to be able to decrease the textsize of the texview as more and more input is added (so the text would never have to overflow to the second line). Any ideas on how to achieve this? If I'm not explaining the problem well enough, Google's Calculator does this. If this is an extremely difficult task, I can always resort to a horizontal scroll view.
There are ways you can do this on your own, but seems like an ideal use case for: android-autofittextview
Sample usage:
<me.grantland.widget.AutofitTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:maxLines="2"
android:textSize="40sp"
autofit:minTextSize="16sp"
/>
Validate the length and reduce/increase the Size of that text
Example
//You can also use Textview/EditText
TextView.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {}
#Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if(s.length() <5)
TextView.setTextSize(TypedValue.COMPLEX_UNIT_SP,context.getResources().getDimension(R.dimen.text_medium));
else
EditTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP,context.getResources().getDimension(R.dimen.large));
//Here I included the Textsize from Dimens file
}
});
dimens.xml
<resources>
<dimen name="text_medium">12sp</dimen>
<dimen name="large">25sp</dimen>
</resources>
Suppose I want to change text size. I'm doing it in code and it looks like this:
_textInputLayout.EditText.SetTextSize(Android.Util.ComplexUnitType.Dip, 40);
When I write text in the entry it looks like 40dip text. But when entry is empty hint text looks like 16-18dip.
Is there any way to change hint text size?
Changing the final hint size / floating label size is possible via a style and calling SetHintTextAppearance using something like the following:-
_nativeView.SetHintTextAppearance(App6.Droid.Resource.Style.MyTextInputLayout);
Where MyTextInputLayout is something along the lines of:-
<style name="MyTextInputLayout" parent="#android:style/TextAppearance">
<item name="android:textColor">#color/blue</item>
<item name="android:textSize">44sp</item>
</style>
However the textSize from this style is only applied to the final destination, when you start to enter some text in.
From what I can see, and including the properties on the object, it doesn't appear to be possible to change the starting font size of the hint unfortunately at the moment?
Where as EditText is exposed, and you can alter things there. The Hint portion is not handled at all by it, and instead by the TextInputLayout. There appears no object exposed to get access to customize this specifically for the Hint.
You can do it by setting a size in the string recource.
For example:
<string name="edittext_hint"><font size="15">Hint here!</font></string>
then in your XML just write
android:hint="#string/edittext_hint"
This will resault in a smaller text for the hint but the original size for the input text.
Or like this:
MYEditText.addTextChangedListener(new TextWatcher(){
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence arg0, int start, int before,
int count) {
if (arg0.length() == 0) {
// No entered text so will show hint
editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, mHintTextSize);
} else {
editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, mRealTextSize);
}
}
});
Does anyone have ideas on how to achieve a blank indent on the first line of an EditText such that the user cannot modify the indent?
My goal is to superimpose some other info (possibly graphics) in the indent area and still allow the rest of the EditText to wrap long lines back to the normal left margin.
Fallback would be to add a separate line or column for the "other info", but that isn't as good a use of the screen real estate.
Perhaps there is better way to do this. Suggestions are welcome!
I'm not sure on what are you trying to do but you can "modify" dinamically the text while is prompted.
Use a TextWatcher that offers you three method called in order. Try in debug with some breakpoints to understand better the variables and use them!
((EditText) findViewById(R.id.myEditText)).addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
#Override
public void afterTextChanged(Editable s) {}
});
I am working on an Android application.
Now I want take input from user into an EditText.
I want display the text after the last fullstop in black color and text before last full stop in red color.
For example, If user types below sentence in EditText:
'my name is john.I am from India.I Love Android'
I want to show the 'I love Android ' in black and first parts of the sentence in red.
Is there any way to do that?
Use SpannableString to apply attributes to your text. Here blog entry you may want to read: http://www.chrisumbel.com/article/android_textview_rich_text_spannablestring.
Listen to changes in text by:
editText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
if (!colorHasSet) {
makeColorText();
}
colorHasSet = false;
}
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}
});
then declare a function to colorize text by using the tutorial which WebnetMobile linked to.
public void makeColorText() {
SpannableString ss = new SpannableString(textEdit.getText());
// customize ss here
// ...
colorHasSet = true;
editText.setText(ss);
}
flag boolean variable colorHasSet should be defined to prevent stackOverflowException.
this is not a complete WYSIWYG editor with instant colored text, and you should do some hacks to make it complete and suitable to your needs, that is left to be done by yourself.