in android, how would you extract the image from a imagebutton? - java

I have two imagebutton and I want to switch the image of each other so button a will have button b's image while button b will have button a's image. I tried to do this in my code, but it does not work
Bitmap temmp = a1.getDrawingCache();
a1.setImageBitmap(a2.getDrawingCache());
a2.setImageBitmap(temmp);

follow like this
buttona.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ImageButton ib = (ImageButton)v;
Drawable d11 = ib.getDrawable(); // this is the image u can get from that button
}

You need to enable the drawing cache before calling the getDrawingCache() with setDrawingCacheEnabled(true).

Related

Change the color of the icon in the ImageView in the java class

I showed the icons in the program via ImageView. The yellow icons should turn red when the button is pressed. I tried all the methods, but none of them worked. ...Backgroundcolor... He turned ImageView red in full square shape. I just want the icon to change color. This is very difficult‚ Please help. Thank you.
My code:
ImageView Test = (ImageView) findViewById(R.id.TestImageIcon);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Here
Test.setcolor(R.id.red);
}
});
you change the tint of drawable like that:
imageView.setColorFilter(context.getResources().getColor(R.color.yourColor), PorterDuff.Mode.SRC_IN);
It can be done using setColorFilter(color).
Try to change color of icon programatically with
Test.setColorFilter(ContextCompat.getColor(YourActivity.this, R.color.red));

ImageView on again click

I want my ImageView to change it's drawable resource as it is pressed. The problem occurs when ImageView is pressed for the second time.
Let me explain, if ImageView is pressed first time, I want it to change from drawable A to drawable B. If ImageView is pressed again I want it to change from drawable B to drawable A.
That pressed again part is not working..
Here's my code:
public void imageViewBiljeskeNaListiCheckMarkMetoda(View view){
imageViewBiljeskeNaListiCheckMark = (ImageView) findViewById(R.id.imageViewBiljeskeNaListiCheckMark);
if (view == imageViewBiljeskeNaListiCheckMark){
imageViewBiljeskeNaListiCheckMark.setImageResource(R.drawable.ic_biljeske_obavljeno);
} else {
imageViewBiljeskeNaListiCheckMark.setImageResource(R.drawable.ic_biljeske_nije_obavljeno);
}
}
Remove this from the method....
You need to init the object once in the onCreate...
imageViewBiljeskeNaListiCheckMark = (ImageView) findViewById(R.id.imageViewBiljeskeNaListiCheckMark);
Then add a boolean variable to control the state of the view. .
public void imageViewBiljeskeNaListiCheckMarkMetoda(View view){
flag =!flag;
if (view == imageViewBiljeskeNaListiCheckMark){
if (flag) {imageViewBiljeskeNaListiCheckMark.setImageResource(R.drawable.ic_biljeske_obavljeno);
} else {
imageViewBiljeskeNaListiCheckMark.setImageResource(R.drawable.ic_biljeske_nije_obavljeno);
}
}
I sugggest to use the "tag" of the view, and keep in the tag the information you need (e.g. if the view is pressed or not)
http://developer.android.com/intl/es/reference/android/view/View.html#setTag(java.lang.Object)
Can't you just use a toggle method like this?
private void toggleDrawableOnClick(){
/* now you can check to see if the set drawable is A using its id */
if(visible drawable is A){
imageViewBiljeskeNaListiCheckMark.setImageResource(R.drawable.ic_biljeske_nije_obavljeno);
}else{
imageViewBiljeskeNaListiCheckMark.setImageResource(R.drawable.ic_biljeske_obavljeno);
}
}
This should be easier I believe!!

How to check image resource of an imageview programmatically?

I want to check background of my imageview and do sth if it equals to some drawable in my drawable folder. How can I do that? I have try this code but no answer:
layoutRoot.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
if (view.getBackground() == R.drawable.grid_single_bg_selected) {
//Do sth
}
}
});
When you set the background drawable also do set a tag to the imageview
imageView.setTag(R.drawable.demo);
And then compare
if (((Integer) imageView.getTag()) == R.drawable.demo){
// Do stg
}
If you are setting the imageView programmatically, while setting, also set a tag to this imageView. Now, when you want to find out the drawable, you can do it by retrieving the tag of this imageView.

How to toggle picture src of ImageButton

I have an image button and want that when the user clicks on it-it changes its src to a different drawable-but i want the background color i defined in xml to remain the same. Here is the code that i have done so far, but doesnt work, because im changing background and not the source-but general concept i will use:
public void onClick(View view) {
if (bgenabled == true) {
holder.ib.setBackground(res.getDrawable(R.drawable.location_deactive));
bgenabled = false;
} else { holder.ib.setBackground(res.getDrawable(R.drawable.location_active));
bgenabled = false;}
Just call the setImageDrawable to replace the current image you are using
with your ImageButton.
ImageButton button;
...
button.setImageDrawable(getResources().getDrawable(R.drawable.new_image));

How to assign an event to a drawable button?

I am trying to assign an event to a button, the button is declared as a Bitmap, I cannot seem to figure out how to add a listener to it, or even simply an event that I can call when the button is touched.
moregamesbtn = BitmapFactory.decodeResource(getResources(), R.drawable.moregames);
Bitmap moregamesbtn;
Edit - I attempted this, with no success;
moregamesbtn = BitmapFactory.decodeResource(getResources(), R.drawable.moregames);
ImageButton imgButton = (ImageButton) findViewById(R.drawable.moregames);
imgButton.setImageBitmap(moregamesbtn);
imgButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
Log.e("MyApplication", "Pressing more games button");
}
});
You will have to assign your Bitmap to the Button background
button.setBackgroundDrawable(bdrawable);
Then make a onButtonClick Listener.
EDIT:
you might want to check this out as well:
http://www.mkyong.com/android/android-imagebutton-example/

Categories