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);
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 listen for clicks on the positive button of an AlertDialog, which I have disabled by calling button.setEnabled(false);.
How should I do this? If this is not possible, is there a known workaround?
PS. The reason I want to do this, is that I want to show a toast when somebody hits the button, saying "You need to do this before you can continue".
This is not a way to listen for clicks on a disabled button. This is a workaround.
I liked the result I got by changing the color of the button, making it look like it's disabled.
What you want to do:
// Instantiate positive button
final Button posButton = ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_POSITIVE);
// Save the original button's background
final Drawable bg = posButton.getBackground();
// Set button's looks based on boolean
if (buttonDisabled) {
posButton.setTextColor(getResources().getColor(R.color.disabledButtonColor, null));
// R.color.disabledButtonColor == #DBDBDB, which is pretty close to
// the color a disabled button gets.
posButton.setBackgroundColor(Color.TRANSPARENT);
// Color.TRANSPARENT makes sure all effects the button usually shows disappear.
} else {
posButton.setTextColor(getResources().getColor(R.color.colorPrimaryDark, null));
// R.color.colorPrimaryDark is the color that gets used all around my app.
// It was the closest to the original for me.
posButton.setBackground(bg);
// bg is the background we got from the original button before.
// Setting it here also re-instates the effects the button should have.
}
Now, don't forget to catch your buttons actions whenever it's "disabled"
public void onClick(View v) {
if (buttonDisabled) {
// Button is clicked while it's disabled
} else {
// Button is clicked while it's enabled, like normal
}
}
That should do, have fun with it.
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.
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.
I'm having a problem when setting the visibility of two image buttons one on top of the other. The idea is to implement a play/pause control. The problem is that the only part where setting the visibility actually works is in the click listeners of the buttons. If I try to change it somewhere else nothing happens. Any idea why is this happening?
playBtn.setOnClickListener(new OnClickListener() {//PLAY BUTTON LISTENER
public void onClick(View v) {
playBtn.setVisibility(ImageButton.GONE);
pauseBtn.setVisibility(ImageButton.VISIBLE);
mp.start();
}});
pauseBtn.setOnClickListener(new OnClickListener() {//PAUSE BUTTON LISTENER
public void onClick(View v) {
pauseBtn.setVisibility(ImageButton.GONE);
playBtn.setVisibility(ImageButton.VISIBLE);
mp.pause();
}});
final class SeekBarTask extends TimerTask {
public SeekBarTask(int duration) {
}
#Override
public void run() {
if(seekBar.getProgress() >= mp.getDuration()) {//IF SONG HAS FINISHED...
pauseBtn.setVisibility(ImageButton.GONE);//THESE ONES
playBtn.setVisibility(ImageButton.VISIBLE);//DOESN'T WORK
mp.stop();
}
else {
seekBar.incrementProgressBy(100);
}
}
}
I would recommend just changing the icon of one ImageButton.
I would think only one of two things could be happening. Either this code never gets hit, or the variables are not referring to the same object instances you're expecting them to. Have you put a breakpoint inside that condition? I would check that a break point even gets hit in there, and then check that the variables are pointing at the correct button instances.
Without seeing the rest of the code I have to ask...why are you checking on a progress bar for a "finished playing" condition versus using the media players on completion callback?
I'm doing something very similar, and I use the MediaPlayer's OnCompletionListener to flip the visibility of my buttons.
I don't remember the details of Android GUI manipulation but could it have to do that you're doing it from another thread and you're not supposed to?
i noticed that setting an ImageButton to View.INVISIBLE is not working when you have set an Animation to it. you have to remove the Animation then make it Invisible. bad pitfall i think...