how to convert TextView into ImageView in android - java

I need to convert TextView into ImageView so I will be able to save the ImageView as an image in the sd card.
How do I convert TextView into ImageView?
Thanks.
(I saw the other questions but them are not realy answer my question).

Yes there is a way to do this, by two ways.
You can create an Bitmap of any View using buildDrawingCache() and getDrawingCache()
TextView tv = (TextView)findViewById(R.id.textview);
tv.buildDrawingCache();
ImageView img = (ImageView)findViewById(R.id.imageview);
img.setImageBitmap(tv.getDrawingCache());
You can also check this answer for further reference.
In the other way , you can take your whole rootview as a screen shot and you can save it into disc.
This will help to achieve the above How to programatically take a screenshot on Android?

public static Bitmap convertViewToBitmap(View view)
{
view.measure(
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
);
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
return bitmap;
}

Related

converting CardView from RecyclerView to a Bitmap

I have a CardView (with multiple items like imageView, textView, Buttons) in a RecyclerView. I want to get bitmap from this cardview. I tried a simple method of converting cardView into bitmap (convert a cardview to bitmap) but its not working properly. I only got the raw view of the card just like in xml without any updated items from a firebaseserver.
CardView extend view, so you probably can convert it as any other view,
try to pass the CardView into that function:
public static Bitmap getBitmapFromView(View view) {
//Define a bitmap with the same size as the view
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
//Bind a canvas to it
Canvas canvas = new Canvas(returnedBitmap);
//Get the view's background
Drawable bgDrawable =view.getBackground();
if (bgDrawable!=null)
//has background drawable, then draw it on the canvas
bgDrawable.draw(canvas);
else
//does not have background drawable, then draw white background on the canvas
canvas.drawColor(Color.WHITE);
// draw the view on the canvas
view.draw(canvas);
//return the bitmap
return returnedBitmap;
}
you are probably taking the screenshot of view before it is fully inflated. Try adding a 2 second delay or implement this on layout listener of the card view

Pass drawable image in an ImageView variable

how to convert an image from drawable file to an Image view
because this won't work
ImageView image= R.drawable.pic;
Try this
ImageView imageview = findViewById(R.id.imageView);
imageview.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.pic));
Try this
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
fab.setImageDrawable(getResources().getDrawable(R.drawable.pic,this.getTheme()));
}else {
fab.setImageDrawable(getResources().getDrawable(R.drawable.pic));
Try this
ImageView image = new ImageView(this);
image.setImageResource(R.drawable.pic);
or
ImageView image = new ImageView(this);
image.setImageDrawable(getResources().getDrawable(R.drawable.pic));

Android Load Many Images Efficiently

I have a lot of PNG images contained in the various Drawable folders (Drawable-xhdpi, Drawable-mdpi, etc.) They are all relatively small (at most like 10KB) but at one point I need to load about 50 of them onto the screen. When I do this, it causes an OutOfMemoryError. Ideally, I would like to be able to load these images by simply calling setContentView once (the content view has a bunch of ImageViews with their src attribute already set to the corresponding images). That's what I'm doing now, but of course this isn't working because of the memory error. Besides reducing the size of the images, is there any way to prevent the OutOfMemoryError?
Avoid loading this number of images all at once,
instead you can load them in a GridView as described here.
Use Picasso with GridView for memory efficiency
public View getView(int position, View convertView, ViewGroup container) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
} else {
imageView = (ImageView) convertView;
}
// Load image into ImageView "using Picasso"
Picasso.with(mContext).load(imageResIds[position]).into(imageView);
return imageView;
}

Android ImageView Layout Issues

There very well may be a duplicate question, but I have yet to find it. I am doing thing all programmatically, not using the xml. Basically what I am trying to do is to have an EditText appear below an image. I am using RelativeLayout with an ImageView and and EditText.
These are the parameters that I am setting up for the ImageView and EditText:
RelativeLayout.LayoutParams editTextParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
editTextParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
editTextParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
editTextParams.width=500;
RelativeLayout.LayoutParams imageParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
imageParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
imageParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
imageParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
imageParams.addRule(RelativeLayout.ABOVE, editText.getId());
Which I have verified correctly places the EditText in the bottom right corner and the image above. What I run into is if the picture it "too tall" then it covers the EditText. I also tried using
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
and it stretches it over the EditText as well.
The full code that I am using is this
RelativeLayout layout = (RelativeLayout) findViewById(R.id.mainLayout);
ImageView imageView = new ImageView(this);
EditText editText = new EditText(this);
RelativeLayout.LayoutParams editTextParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
editTextParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
editTextParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
editTextParams.width=500;
RelativeLayout.LayoutParams imageParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
imageParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
imageParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
imageParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
imageParams.addRule(RelativeLayout.ABOVE, editText.getId());
imageView.setLayoutParams(imageParams);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
editText.setLayoutParams(editTextParams);
Bitmap image = getImage();
imageView.setImageBitmap(image);
layout.addView(editText);
layout.addView(imageView);
Thanks. Any suggestions would be greatly appreciated.
Create a LinearLayout and then add the two components in it (ImageVIew and EditText)
Here is what you should do;
Set the orientation to vertical for the horizontal layout and width to whatever you need
Set the with of both components to 0 and weights to 1 each
After that, you should have the two items one above the other;
I hope this helps
One thing you can do is fix the imageview width and height. This way you can control the maximum size without the imaging going over the edit text.
RelativeLayout.LayoutParams imageParams = new RelativeLayout.LayoutParams(256, 256);
Hopefully this helps.
Your editText has a vertical MATCH_PARENT. Shouldn't it be WRAP_CONTENT?
(I am not sure if this helps, but it might help, as the MATCH_PARENT contradicts your planned layout, and your screenshot indicates that the editText is vertically centered.)

Setting an Image to ImageView dynamically

Please forgive me if this is a duplicate post but I tried and could not find anything on this subject.
I have a blank ImageView in a layout and now I want to put an image there dynamically. Since there is
TextView txt = (TextView) findViewById(R.id.textView1);
txt.setText("my text");
is there a way to do it for an ImageView like the way you would do it for a TextView?
ie...
ImageView image = (ImageView) v.findViewById(R.id.pPicture);
image.setImage(R.drawable.myImage); // I know this isn't correct.
Any help is much appreciated.
Try this
image.setImageResource(R.drawable.yourimage);
or
image.setImageDrawable(getResources().getDrawable(R.drawable.yourimage);
image.setImageResource(int resId)
If you want to display an image file on the phone, you can do this:
private ImageView imgView;
imgView = (ImageView) findViewById(R.id.imageViewId);
imgView.setImageBitmap(BitmapFactory.decodeFile("pathToImageFile"));
If you want to display an image from your drawable resources, do this:
private ImageView imgView;
imgView = (ImageView) findViewById(R.id.imageViewId);
imgView.setImageResource(R.drawable.imageFileId);
To avoid every problem you can use the sure way
Resources res = getActivity().getResources();
Drawable drawable= res.getDrawable(R.drawable.myImage);
image.setImageDrawable(drawable);

Categories