Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert text of a TextView to bitmap in Android?

I'm a beginner in Android development and am trying to develop a program where the user can convert the text being displayed in the TextView (TextView is displaying a code 39 barcode font text which I've imported from assets) to bitmap after pressing the "Convert to Bitmap!" button. I've tried searching around google but I've only managed to get answers like converting string to bitmap without guides on where to type the codes so I'm rather confuse on that. I've tried running the program with the codes I tried typing out after googling around but it crashes everytime I press the convert button.

Really hope that you can help! Thank you in advance! :D

The following are my codes so far:-

*Edited with respect to Simon's code

At java:

public class MainActivity extends Activity 
implements OnClickListener {
    //Called when activity is first created

    TextView tv1;
    ImageView iv;
    Button b;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView tv1 = (TextView) findViewById(R.id.txtV);  
        tv1.setDrawingCacheEnabled(true); 

        //To change to code 39 barCode font
        Typeface barcodefont = Typeface.createFromAsset(getAssets(),                 
                "fonts/IDAutomationHC39M_FREE.otf");         
        TextView tv = (TextView) findViewById(R.id.txtV);         
        tv.setTypeface(barcodefont);
    }

    public void onClick(View v) {

        tv1.buildDrawingCache(); 
        iv.setImageBitmap(tv1.getDrawingCache()); 
    } 

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

Logcat error:

10-04 06:33:25.076: E/AndroidRuntime(1423): FATAL EXCEPTION: main
10-04 06:33:25.076: E/AndroidRuntime(1423): java.lang.IllegalStateException: Could not find a method ConvertText(View) in the activity class com.example.txtvbitmapconverter.MainActivity for onClick handler on view class android.widget.Button with id 'btnConvert'
10-04 06:33:25.076: E/AndroidRuntime(1423):     at android.view.View$1.onClick(View.java:3578)
10-04 06:33:25.076: E/AndroidRuntime(1423):     at android.view.View.performClick(View.java:4084)
10-04 06:33:25.076: E/AndroidRuntime(1423):     at android.view.View$PerformClick.run(View.java:16966)
10-04 06:33:25.076: E/AndroidRuntime(1423):     at android.os.Handler.handleCallback(Handler.java:615)
10-04 06:33:25.076: E/AndroidRuntime(1423):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-04 06:33:25.076: E/AndroidRuntime(1423):     at android.os.Looper.loop(Looper.java:137)
10-04 06:33:25.076: E/AndroidRuntime(1423):     at android.app.ActivityThread.main(ActivityThread.java:4745)
10-04 06:33:25.076: E/AndroidRuntime(1423):     at java.lang.reflect.Method.invokeNative(Native Method)
10-04 06:33:25.076: E/AndroidRuntime(1423):     at java.lang.reflect.Method.invoke(Method.java:511)
10-04 06:33:25.076: E/AndroidRuntime(1423):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-04 06:33:25.076: E/AndroidRuntime(1423):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-04 06:33:25.076: E/AndroidRuntime(1423):     at dalvik.system.NativeStart.main(Native Method)
10-04 06:33:25.076: E/AndroidRuntime(1423): Caused by: java.lang.NoSuchMethodException: ConvertText [class android.view.View]
10-04 06:33:25.076: E/AndroidRuntime(1423):     at java.lang.Class.getConstructorOrMethod(Class.java:460)
10-04 06:33:25.076: E/AndroidRuntime(1423):     at java.lang.Class.getMethod(Class.java:915)
10-04 06:33:25.076: E/AndroidRuntime(1423):     at android.view.View$1.onClick(View.java:3571)
10-04 06:33:25.076: E/AndroidRuntime(1423):     ... 11 more
like image 786
Amy Avatar asked Oct 03 '12 06:10

Amy


1 Answers

Try this

Add to your onCreate()

tv1.setDrawingCacheEnabled(true);

Then in your onClick()

tv1.buildDrawingCache();
iv.setImageBitmap(tv1.getDrawingCache());

http://developer.android.com/reference/android/view/View.html#buildDrawingCache(boolean)

http://developer.android.com/reference/android/view/View.html#getDrawingCache(boolean)

[EDIT]

The problem you now have is that you are trying to enable the drawing cache before tv1 exists.

You should do this:

TextView tv1 = (TextView) findViewById(R.id.txtV); 
tv1.setDrawingCacheEnabled(true);
like image 196
Simon Avatar answered Oct 24 '22 10:10

Simon