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.
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);
}
}
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);
}
}
});
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 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 want to allow users to create a username using only alphanumeric characters but where the alpha characters can be any of the characters in the user's native language. It must be possible to restrict the input to only those characters that are part of the alphabet of the native language or if there is no alphabet in a language (like Chinese), then limited to those characters that would normally be considered non-symbolic (symbolic characters being a question mark, colon, etc).
Using inputType seems to pose a problem because setting it to "text" actually allows the keyboard to display symbols as well.
EditText editText = (EditText)findViewById(R.id.your_edit_text);
editText.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 arg1, int arg2, int arg3) {
for(int i=0;i<arg0.toString().length();i++){
if(arg0.toString().charAt(i)=='a char you hate')
//Show an error and change the contents of arg0
}
}
});
EDITED
I have edited the answer a little bit because In the previous version assumed that he changed the last character. However this is not true onTextChanged is called even if he inserted a character in the middle of the String.