The background color of my View is #FFFFFFFF. I want to get this through code. I do not want to just put #FFFFFFFF into the method because I will be changing the background through code, so this value will change all the time.
public void toggleEraser() {
ImageView btnEraser = (ImageView) this.findViewById(R.id.imgEraser);
ImageView btnBrush = (ImageView) this.findViewById(R.id.imgBrush);
if (erase) {
btnEraser.setImageResource(R.drawable.greyeraser);
btnBrush.setImageResource(R.drawable.brush);
} else {
btnEraser.setImageResource(R.drawable.eraser);
btnBrush.setImageResource(R.drawable.greybrush);
}
erase = !erase;
if (erase){
//Here is the problem
drawView.setColor(//drawView.getBackgroundColor());
}
else
drawView.setColor(brushColor);
drawView.setErase(erase);
}
store the color you want 0xFFFFFFFF in somevariable and then
change drawView.setColor( to drawView.setBackgroundColor(somevariable);
Check out this answer: Get the background color of a button in android. it is a little different but can lead you on the right path.
Related
I have one ImageView and set a drawable on it. Now I need to get the ID of the drawable on click event of ImageView dynamically. How can I get it?
imgtopcolor = (ImageView) findViewById(R.id.topcolor);
imgtopcolor.setImageResource(R.drawable.dr); // How do I get this back?
Now on touch event of imgtopcolor i want to need drawable id because I am setting different drawable each time and want to compare the drawable with other
I think if I understand correctly this is what you are doing.
ImageView view = (ImageView) findViewById(R.id.someImage);
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
ImageView imageView = (ImageView) view;
assert(R.id.someImage == imageView.getId());
switch(getDrawableId(imageView)) {
case R.drawable.foo:
imageView.setDrawableResource(R.drawable.bar);
break;
case R.drawable.bar:
default:
imageView.setDrawableResource(R.drawable.foo);
break;
}
});
Right? So that function getDrawableId() doesn't exist. You can't get a the id that a drawable was instantiated from because the id is just a reference to the location of data on the device on how to construct a drawable. Once the drawable is constructed it doesn't have a way to get back the resourceId that was used to create it. But you could make it work something like this using tags
ImageView view = (ImageView) findViewById(R.id.someImage);
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
ImageView imageView = (ImageView) view;
assert(R.id.someImage == imageView.getId());
// See here
Integer integer = (Integer) imageView.getTag();
integer = integer == null ? 0 : integer;
switch(integer) {
case R.drawable.foo:
imageView.setDrawableResource(R.drawable.bar);
imageView.setTag(R.drawable.bar);
break;
case R.drawable.bar:
default:
imageView.setDrawableResource(R.drawable.foo);
imageView.setTag(R.drawable.foo);
break;
}
});
I answered something like this in another question already, but will change it just a little for this one.
Unfortunately, there is no getImageResource() or getDrawableId(). But, I created a simple workaround by using the ImageView tags.
In onCreate():
imageView0 = (ImageView) findViewById(R.id.imageView0);
imageView1 = (ImageView) findViewById(R.id.imageView1);
imageView2 = (ImageView) findViewById(R.id.imageView2);
imageView0.setTag(R.drawable.apple);
imageView1.setTag(R.drawable.banana);
imageView2.setTag(R.drawable.cereal);
Then, if you like, you can create a simple function to get the drawable id:
private int getDrawableId(ImageView iv) {
return (Integer) iv.getTag();
}
Too easy.
As of today, there is no support on this function. However, I found a little hack on this one.
imageView.setImageResource(R.drawable.ic_star_black_48dp);
imageView.setTag(R.drawable.ic_star_black_48dp);
So if you want to get the ID of the view, just get it's tag.
if (imageView.getTag() != null) {
int resourceID = (int) imageView.getTag();
//
// drawable id.
//
}
Digging StackOverflow for answers on the similar issue I found people usually suggesting 2 approaches:
Load a drawable into memory and compare ConstantState or bitmap itself to other one.
Set a tag with drawable id into a view and compare tags when you need
that.
Personally, I like the second approach for performance reason but tagging bunch of views with appropriate tags is painful and time consuming. This could be very frustrating in a big project. In my case I need to write a lot of Espresso tests which require comparing TextView drawables, ImageView resources, View background and foreground. A lot of work.
So I eventually came up with a solution to delegate a 'dirty' work to the custom inflater. In every inflated view I search for a specific attributes and and set a tag to the view with a resource id if any is found. This approach is pretty much the same guys from Calligraphy used. I wrote a simple library for that: TagView
If you use it, you can retrieve any of predefined tags, containing drawable resource id that was set in xml layout file:
TagViewUtils.getTag(view, ViewTag.IMAGEVIEW_SRC.id)
TagViewUtils.getTag(view, ViewTag.TEXTVIEW_DRAWABLE_LEFT.id)
TagViewUtils.getTag(view, ViewTag.TEXTVIEW_DRAWABLE_TOP.id)
TagViewUtils.getTag(view, ViewTag.TEXTVIEW_DRAWABLE_RIGHT.id)
TagViewUtils.getTag(view, ViewTag.TEXTVIEW_DRAWABLE_BOTTOM.id)
TagViewUtils.getTag(view, ViewTag.VIEW_BACKGROUND.id)
TagViewUtils.getTag(view, ViewTag.VIEW_FOREGROUND.id)
The library supports any attribute, actually. You can add them manually, just look into the Custom attributes section on Github.
If you set a drawable in runtime you can use convenient library methods:
setImageViewResource(ImageView view, int id)
In this case tagging is done for you internally. If you use Kotlin you can write a handy extensions to call view itself. Something like this:
fun ImageView.setImageResourceWithTag(#DrawableRes int id) {
TagViewUtils.setImageViewResource(this, id)
}
You can find additional info in Tagging in runtime
I recently run into the same problem. I solved it by implementing my own ImageView class.
Here is my Kotlin implementation:
class MyImageView(context: Context): ImageView(context) {
private var currentDrawableId: Int? = null
override fun setImageResource(resId: Int) {
super.setImageResource(resId)
currentDrawableId = resId
}
fun getDrawableId() {
return currentDrawableId
}
fun compareCurrentDrawable(toDrawableId: Int?): Boolean {
if (toDrawableId == null || currentDrawableId != toDrawableId) {
return false
}
return true
}
}
A simple solution might be to just store the drawable id in a temporary variable. I'm not sure how practical this would be for your situation but it's definitely a quick fix.
Even easier: just store the R.drawable id in the view's id: use v.setId(). Then get it back with v.getId().
how to fill an image with a color in android? Like the image given below
You can set color to the icon by using the below code
ImageView imageView = (ImageView) findViewById(R.id.your_imageview);
imageView.setColorFilter(Color.RED);
Option 1: use backgroundTint property in xml file of ImageView
Option 2:
ImageView imageView = ...;
Drawable drawable = imageView.getDrawable();
ColorFilter colorFilter = ColorFilterGenerator.from(drawable).to(Color.RED);
imageView.setColorFilter(colorFilter);
Since,Android Drawable Tinting is supported in Android 5.0+ (API 21+) only. (not 100% sure ).
u can provide two different images in drawable folder and set it problematically when user clicks on it which will work in every android version.
public boolean enable = false;
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(enable)
{
imageView.setImageResource(R.drawable.ic_home_enable);
}
else
{
imageView.setImageResource(R.drawable.ic_home_disable);
}
}
});
I have a problem like this
in ImageView, by default I set ivImage.setImageResource(R.drawable.avatar)
And when I click button "Save", if image in ImageView is not changed, it should not save image. I tried this, but it's not working
private Drawable oldDrawable;
in onCreate()
oldDrawable = imgAvatarDoctor.getDrawable();
and in button click
if (imgAvatarDoctor.getDrawable() == oldDrawable) {
isNoError = false;
}
So how can i fix that? thanks so much
Use setTag and getTag for comparison of your drawable like this:
initially set tag of your imageview to 0 in your onCreate
imgAvatarDoctor.setTag("0");
and whenever you change the imageview you can change the tag for something other than 0 like:
imgAvatarDoctor.setTag("UpdatedTag");
Now on your click listener you can do this:
if (imgAvatarDoctor.getTag().equalsIgnoreCase("UpdatedTag")){
//your image view is updated
} else {
// your image view is not update
}
Happy Coding !!!!!
you can use bitmap instead,
private Bitmap oldDrawable;
oldDrawable = ((BitmapDrawable) ivImage.getDrawable()).getBitmap();
if (oldDrawable == oldDrawable) {
isNoError = false;
}
You can use a flag to trigger image change.
In onCreate()
imageChanged = false;
And in function which gets changed image(i.e. onActivityResult), update this flag as
imageChanged = true;
then on save button click you can easily check for this flag :
if(imageChanged)
//save image
else
//pass
While setting the image, set any tag to the image
image.setTag(res); //here, res is drawableID
While checking the ID of image, you can use this tag to compare:
int oldImage = image.getTag(); //oldImage will be same as drawable ID
Now you can compare oldImage with drawable ID of that image and check whether its updated or not.
I want to get the current background to do a condition base on it.
For example, I have a xml with a next arrow, if the background=R.drawable.A, I want to change the background to R.drawable.B when next Button is pressed.
I defined my relative layout as follows :
final RelativeLayout rl = (RelativeLayout) findViewById(R.id.myLayout);
if (rl.getBackground()== R.drawable.A){ //here the error
rl.setBackgroundResource(R.drawable.B);
}
The error is :
incompatible operands types int and drawable.
If there a way to get the current background and base on it do something?
Actually I don't know why they didn't override the equals method in the Drawable class. So you should use getConstantState() method from the Drawable object that returns a Drawable.ConstantState instance that holds the shared state of this Drawable to be able to compare it.
Kotlin
val drawableAConstantState = ContextCompat.getDrawable(this, R.drawable.A)?.constantState
rl.setBackgroundResource(if (rl.background?.constantState == drawableAConstantState) R.drawable.B else R.drawable.A)
Java
if (rl.getBackground() != null && rl.getBackground().getConstantState().equals(getResources().getDrawable(R.drawable.A).getConstantState()) {
rl.setBackgroundResource(R.drawable.B);
} else {
rl.setBackgroundResource(R.drawable.A);
}
You can check if the background drawable is null or not like this:
if (rl.getBackground() != null){
rl.setBackgroundResource(R.drawable.B);
}else{
// do whatever you want
}
Well to remedy the error just change it to:
final RelativeLayout rl = (RelativeLayout) findViewById(R.id.myLayout);
if (rl.getBackground() == getResources().getDrawable(R.drawable.A){ //<-- fixes the error
rl.setBackgroundResource(R.drawable.B);
}
a side note: I suggest you check which drawable should be set against a logical component and not it's manifestation in the UI, but this is up to you.
my problem is, I have custom listView filled from Runnable returnRes. It fills particular data in layout I have named as lay (R.id.layoutList). My aim is to have different colour for each lay in my listView, I want to switch colours between each. 1st is dark blue, second light blue, thir dark blue and so on... This code is doing well, but with no result, my custom listView is still black, when I change it in XML, it is changing, but not when it is set from Java. Any ideas?
Thanks
private Runnable returnRes = new Runnable() {
#Override
public void run() {
if(myTasks != null && myTasks.size() > 0){
TasksAdapter.notifyDataSetChanged();
LinearLayout lay=(LinearLayout)findViewById(R.id.layoutList);
for(int i=0;i<myTasks.size();i++){
TasksAdapter.add(myTasks.get(i));
if(i>0){
if(i%2==0){
lay.setBackgroundColor(R.color.background);
}
}else{
if(i>0){
lay.setBackgroundColor(R.color.lightBlue);
}
}
}
}
ProgressDialog.dismiss();
TasksAdapter.notifyDataSetChanged();
}
};
Try googling. getResources().R.color.lightBlue is not the actual color, it's the id of
the color resource (which is an integer code for the color). It will work fine if you use it
in an API which expects ids of resources, but setBackgroundColor
actually needs the code of the color.
colors and ids are both just coded as int when you come down to it, so it's
really easy to confuse one for the other.
yourlayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.yourbackgroundimage))