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);
}
});
Related
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.
I've been searching for a solution for this for a while but cannot seem to get one working. There are one or two on here about this subject but I can't seem to get them going. I'm also a novice in Android and while I've been on and off playing with it for a few years, I still understand next to nothing about what I'm writing.
Basically I've got a TextView and a button. Ideally I'd like to put some text in the TextView, press a button it's gone, press the button again and it's back.
I've narrowed it down to needing to understand what findViewById(R.id.button2) does but honestly I'm a bit lost.
I've added my button code but apologies that this is such a noob question
public void onClick(Button v){
TextView t1 = (TextView)findViewById(R.id.editText);
v.setVisibility(View.GONE);
Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TextView t1 = (TextView)findViewById(R.id.TextView);
v.setVisibility(View.GONE);
}
});
}
Your code has a couple of issues. I'm not going to give you the code because that won't really help you learn. Instead I'll explain things and let you try to figure it out or come back with more explicit questions.
You know that xml file you set using setContentView? Some of the tags in it had a property android:id="xxxx". That xxxx is the id of that view, its used so you can find that view in your code. The findViewById function walks through all the views on screen and finds a view with that id and returns it. That gives you a reference to the view so you can change it. For example, you can set its visibility, set its background color, or set an OnClickListener.
So to have a button toggle the visibility of another view, you need to be able to do the following things:
1)Find the view who's visibility you want to change
2)Figure out what its visibility currently is
3)Figure out what you want it to be (the opposite of what it currently is
4)Set that visibility
You need to write a function that does all that. Then you need to do this
1)Find the button you want to use to change the visibility
2)Tell it to call your function when its pressed.
Figure out how to do each of those steps individually, and you should be able to put it together. Good luck.
findViewById(R.id.button2) finds the view with the id button2.
You can check inside onClick whether t1 is visible or not (t1.setVisibility(View.GONE); not v.setVisibility(View.GONE);), and toggle between View.GONE and View.VISIBLE.
Remember that your findViewById() should have a real id. They are normally set on the activity_name.xml.
You are using a onClick inside a onClick. Personally I recommend setting the listener manually with setOnClickListener.
There's a lot of work for you, start with these tutorials. Keep trying and try to understand what you are doing.
Look like you need a toogle button feature, here is a piece of code.
Important: you must pay heed to #GabeSechan and #SkyDriver2500 answers.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
//your other code
Button button = (Button) findViewById(R.id.button2);
final TextView t1 = (TextView) findViewById(R.id.editText);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
t1.setVisibility(t1.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
}
});
}
I'm not sure if the code will help you now. But just in case, here it is
final boolean[] isTvVisible = {false};
final TextView t1 = (TextView)findViewById(R.id.editText);
t1.setVisibility(View.GONE);
Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (isTvVisible[0]) {
t1.setVisibility(View.GONE);
isTvVisible[0] = false;
} else {
t1.setVisibility(View.VISIBLE);
isTvVisible[0] = true;
}
}
});
I'm new to Android and I'm trying to change the content of an ImageView with a button and if I press the button again the image changes back. I thought it would be easy with an if-else statement but I have been looking around in the ImageView API and I don't see the method that allows me to get the image that is being displayed in that moment... Any ideas?
Here is my code so far...
boton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
imagen.setImageResource(R.mipmap.imag1);
}
});
I didn't copy the rest of the code because I dont think it's necessary
You can get the image of the button with using this method:
Bitmap bitmap = ((BitmapDrawable)imagen.getDrawable()).getBitmap();
To set image of a button with another bitmap you can use:
imagen.setImageBitmap(bitmap);
Well, I developed a menu with eight Buttons for an App. So, every time the user clicks on in one of the buttons, such button changes its background. And I would would like to change its color as well. But I got now idea how, since setTextColor does not work with Views.
I'm using View because its part of onClick method that I override in order to achieve what I want. So, here go the code:
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
v.setBackgroundResource(R.drawable.degrade_menu);
}
So, how could I change the text color?
Cheers,
Cast your v to TextView and then set the text color. Do not forget to read color from resourse
((TextView)v).setTextColor(getResources().getColor(R.color.errorColor));
quick solution:
final Button button = (Button) findViewById(<id>);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
button.setTextColor(<color>);
}
};
better solution: use states
Cast the view to a button. Then you can use settextcolor
I want to change the image of a button in my code. I found this can be done in xml:
please check this link
but the image will not stay on after I released the button. I want to click the button
and the button change to a new image. Can this be done?
in onClick method you need to change the Button's image, this way..
public void onClick(View v) {
if(v==buttonName){
buttonName.setBackgroundResource(R.drawable.imageName);
}
}
Assuming an ImageButton...
You can change the background image in an onClick listener, similar to the following:
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//set button image
myButton.setImageBitmap(myBitmapFile)
}
});