I have a loginscreen.
In this loginscreen I have a button that is by default disabled.
When the user has entered 4 numbers I enable the button and change the textcolor to green.
But when the 4 numbers are not the correct code I clear my edittext and disable my button again.
At the moment the textcolor of this disabled button is offcourse green.
How can I set it back to the default color?
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(s.length() >= maxLength)
{
btnOk.setEnabled(true);
btnOk.setTextColor(Color.parseColor("#00B32D"));
}
else
{
btnOk.setEnabled(false);
}
private void checkIfValid(String inputPin)
{
if(inputPin.equals("0000"))
{
startActivity(new Intent(this, ShowScreenActivity.class));
finish();
}
else
{
clearText();
====> //Here i want to set my textcolor back to normal.
Toast.makeText(this, "Pincode foutief", Toast.LENGTH_SHORT).show();
}
}
Get the default color of Button using this code,
int DefaultButtonColor = btnOk.getTextColors().getDefaultColor();
If its not what you are looking for, then you can get Android Platform Resource Color using
something like,
android.R.color.secondary_text_dark
Check others too...
Back up your default color in onCreate();
defaultTextColor = btnOk.getTextColors().getDefaultColor();
Then set it back
btn.setTextColor(defaultTextColor);
If you have another button that always maintains default color, you can set the color of your color-modified button to this other button to get back to default. The code might be...
btnOk.setTextColor(btnCancel.getTextColors());
This is a simple one line solution, but you have to be careful the other button color is not being modified for other reasons or this may not work.
Related
how to keep button disable any editText Empty ?
i've try with implementation 'androidx.core:core-ktx:1.1.0':
txtEmailOrNoPhone.doOnTextChanged { text, start, count, after ->
if (text.toString().isEmpty()) {
buttonLogin.isEnabled = false
} else {
txtPassword.doOnTextChanged { text, start, count, after ->
buttonLogin.isEnabled = text.toString().isNotEmpty()
}
}
}
but, not work.
because, if i run app (button enable). if i type and delete all (button disable). so, i must type first and delete to (button disable)
i want :
if start app
Since your code inside the doOnTextChanged is not triggered until you type something, your initial state of disabling the button will not work.
For that, just set the button disabled initially in the xml
android:enabled="false"
I need to find the red buttons on Activity and set orange background color to these buttons.
When I click on the button I set it to red:
view.setBackgroundTintList(ColorStateList.valueOf(Color.RED));
When I click on another button, the red buttons should turn orange.
public void Active(View view){
for (View but : buttons) {
but.setClickable(true);
but.setBackgroundTintList();
}
}
I do not know how to get the id of the colors
For me is unclear your question but I'll try to answer it.
Supposing buttons is a List<Button>, so what you can do is.
for(View but : buttons){
int color = ((ColorDrawable)but.getBackground()).getColor();
if(color == Color.Red){
//This button is red change it to orange
but.setBackgroundColor(R.colors.orange);
}
}
And when you are clicking the button use
button.setBackgroundResource(Color.Red);
but.setBackgroundColor(Color.RED);
use that
When the EditText line is blank (the default), I'd like the ImageView icon color to be white.
When the icon is pressed, I'd like the color to be black and then revert back to white because the press clears the EditText line, so it would then again be empty.
With the ImageView, I first set src equal to a white drawable. Any thoughts on how to switch the color state based on the length test of the EditText line?
I'm not too exactly sure what the question is asking for, I got confused when you brought in ImageView and hover because I don't believe phones support the onHoverListener as well.. Do you mean changing the EditText accent color when you click on it to type something? If so it will be under the colors.xml-->colorAccent
Also if you mean to change an images background color
public void onClick(View v) {
imageView.setBackgroundColor(Color.parseColor(EditText.getText().toString()));
}
Hopefully I provided you with some help to continue your project.
To update icon if textview empty, check Android: How can I validate EditText input? with addTextChangedListener() then
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
Log.i("Text", charSequence + "");
if(charSequence.length()==0)
ib.setImageResource(R.drawable.button_unpressed);
else
ib.setImageResource(R.drawable.button_pressed);
}
and to change imagebutton background follow its state How to set image button backgroundimage for different state? with "create an xml in your drawable"
If you want to keep color of icon remain black after pressed, just override onClick() by
ib.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ib.setImageResource(R.drawable.button_pressed);
}
});
Ok so i have radiobuttons that repressent correct and incorrect answer. What would like is to turn color of that button to some color for a moment and then turn it right back to its default color that i have already defined but i do not know how to implement this default color(R.drawable.radiodefault) to appear and change back color of the radiobutton moment after R.drawable.tocan/R.drawable.netocan is shown
if(currentQ.getANSWER().equals(answer.getText()))
{
answer.setBackgroundResource(R.drawable.radiotocan);
}
else if(currentQ.getANSWER()!=answer.getText()){
answer.setBackgroundResource(R.drawable.radionetocan);
}`
The problem might be in your else if condition. You are comparing strings with !=
if(currentQ.getANSWER().equals(answer.getText())){
answer.setBackgroundResource(R.drawable.radiotocan);
} else if(!currentQ.getANSWER().equals(answer.getText())){
answer.setBackgroundResource(R.drawable.radionetocan);
}
If there are no additional conditions then it would be better to use else instead of else if.
As for changing it back to default you could use handler.
new Handler().postDelayed(new Runnable(){
#Override
public void run(){
//change it back
answer.setBackgroundResource(R.drawable.radiodefault);
}
}, delayInMillis);
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.