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
Related
I have a set of buttons. Here I want, If I click one button then change its background color and then I click another button then change its color, and also change previous clicked button's color.
I used
setBackgroundColor(getResources().getColor(R.color.btnBg));
It changed the background color but It is not what I want. (I try this for Number pad)
Paste this code in onclick
new Handler().postDelayed(new Runnable() {
public void run() {
setBackgroundColor(getResources().getColor(R.color.white));
}
}, 100);
it will change the color to white in 100 miiliseconds
i want to when one cardview click event call it's background color change . but i want to only one cardview color change at a time means i click on 1st cardview , color change yellow , but when i click on 2nd cardview change color yellow ,and other all card set color white.
Here is my code :-
btnOpt1.setOnClickListener {
btnOpt1.setCardBackgroundColor(Color.parseColor("#fcfca9"))
}
btnOpt2.setOnClickListener {
btnOpt2.setCardBackgroundColor(Color.parseColor("#fcfca9"))
}
btnOpt3.setOnClickListener {
btnOpt3.setCardBackgroundColor(Color.parseColor("#fcfca9"))
}
btnOpt4.setOnClickListener {
btnOpt4.setCardBackgroundColor(Color.parseColor("#fcfca9"))
}
btnOpt5.setOnClickListener {
btnOpt5.setCardBackgroundColor(Color.parseColor("##fcfca9"))
}
Create the method and pass your view for selected and unselcted
btnOpt1.setOnClickListener {
clickCardView(btnOpt1)
}
Create the method and call from your all click listeners
private void clickCardView(View btnView){
btnOpt1.setCardBackgroundColor(Color.parseColor("#ffffff"));
btnOpt2.setCardBackgroundColor(Color.parseColor("#ffffff"));
btnOpt3.setCardBackgroundColor(Color.parseColor("#ffffff"));
btnOpt4.setCardBackgroundColor(Color.parseColor("#ffffff"));
//// main logic is here
btnView.setCardBackgroundColor(Color.parseColor("#fcfca9"));
}
When your button is clicked change white color for rest of the buttons like this
btnOpt1.setOnClickListener {
btnOpt1.setCardBackgroundColor(Color.parseColor("#fcfca9"));
btnOpt2.setCardBackgroundColor(Color.parseColor("#ffffff"));
btnOpt3.setCardBackgroundColor(Color.parseColor("#ffffff"));
btnOpt4.setCardBackgroundColor(Color.parseColor("#ffffff"));
}
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);
}
});
Im trying to implement a feature of my small android app.
There is a image button, and a popup window will appear when I click it. According to which button in that popup window user click, that image button should change its image accordingly.
Like when i click 1 in the popup window, I should inform the button to update image to 1.
Can anybody tell me how to do this?
You can make use of the following method
public void setTabFor(Button btn) {
for (Button button : btnArray) {
if (button == btn) {
if (button == button_one) {
Utils.setTabButton(R.drawable.left_selected, button_one);
} else if (button == button_two) {
Utils.setTabButton(R.drawable.middle_selected, button_two);
} else if (button == button_three) {
Utils.setTabButton(R.drawable.middle_selected, button_three);
} else {
Utils.setTabButton(R.drawable.right_selected, button_four);
}
}
Where setTabButton in Utils class is used to set backgrounddrawable:
public static void setTabButton(int drawable, Button... btn) {
for (Button button : btn) {
button.setBackgroundResource(drawable);
}
}
You should assign an ID to each of the buttons in popup window. Then you have to implement a listener that will notify (and pass the ID to) your object (responsible for the image button) that a button was clicked and your object will update the image button according to the received ID.
Try this,
First of all,you have to assign unique Id for all image button that you used in your application.then which button you clicked with help of Ids u can display buttons as per your choice.
I hope it will help you and solve your problem very soon.
Change the color of the two buttons when its correct
I have a game, comparing buttons from the left side and the right side.
when you click on the left side and compare it to right side and its correct the 2 buttons must be color green. How can i implement it?
public void getClick1(int num)
{
firstClick = arrNum1[num];
}
public void getClick2(int num){
secondClick = arrNum2[num];
if (firstClick == secondClick){
guess.setText(Integer.toString(--score ));
}else{
Toast.makeText(this,"WRONG!", Toast.LENGTH_LONG).show();
}
}
You can change the color of the Button with this method:
btn2.setBackgroundColor(Color.RED);
... where btn2 is the instance of Button.