How to get image from an ImageButton? - android

I'm not sure about the code, but I tried doing this:
ImageButton stuff = (ImageButton) createView.findViewById(R.id.stuff);
int stuff2 = stuff.getId();
champs.setImageResource(stuff2);
But that didn't work, the Imagebutton was shrinked for some reason, there was no image, just a gray rectangle.

try this
Bitmap bitmap = ((BitmapDrawable)stuff.getDrawable()).getBitmap();

stuff.getId();
This will return the id of the View not the image resource associated with it. So you won't be having a resource related to this that is why you are not seeing the Image.
Set a valid drawable to setImageResource Some thing like R.drawable.drawable_id

Try this
ImageButton stuff = (ImageButton) createView.findViewById(R.id.stuff);
Drawable d = stuff .getBackground();
champs.setBackgroundDrawable(d);

This will help get the drawable object and set the drawable object.You can also get the Bitmap object for image drawable This will set the exact drawable associated with the imagebutton
ImageButton mButton = new ImageButton(this);
Drawable drawable = mButton.getBackground();
champs.setImageDrawable(drawable)

You need to set the ID of drawable resource but you are setting ID of the ImageButton as image resource.
here stuff.getId() returns R.id.stuff.
set the valid drawable resource like
ImageButton stuff = (ImageButton) createView.findViewById(R.id.stuff);
stuff.setImageResource(R.drawable.id_of_image);

Related

Why is only one ImageView drawn to my Layout

I want to add multiple ImageView, that show a drawable, to a RelativeLayout. This short example should add two such ImageViews:
//get drawable
Drawable oval = this.getResources().getDrawable(R.drawable.circle);
// set color
oval.setColorFilter(imgEl.getColor(), PorterDuff.Mode.SRC_ATOP);
// create new image view
ImageView imageView = new ImageView(this);
//set drawable and add to layout
imageView.setImageDrawable(oval);
imageView.setLayoutParams(new RelativeLayout.LayoutParams(100,100));
imageView.setPadding(0,0,0,0);
mLayout.addView(imageView);
//get drawable
oval = this.getResources().getDrawable(R.drawable.circle);
// set color
oval.setColorFilter(imgEl.getColor(), PorterDuff.Mode.SRC_ATOP);
// create new image view
imageView = new ImageView(this);
//set drawable and add to layout
imageView.setImageDrawable(oval);
imageView.setLayoutParams(new RelativeLayout.LayoutParams(100,100));
imageView.setPadding(200,200,0,0);
mLayout.addView(imageView);
Unfortunately, only the first one is shown, but the second one is not drawn on the screen. What is my mistake?

How to convert a textview to Imageview

I am trying to get a Imageview from textview. I tried many methods but not able to set that textview on Imageview as image.
I tried following method:
TextView tv = (TextView)findViewById(R.id.textview);
tv.buildDrawingCache();
ImageView img = (ImageView)findViewById(R.id.imageview);
img.setImageBitmap(tv.getDrawingCache());
but i am not still able to show that textview on Imageview. Can someone help me that how can i set textview as image on that imageview?
Try this, if it maya help you
yourTextView.setDrawingCacheEnabled(true);
yourTextView.buildDrawingCache();
yourImageview.setImageBitmap(tv1.getDrawingCache());
call
tv.buildDrawingCache(true);
Bitmap b = tv.getDrawingCache(true).copy(Bitmap.Config.ARGB_8888, false);
tv.destroyDrawingCache();
img.setImageBitmap(b);

android get the drawable that has sett as drawabletop of textview

i have textview
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:drawableTop="#drawable/new" />
and i want on activity jave to know which image has set on drawableTop, how ?
Use
Drawable[] drawables = textView.getCompoundDrawables();
Also if you want to compare two drawable.
Bitmap bitmap = ((BitmapDrawable)drawables[1] ).getBitmap();
Bitmap bitmap2 = ((BitmapDrawable)getResources().getDrawable(R.drawable.twt_hover)).getBitmap();
if(bitmap == bitmap2)
{
//Code blcok
}
First, give your TextView an ID so you can find it in your Activity:
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:drawableTop="#drawable/new" />
Second, in your Activity find your TextView and get the compound drawables in your onCreate method after calling setContentView:
TextView textView = (TextView) findViewById(R.id.textView);
// Left, top, right, bottom drawables.
Drawable[] compoundDrawables = textView.getCompoundDrawables();
// This is the top drawable.
Drawable topCompoundDrawable = compoundDrawables[1];
I think you should use textView.getCompoundDrawables(); As for getting from drawable top i would think it would be the second drawable eg
Drawables tmp[] = textView.getCompoundDrawables();
tmp[1]; //this should be your top drawable but not 100% sure.
You cant get drawable ID in runtime. TextView uses ID only in it's constructor to load corresponding drawable. After drawable is loaded ID is gone.
In some cases there is no drawable ID at all. drawableTop can be a color rgb value.

How to retrieve an image from ImageView when Click on that image?

i have a ImageView in the layout, when i click on the image i want to get that image into a variable and replace with another image in this ImageView. please help me..
The onClick Listener will give you a View, that's the ImageView that was clicked. Cast it to an ImageView and do whatever you want with it.
in this example i have take previous image in Drawable and replace i with new image. if you set any imageview to image which stay in drawable variable(d) then use :: setBackgroundDrawable(d); is useful
public void onClick(View v){
ImageView i;
i = (ImageView) findViewById(R.id.img);
Drawable d = i.getBackground();
i.setBackgroundResource(R.id.secondImage);
}

How set an image in Android ImageView

I need to show a image, when i press a button on my application.
I'm trying with:
ImageView imageView = (ImageView) findViewById(R.id.imageView2);
imageView.setImageDrawable(R.drawable.loading);
but it shows an error.
Anyone can help me? Thanks.
import package_name.R.drawable;
ImageView imageView = (ImageView) findViewById(R.id.imageView2);
imageView.setImageResource(drawable.loading);
OR
you can set the image from the start, set it to invisible and when you want to show it just change the visibility property.
for future visitors:
sometime setImageDrawable(Drawable) method show an error like:
The method setImageDrawable(Drawable) in the type ImageView is not applicable for the arguments (int)
can try following codes:
ImageView imageView = (ImageView) findViewById(R.id.myImageView);
Drawable image = getResources().getDrawable(R.drawable.your_image);
imageView.setImageDrawable(image);
--------- OR can try this ------------------
imageView.setImageDrawable(ContextCompat.getDrawable(YourActivity.this, R.drawable.your_image));
hope it work for you.

Categories

Resources