Reputation: 71
My question is - How to change color of cursor in EditText more then one time.
I have editText and two btn for change color of cursor
public class MainActivity extends AppCompatActivity {
public EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.edit_text);
editText.setText(Html.fromHtml("5+6+3<sup>2</sup>+6"));
}
public void btn1(View v){
try {
Field f = TextView.class.getDeclaredField("mCursorDrawableRes");
f = TextView.class.getDeclaredField("mCursorDrawableRes");
f.setAccessible(true);
f.set(editText, R.drawable.cursor);
} catch (Exception e) {
Log.e("ALERT", "exception: " + e.getMessage());
Log.e("ALERT", "exception: " + e.toString());
e.printStackTrace();
}
editText.invalidate();
}
public void btn2(View v){
try {
Field f = TextView.class.getDeclaredField("mCursorDrawableRes");
f = TextView.class.getDeclaredField("mCursorDrawableRes");
f.setAccessible(true);
f.set(editText, R.drawable.cursor_exponent);
} catch (Exception e) {
Log.e("ALERT", "exception: " + e.getMessage());
Log.e("ALERT", "exception: " + e.toString());
e.printStackTrace();
}
editText.invalidate();
}
}
When I start app and press btn1, color change happens.
But after, when I press btn2 color does not change.
If I close app, and start again, and then I Press btn2 and color changes.
But after, if I press btn1, color does not change.
How to fix it?
Thanks.
UPDATED
I updated the code in a question
1.1. Used different Field f
1.2. I need use the same EditText becouse this btn must change cursor in a same edittext
For what i need use View v?
Yes, its my mistake
I added showing Log in catch but nothing inside had
And i used a public field
It not fixed my issue, have any different idea?
UPDATED 2
Maulik Panchal thanks for the help. It work. I'm sorry I didn't notice that in your last code you use "if" for deny second change color.
But now i see your code and i have a few question.
You use "editText.getBackground().mutate().setColorFilter(...);
" for change background color.
I want change cursor color, and i looked and not find option in editText for change cursor color.
And if i use my old part code for change cursor color its work only in first time
try {
Field f = TextView.class.getDeclaredField("mCursorDrawableRes");
f = TextView.class.getDeclaredField("mCursorDrawableRes");
f.setAccessible(true);
f.set(editText, R.drawable.cursor_exponent);
} catch (Exception e) {
Log.e("ALERT", "exception: " + e.getMessage());
Log.e("ALERT", "exception: " + e.toString());
e.printStackTrace();
}
How to exactly change color of cursor?
Thanks
Upvotes: 0
Views: 384
Reputation: 116
i tried to solve your problem as per below:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
EditText editText;
Button btnRed, btnGreen;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.et);
btnGreen = findViewById(R.id.green);
btnRed = findViewById(R.id.red);
btnRed.setOnClickListener(this);
btnGreen.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.red:
editText.getBackground().mutate().setColorFilter(getResources().getColor(android.R.color.holo_blue_light), PorterDuff.Mode.SRC_ATOP);
break;
case R.id.green:
editText.getBackground().mutate().setColorFilter(getResources().getColor(android.R.color.holo_green_dark), PorterDuff.Mode.SRC_ATOP);
break;
}
}
}
Solution for Update2:-
You can change your cursor and underline below your text by below code..
First of all make one style in your style.xml section:
<style name="AppTheme2" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorAccent">your color</item>
</style>
after this just Add this line in your EditText:
android:theme="@style/AppTheme2"
This is your solutions..Thank you:)
Upvotes: 0
Reputation: 4487
(I updated your question to be more clear).
You did some mistakes:
Try to adapt that, and let us know if it fixes your issue.
Upvotes: 0