Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert TextView into ImageView in android

Tags:

java

android

xml

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).

like image 793
guy1820 Avatar asked Aug 09 '15 07:08

guy1820


People also ask

How to convert TextView to ImageView in Android?

TextView tv = (TextView)findViewById(R. id. textview); tv. buildDrawingCache(); ImageView img = (ImageView)findViewById(R.

Which layout you can not use to add a TextView on an ImageView?

Do you mean you want to put a TextView in fromt of an ImageView? You can't do this simply with a LinearLayout because this puts all views in a line side by side.


2 Answers

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?

like image 193
King of Masses Avatar answered Oct 30 '22 02:10

King of Masses


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;  
} 
like image 39
Pepe Peng Avatar answered Oct 30 '22 02:10

Pepe Peng