Change edittext text color android? - android

Can you change the edittext text color AFTER it is entered in android? (Take an input, and on the press of a button, change the color of the contents of the edittext!)
Thanks

public void onButtonClick(View v){
EditText et = (EditText)findViewById(R.id.editText);
et.setTextColor(Color.rgb(red, green, blue));
}

On Button Click You can use one of the following :
editText.setTextColor(Color.RED);
editText.setTextColor(Color.parseColor("#FFFFFF"));
editText.setTextColor(Color.rgb(200,0,0));
editText.setTextColor(Color.argb(0,200,0,0));
editText.setTextColor(getResources().getColor(R.color.editTextColor));
editText.setTextColor(0xAARRGGBB);

If you are looking to change the color of all of the text, you can call setTextColor() on the EditText.
If you are looking to change the color of some of the text, you can apply a ForegroundColorSpan to that text. getText() on an EditText should return a SpannableStringBuilder, on which you can call setSpan() as needed.

Related

How to make clickable and editable Textfield in Android?

I am working on Android application in which I want to make my textfield editable and clickable. It has multiple TextFields and EditTexts on my screen. I have "EDIT" TextField for which I want to make it clickable and after clicking I want to make other field editable and enable. Without clicking edit Textfield all of them should not be enable.
My code snippet is given below:
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_profile);
fName = (TextView) findViewById(R.id.fnametxt);
lName = (TextView) findViewById(R.id.lastnameTxt);
mailText = (TextView) findViewById(R.id.mailTxt);
mobileText = (TextView) findViewById(R.id.mobileTxt);
dobText = (TextView) findViewById(R.id.dobTxt);
fName.setText(currentUserFirstName);
lName.setText(currentUserLastName);
dobText.setText("");
mobileText.setText(currentUserContactNumber);
mailText.setText(currentUserEmail);
}
You can't convert TextView to EditText, but you can rather use setEnabled property for EditText
You can use editText.setEnabled(true); to make the EditText editable.
Say you are having two edittexts as follows. And on entering data in first edittext, you need to make edittext2 editable, then you can do this:
EditText edittext1, edittext2;
//findViewByIds for both views
editText1.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if(s.length() != 0)
editText2.setEnabled(true);
else
editText2.setEnabled(false);
}
});
Hope this gives a clue how to use it.
EDIT:
Don't get confused between TextField, Button, EditText.
In Android, simple read only field is TextView. Editable textbox is called EditText, and Button is plain Button.
So as per what you are saying, you want tomake EditTexts editable upon clicking of a Button.
Use this:
EditText edittext1, edittext2;
Button button;
//findViewByIds for all views.
buton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
editText1.setEnabled(true);
editText2.setEnabled(true);
}
});
In my opinion, if you want to edit your textfield, then its better to go with EditText. The reason is as follows
The TextView's editable param does make it editable (with some restrictions).
If you set
android:editable="true"
you can access the TextView via the D-pad, or you could add
android:focusableInTouchMode="true"
to be able to gain focus on touch.
The problem is you cannot modify the existing text, and you cannot move the cursor. The text you write just gets added before the existing text.

Change Text in EditText when pressing the EditText

Some EditText (Like when you write a text message) can be clicked and opened, so you can change the text inside the EditText. It also gives you the keyboard so you can write, etc.
How do I achieve the same thing?
Currently I got this:
public void onDescriptionBoxClick(View view) {
EditText et = (EditText) this.findViewById(R.id.editText1);
}
And I don't know what to do other than that.
Do I have to call the keyboard forth so the user can edit the text in the box?
I would place an EditText element right next to your textView which has the attribute android:visibilty="gone"
then in your code:
public void onDescriptionBoxClick(View view) {
TextView tv = (TextView) this.findViewById(R.id.textView2);
tv.setVisibility(View.GONE);
EditText et = (EditText) this.findViewById(R.id.editText2);
et.setVisibilty(View.VISIBLE);
}
You can't edit text using a TextView. to accomplish what you want you have to replace the TextView with a EditText widget. and then replace it with the TextView containing the text entered in the EditText.
You can accomplish this in a few ways:
Using Fragment, more compicated.
You can use simple TextView and EditText widgets and set their Visability accordingly.

How to control background of a Edit text when in edit mode. For Android?

I have a edit text field. I want it to have a transparent background. But when the user tries to edit it, it should be with a different background say white color and text color as black. And when he is done with the edit of the edit text and moves to the next control to enter other values i want the background to be transparent again. Please let me know how this is possible in android. Thank you for the help and time.
EditText editText = (EditText)findViewById(R.id.edit1);
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
EditText editText = (EditText)findViewById(R.id.edit1);
editText.setBackgroundColor(hasFocus ? Color.WHITE : Color.TRANSPARENT);
editText.setTextColor(hasFocus ? Color.BLACK : Color.GRAY);
}
});
implement method onFocusChabgeListener() like below.
textView.setOnFocusChangeListener(new OnFocusChangeListener(){
if(textView.hasFocuss){
//set textColor and background color
}
else
{
//reset it
}
});

TextView: how to dynamically change the color of certain word in paragraph

How to change the color of a paragraph word by word with timer. like at first second i want to change color of The in the below paragraph, after 5th second change color of text, after 8th second change color of will be and so on....
The text will be wrapped in tags, and displayed in a monospaced font.
just use Timer and change the font color of your edit text accordingly and stop timer in focus lost.
i think you can do something like this :
Split the paragraph to words by using the method :
split(String separator);// it will return an array of Strings
//in your case you will do somthing like this
myWords = paragraph.split(" ");// the separator is the space
And then , you can use the method to colorate what ever you want of those words by using the method :
myNewParagraph.append(HTML.fromHtml("<font color...>... Your HTML Code to colorate your Text"));
and when you finish coloring each word , you update your textView to display the new text colored
Hope it helps
You can use spans to control the appearance of text. Take a look at Spannable and CharacterStyle.
Here is an example. Of course you would need to put this in some sort of timer.
Spannable spannableText = getSpannableText(yourTextView);
spannableText.setSpan(new TextAppearanceSpan(...), wordStart, wordEnd)
yourTextView.setText(spannableText);
private Spannable getSpannableText(TextView tv) {
CharSequence cs = tv.getText();
if (cs instanceof Spannable) {
return (Spannable)cs;
} else {
return SpannableString.valueOf(cs);
}
}

setHintTextColor() in EditText

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.

Categories

Resources