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"));
}
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 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
How can we change the background and text color in AlertController?
Is there any way without using xml, so that we can add or modify here itself so that the color can be changed?
I was able to change color of bluetooth_discoverable but not able to change the background color in the top and down area where icon is present.
void createDialog(){
final AlertController.AlertParams p=mAlertParams;
p.mIconId=android.R.drawable.ic_dialog_info;
p.mTitle=getString(R.string.bluetooth_permission_request);
View view=getLayoutInflater().inflate(R.layout.bluetooth_discoverable,null);
p.mView=view;
TextView tv=(TextView)view.findViewById(R.id.message);
if (mEnableOnly) {
tv.setText(getString(R.string.bluetooth_ask_enablement));
}
else {
v.setText(getString(R.string.bluetooth_ask_enablement_and_discovery,mTimeout));
}
p.mPositiveButtonText=getString(R.string.yes);
p.mPositiveButtonListener=this;
p.mNegativeButtonText=getString(R.string.no);
p.mNegativeButtonListener=this;
setupAlert();
}
I have certain entries in my list view item. There I have a simple "like button" (not facebook like button). You can see the above mentioned SCREENSHOT; for the reference.
The moment I click on like button; i want the like button color to be changed and the like button color should remain same(changed on like) when I'll login again.
Also, all the entries must get filled in Database with cust_id, bus_id, Offer_id using json; that I know very well.
When I again click on the same button(like button), whose color has been changed. It must be changed back to the default color and data must get removed from database.
How can I do this...?
1. How to get value of click button.
2. How to bring back the changed color to default; once the button has been re-clicked.
Plz suggest me...
this is button code
holder.b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (clicked) {
holder.b1.setBackgroundResource(R.drawable.like_icon_hover);
} else {
holder.b1.setBackgroundResource(R.drawable.like_icon);
}
clicked = true;
}
});
You need to add a listener to the button and using ValueAnimator you can change the button color and reverse it back when you click again.
Here is a simple and best approach to achieve your scenario. Add the onClick listener for the button in your list item like this.. I have explained each line ..
// set a default background color to the button
placeHolder.likeButton.setBackgroundColor(Color.RED);
placeHolder.likeButton.setOnClickListener(new View.OnClickListener() {
ValueAnimator buttonColorAnim = null; // to hold the button animator
#Override
public void onClick(View v) {
// first time this will be null
if(buttonColorAnim != null){
// reverse the color
buttonColorAnim.reverse();
// reset for next time click
buttonColorAnim = null;
// add your code here to remove from database
}
else {
final Button button = (Button) v;
// create a color value animator
buttonColorAnim = ValueAnimator.ofObject(new ArgbEvaluator(), Color.RED, Color.BLUE);
// add a update listener for the animator.
buttonColorAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animator) {
// set the background color
button.setBackgroundColor((Integer) animator.getAnimatedValue());
}
});
// you can also set a delay before start
//buttonColorAnim.setStartDelay(2000); // 2 seconds
// start the animator..
buttonColorAnim.start();
// add your code here to add to database
}
}
});
This will change the button color on your first click and then revert the color back on the next click. You can also set a delay to change the color.
Note: You have to set the default button color based on your logic.
#Override
public void onClick(View view) {
if(!check)
{
personViewHolder.img_like_job.setImageResource(R.drawable.ic_thumbsup_blue);
check = true;
}
else
{
personViewHolder.img_like_job.setImageResource(R.drawable.ic_thumbsup);
check = false;
}
}
you can use custom adapter for your listview(it has own layout.xml),and you can set your clicklistener in it.
You can change color or what you want. Actually I did have project like you want.I put some link if you can t do it.
Try following:
use setOnClickListener() on the button.
eg.
viewHolder.imgVwFbLike.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
// TODO :
// 1. make webservice call to update like status (Assuming a web service call)
// 2. Implement a callback for webservice call, to get the status of request.
if(success)
a) change the colour of like btn. and insert the data in Db.
b) Also maintain a column in db for likestatus(by default set it false).
}
}
);
Assuming you are fetching the data from db when you login, you can check the likestatus and set the color of button accordingly.
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.