Android convert TextView to image

If your application allows user to convert a TextView containing text to an image, this tip is useful to you. The code to achieve this goal is simple. However, there are two important things you need to know. The first thing is how to convert the TextView that contains text without losing some words. If you have a long text in the TextView, the width of the TextView is less than the length of text. So part of the text that is outside the TextView 's bounds will not be placed in the result image. One solution to this problem is wrapping the TextView in a ScrollView. By doing this, you make sure that the size of the TextView matches the text so the caching bounds cover the entire text.



Another important thing is that you have to place the code to convert the TextView to image in a place where the TextView is already completely laid out so you avoid NullPointerException error. This place should be in the onWindowFocusChanged() method of your activity.
Now to have an example application on converting TextView to image, you create a new Android project. Then in the activity_main.xml file, you add a TextView inside a ScrollView as shown below.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
  
   tools:context="com.example.myapp.MainActivity" >  
  
   <ScrollView
     android:id="@+id/scrview"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<TextView
android:id="@+id/txtview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:textSize="22sp"

/>
</ScrollView>
  
</RelativeLayout>


Here is the code written in the MainActivity class to convert the TextView to image using setDrawingCacheEnabled() and getDrawingCache() methods.

public class MainActivity extends Activity{
   TextView tv;
   protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     tv=(TextView)findViewById(R.id.txtview);
     tv.setText("This TextView will be converted to Bitmap image. To make sure the entire text is placed in the image, the TextView is placed in a ScrollView.");
    
     }
  
    
   public void onWindowFocusChanged(boolean hasFocus){
   super.onWindowFocusChanged(hasFocus);
   tv.setDrawingCacheEnabled(true); // Enable drawing cache before calling the getDrawingCache() method
   // Get bitmap object from the TextView
     Bitmap tvImage= Bitmap.createBitmap(tv.getDrawingCache());
     try {
     // Save the bitmap object to file
tvImage.compress(CompressFormat.PNG, 100, new FileOutputStream(Environment.getExternalStorageDirectory()+"/tvimage.png"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
    
    
   }
}

Android-convert-TextView-to-image

Posted by: Dara | post date: 01-10-2015 | Subject: Android Apps Development

write programming tip




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.