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);
}
}
});
Related
If the user enters any value like 1234 in the edit text box, then all the values of the edit text box are Addition in each other and the answer is shown in a text box.
For example if user enter values such as 1234 in edit text and after addition (1+2+3+4=10) their answer that is 10 show in text box.
You could probably use data binding, either one or two way depending on how you want to do things.
Make sure to read the documentation around data-binding before starting to successfully implement!
Using one-way data binding, you can set a value on an attribute and set a listener that reacts to a change in that attribute - android docs
First set your binding adapters on your new SumNumberTextView,
Then in your parent view's layout file, if you want to use the more verbose one way data binding, you'd reference something like:
android:numbers="#{viewmodel.numbers}"
and
android:onNumbersChanged="#{() => viewmodel.onNumbersChanged()}"
Then you can define a function on your view model that does your numeric operations whenever you input numbers.
You can just use textwatcher on edittext to get update when text change and do your operation of addition just like this
editText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// call function from here if you want to perform operation as user provide input
add(cs.toString());
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
Toast.makeText(getApplicationContext(),"before text change",Toast.LENGTH_LONG).show();
}
#Override
public void afterTextChanged(Editable arg0) {
// call function from here if you want to perform operation only user stop input
add(arg0.getText().toString());
}
});
private void add(String input){
if(input != null && input != ""){
String[] numbers = input.split("")
int total = 0;
for(int counter = 0 ; counter < numbers.length ; counter++){
total += Integer.parseInt(numbers[counter]);
}
Log.e("total",total);
}
}
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);
}
});
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.
I have an edittext in my app, which accepts only numbers. It is set to 0 by default. But when i enters some umbers, i want to overwrite the initial 0 in the left. Now i am having some problem with edittext. For example, when i inserts a 1, i want it to how 1, not 01. my code is like,
value = (EditText)findViewById(R.id.value);
value.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
{
if(arg0.length()==0)
{
value.setText("0");
int i = value.getText().length();
value.setSelection(i);
}
}
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
How can i achieve it? or is it possible?
You can set android:hint in your xml file.
For example,
<EditText
android:id="#+id/value"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="0"/>
There are lots of options to accomplish what you're looking for. Besides the ones already mentioned, I'd say the EditText's attribute android:selectAllOnFocus="true" might be worth looking at. It's convenient for a default value, and at the same quite user friendly as changing the value does not require any 'backspacing'.
You can change string to empty in your beforeTextChanged Function Call and let the onTextChanged as it is.
Instead of using value.setText("0");
use value.setHint("0");
Use setHint(int) method to display 0 initially. So that when the user types something it is automatically cleared and you can get the text as desired.
I have View in which there are two text boxes, and the user can select text color from another view on the same screen (through dialog box).
So when the user changes color via dialog box, I am changing color of EditText text and its hint. But when there is some text is available in EditText after that user selects other color, then that text is coming in that color. But if I remove all that text then the color of HintText is that of the previous color.
For example, currently if I have red color in text box and the user selects green color so text is there in green color. But if I remove that text then hint text are coming in red even if I change hint color in code. This problem only comes when there is some text there. if it is blank and hint text is there then problem is not coming.
Simply add this in your layout for the EditText :
android:textColorHint="#FFFFFF"
Use this to change the hint color. -
editText.setHintTextColor(getResources().getColor(R.color.white));
Solution for your problem -
editText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,int arg3){
//do something
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
//do something
}
#Override
public void afterTextChanged(Editable arg0) {
if(arg0.toString().length() <= 0) //check if length is equal to zero
tv.setHintTextColor(getResources().getColor(R.color.white));
}
});
Default Colors:
android:textColorHint="#android:color/holo_blue_dark"
For Color code:
android:textColorHint="#33b5e5"
Inside Layout Xml File We can Change Color of Hint.....
android:textColorHint="#android:color/*****"
you can replace * with color or color code.
Seems that EditText apply the hintTextColor only if the text is empty. So simple solution will be like this
Editable text = mEditText.getText();
mEditText.setText(null);
mEditText.setHintTextColor(color);
mEditText.setText(text);
If you have multiple fields, you can extend the EditText and write a method which executes this logic and use that method instead.
Programmatically in Java - At least API v14+
exampleEditText.setHintTextColor(getResources().getColor(R.color.your_color));
This is like default hint color, worked for me:
editText.setHintTextColor(Color.GRAY);
You could call editText.invalidate() after you reset the hint color. That could resolve your issue. Actually the SDK update the color in the same way.