Changing the button background alternatively - android

I have an android project where i need to use a switch.
I am using a Button for this.
I have two images one for on state and another for off state. Initially i am giving "off_image" as background of Button. When the user clicks the button the background should change to "on_image" and when again the user clicks it should change back to "off_image".
I am using the below code but it is not working....
Inside onClick method --->
if(button.getBackground().equals(R.drawable.off_image)
button.setImageResource(R.drawable.on_image);
if(button.getBackground().equals(R.drawable.on_image)
button.setImageResource(R.drawable.off_image);
Please treat me as a novice and give detailed solution.
Thank you.

OK, save a global var lastImageResource.
int lastImageResource= R.id.off_image;
now, each time you switch background update it like this:
if(lastImageResource == (R.id.off_image)){
button.setImageResource(R.id.on_image);
lastImageResource = R.id.on_image;
}
if(lastImageResource == (R.id.on_image)){
button.setImageResource(R.id.off_image);
lastImageResource = R.id.off_image;
}
Now it should work.

Use getConstantState()
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
drawableDownloaded=holder.iv_catch_status.getDrawable().getConstantState() == context.getDrawable(R.drawable.ic_cloud_download).getConstantState();
}else{
drawableDownloaded=holder.iv_catch_status.getDrawable().getConstantState() == context.getResources().getDrawable(R.drawable.ic_cloud_download).getConstantState();
}
and to set image
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
this.statusImage.setImageDrawable(context.getDrawable(R.drawable.ic_play));
}else{
this.statusImage.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_play));
}

Compare the constant states and use setBackgroundResource() as follows:
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (button.getBackground().getConstantState().equals(getDrawable(R.drawable.ic_menu_camera).getConstantState())){
button.setBackgroundResource(R.drawable.ic_menu_gallery);
}
else if(button.getBackground().getConstantState().equals(getDrawable(R.drawable.ic_menu_gallery).getConstantState())){
button.setBackgroundResource(R.drawable.ic_menu_camera);
}
}
});
If minSdkVersion is less than 21 use getResources().getDrawable() instead of the getDrawable() method.

How come you are using R.id.image.
These are drawables, use R.drawable.image.
Also,
You are setting, ImageResource, but check background.
Both are different things.
Why don't you just use boolean?
boolean isOn = false
And, then toggle it.

Related

How to set the background of button programmatically when that is at some of conditions in android?

I am going to change the background of button in android when text value is lower than 1.
btn_minus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int temp = convertStringToInt(text_count.getText().toString());
if (temp != 1){
text_count.setText(temp-1);
} else {
btn_minus.setBackground(R.drawable.ic_circle_gray_minus);
}
}
});
btn_minus is the object of Button. and btn_minus.setBackground is not working now.
Using setBackgroundResource() function instead of the setBackground().
Use below code for changing button background color :
btn_minus.setBackgroundResource(R.drawable.ic_circle_gray_minus);

Android Delete setBackgroundResource image

How to delete the image of BackgroundResource from button?
For example: b.setBackgroundResource(R.drawable.breakdown)
I'm sure that setBackgroundResource(0) is not available!
Thank you
Put this code...
b.setBackgroundResource(null);
try this using this you will get default button style.
btn.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.btn_default));
or this
b.setBackgroundResource(null);
or this
b.setBackgroundResource(0);
Try:
b.setBackground(null);
The documentation states:
Set the background to a given Drawable, or remove the background.
Also:
b.setBackgroundResource(0);
The documentation states:
Set the background to a given resource. The resource should refer to a
Drawable object or 0 to remove the background.
Edit:
Because you stated that it does not work, I wrote this to test it, and it works perfectly:
final Button btnTest = (Button) findViewById(R.id.btn_test);
btnTest.setBackgroundResource(R.drawable.some_drawable);
Button btnClean = (Button) findViewById(R.id.btn_clean);
btnClean.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
btnTest.setBackground(null);
}
});

how to implement simple like button in listview of each item

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 Text in a same Layout

I'm Making simple app for project
That App contains lot of text so i want,
"when a button is pressed, text should Change in same layout"
like PowerPoint slide.
I want change text only not scroll.
Now i made my app, have lots of Windows or Layouts.
It is not looking good, too much layout in simple app so please help me .
Thanks in advance
Doing this is very easy, I will quickly walk you through the Algorithm:
Set a class level variable called as FLAG initialize it to 1.
Let us assume that FLAG = 1 will represent the first slide. FLAG = 2 the second slide and so on.
Now in your button click you can use a switch case or an if else condition, based on the value of the flag display the relevant text in textview.
Once done, increment the flag, for the next set of sentence(s).
Class level:
int FLAG = 1;
onCreate:
Initialize your textView:
TextView mtv = (TextView)findViewById(R.id.yourid);
Set a button click listener:
private View.OnClickListener slides = new View.OnClickListener() {
#Override
public void onClick(View v) {
if(FLAG ==1)
mtv.setText("First slide");
else if(FLAG ==2)
mtv.setText("Second Slide");
//and so on...
FLAG = FLAG+1;//increment flag
}
};

Toggle button android

I want my toggle button's ON text to be large and OFF text to be small. But can't do it.. any suggestions? This is what i was trying
mf=(ToggleButton)findViewById(R.id.mf);
if(mf.isEnabled()==true)
{
mf.setTextSize(13);
}
else
mf.setTextSize(8);
Your code has to be called each time you click on your button. so use :
toggleButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (toggleButton.isChecked()) {
toggleButton.setTextSize(13.0f);
} else {
toggleButton.setTextSize(8.0f);
}
}
});
You can set OnClickListner with a easy method. In your .xml put the option
android:onClick="nameOfMethod"
And then in your code:
public void nameOfMethod(View v){
}
Where v is the togglebutton in this case ( ToggleButton mf = (ToggleButton)v; )
I put the solution here:
mf=(ToggleButton)findViewById(R.id.mf);
if(mf.isChecked())
{
mf.setTextSize(13);
}
else
mf.setTextSize(8);
Use isChecked() instead of isEnabled()
You need to do some debugging.
Firstly:
if(mf.isEnabled()==true)
can be
if (mf.isEnabled())
Does mf.setTextSize(13) on it's own work as expected?
Add some logging:
if (mf.isEnabled())
{
// Add some logging, is this line reached correctly?
mf.setTextSize(13);
}
else
// Add some logging, is this line reached correctly?
mf.setTextSize(8);
Chances are you need to change isEnabled() to isChecked(). isEnabled() means exactly that, whether it's enabled or not. You want to know whether the button has been checked.

Categories

Resources