I am facing a situation in which-
1.First I have to set the background image of a ImageButton to a default image.
2.Now at runtime based on some condition I have to change the background image of that ImageButton to some other image.
I have tried this but it doesn't work and the background image remains to default image and
doesn't change.
btn.invalidateDrawable((Drawable)getResources().getDrawable(R.drawable.info));
btn.setBackgroundDrawable((Drawable)getResources().getDrawable(R.drawable.infonewred));
How can I achieve this goal.
Have you tried btn.setBackgroundResource(R.drawable.infonewred).
I suggest you set the "default" Background directly in your XML-Layout-File via android:background.
If you have to change the Background in Runtime just use btn.setBackgroundResource(R.drawable.imagename);
since you haven't provided xml, i am guessing you are providing src to imageButton.. so to change the source of the image , what you are doing is changing the image background which will not be visible since it is the source which will be visible to you.. so to change source..
do this.
btn.setImageDrawable()
rather than..
btn.setBackgroundDrawable()
I know that's old but i got the solution as i'm also was searching for that, the correct answer is to use setImageResource , so the code will be like that:
btn.invalidateDrawable(getResources().getDrawable(R.drawable.info));
btn.setImageResource(R.drawable.infonewred);
Try the method setImageResource
Ex:
if(someCondition()){
btn.setImageResource(R.drawable.info);
} else {
btn.setImageResource(R.drawable.infonewred);
}
Just try following code to change at runtime and for default view set background property in layout xml:
if(yourCondition)
btn.setBackgroundResource(R.drawable.infonewred);
in activity file you can write
btn.setBackgroundResource(R.drawable.sliderr);
OR
in your xml layout file you can add one extra line to you button xml code
android:src="#drawable/sliderr"
Related
I am using a Translucent AppCompatActivity, and in the layout of that activity I am trying to use an ImageView. I am putting the src of the image by using the srcCompat tag. I do the see the image on the design view of the Xml file, but not able to see that being rendered on the phone screen.
Thanking in advanced.
First try using the Layout Inspector tool to verify that your ImageView has at least its width and height well defined. Go to Android Studio->Tools->Layout Inspector and analyze your layout.
https://developer.android.com/studio/debug/layout-inspector?hl=en
You need to add vectorDrawables.useSupportLibrary = true inside your Gradle file if it not there. Browse to this https://developer.android.com/guide/topics/graphics/vector-drawable-resources you can find more data here. If it is not resolved by adding this, you can use android:src attribute instead of srcCompact
I tried this code:
LinearLayout someLayout=(LinearLayout)view.findViewById(R.id.someLayout);
someLayout.setBackgroundTintList(context.getResources().getColorStateList(Color.parseColor("#ff8800")));
But I'm getting an error: android.content.res.Resources$NotFoundException
I'm getting the color hex from external source so I can't embed it in colors.xml.
Also I want to change the tint, not the background so setBackground is not an option.
I figured I can't use getColorStateList() so I searched for another way to do it.
At the end I was able to set color tint using the following code:
LinearLayout someLayout=(LinearLayout)view.findViewById(R.id.someLayout);
someLayout.getBackground().setColorFilter(Color.parseColor("#ff8800"), PorterDuff.Mode.SRC_ATOP);
This worked as if I changed the backgroundTint property in the xml file, so it's perfect for my problem.
I was able to manage using the following line. change it to your circumstances.
myView.getBackground().setTint(currentView.getResources().getColor(R.color.colorAccent));
For Kotlin ,
I modified #Krestek answer :
someLayout.background.setColorFilter(Color.parseColor("#ff8800"), PorterDuff.Mode.SRC_ATOP)
You can't do this like that because getColorStateList method expect int id of resource, and you are passing RGB color int. You should create color state list following this link
and then set it like this:
.getColorStateList(R.color.your_xml_name)
In my application I have a CameraPreview. I would like to add an imageView over it so I need the imageView to have a transparent background. The src of the imageView will be a png so I could see the CameraPreview underneath.
However, it's like I've tried everything to manage the background of the ImageView Transparent:
change the alpha value (in the code AND in the xml file)
change the background to #null then to #00000000 (in the code AND in the xml file for both)
But it still not work !
Did I miss anything ?
Please ask if you need parts of my code.
P
Ok thanks for your answears but I have managed to solve my problem:
I gave a random color to the background of my layout (for example #FF34035544) and I changed the two first letters to 00 which made #0034035544.
It was finally very simple !
I cannot seem to change the background image of my image button. Heres the code i'm currently trying to use:
ImageButton imgButton = (ImageButton) findViewById(R.id.showSportsButton);
imgButton.setBackgroundResource(R.drawable.tab2_selected);
However this seem to be placing the new image on top of the old image leaving me with 2 images overlapping each other.
Does anyone know why this is??
For solving this question you should implement
imgButton.setImageResource(R.drawable. tab2_selected);
Use this method:
imgButon.setBackground(getActivity().getDrawable(R.drawable.your_icon));
in xml file, <Button> write: android:backgroud="#drawable/your_file"
Make sure you are on same activity. If you are changing background of different activity first create the constructor then use object to change button.
Activity obj= new activity();
obj.imgButton.setBackgroundResource(R.drawable.tab2_selected);
and also check that oncreate() method has void return type if you are using only findviewbyid.
how to use color state list for background?
I know android:background="#drawable/drawable_selector", but android:background="#color/color_selector" will cause exceptions.
but android:background="#FFFFFF" works again, can anyone explains why?
now i want to change a layout's background color(not a drawable) when it's pressed,
how to do it?
dynamically you can change like this. Use this if it is useful for you -
textView.setBackgroundColor(Color.parseColor(getResources().getString(R.string.red)));
put the color in res/values/colors.xml,
like #FFFFFF,
and then create a drawable xml in drawable directory,
,that is ok.