Reputation: 770
How do I change color of the TEXT EDIT control when I am entering text and change the color again after I am done typing.
I need black color text on typing after typing if i go to next edit text the previous one should be in white color.and one more thing i placed all edittexts in list view.How do i get? Thanks in Advance.
Upvotes: 2
Views: 194
Reputation: 1891
simple and effective solution
Create inside res/color
folder textcolorselector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#FFF"/>
<item android:state_focused="true" android:color="#000"/>
<item android:color="#000"/>
</selector>
now in editext.
<EditText
android:textColor="@color/textcolorselector">
<requestFocus />
</EditText>
Upvotes: 1
Reputation: 2141
Simply you can create a focus changed listener as a global variable.
View.OnFocusChangeListener listener = new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
((EditText) v).setTextColor(hasFocus ? Color.BLACK : Color.WHITE);
}
};
Now set that listener to the edittext you create on your listview.
editText.setOnFocusChangeListener(listener);
So when you are in the edittext, its color will be black and when you leave the edittext the color will be changed to white and the next edittext focused will become black.
Upvotes: 4
Reputation: 36
You can use TextWatcher
Example
yourEditText.addTextChangedListener(new TextWatcher()
{
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
//do stuff
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
//do stuff
}
@Override
public void afterTextChanged(Editable s)
{
//do stuff
}
});
Upvotes: 0
Reputation: 2618
Try This
etEmail.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
etEmail.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
etEmail.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.defaultColor));
}
});
Upvotes: 0