10

I have an simple EditText inside an Activity and I want to create a Bitmap Image from that EditText and content of the EditText. The Image should look same as the EditText and its content.

Here is how I want the Image should look like, though this is an EditText but I want a Bitmap which should look same as this. So, how can I achieve this? Any idea would be great.

enter image description here

This is my code,

public class EditTextPinchZoomActivity extends Activity {

    EditText editText;
    ImageView imageView;

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

        imageView = (ImageView) findViewById(R.id.image_view);
        editText = (EditText) findViewById(R.id.edit_text);
        editText.buildDrawingCache();
    }

    public void myOnClick(View view) {

        switch (view.getId()) {
        case R.id.btn:
            imageView.setImageBitmap(editText.getDrawingCache());

            break;
        default:
            break;
        }
    }
}
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
  • This works!!! but with little bug. If more text exists in textbox then image height not resized. it cut text. Do you have any idea to solve this? – Manish Sapkal May 02 '17 at 12:24

2 Answers2

13

You can get the bitmap by building the drawing cache. This should work.

EditText edit = (EditText)findViewById(R.id.edit);
edit.buildDrawingCache();
ImageView img = (ImageView)findViewById(R.id.test);
img.setImageBitmap(edit.getDrawingCache());

Lalit when you try to build the cache in the onCreate method, the drawing hasn't happened yet so the drawingCache should have nothing. Either put the buildDrawingChache method in the onClick method. Or use the following code in onCreate.

ViewTreeObserver vto = editText.getViewTreeObserver(); 
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() {
        editText.buildDrawingCache();
        } 
});

But I think building the cache in the onClick method will be proper as the screen will be drawn by then. And other thing I remebered is, if you entering text after the onCreate, then you should build the cache after that. So try the first method.

ViewTreeObserver

Its a global layout listener to a view or viewgroup which keeps getting called when anyhing changes to the layout. I normally use this in cases where I need to get width and height's of views in the onCreate method. Without this the value will always be 0 as no drawing or measuring has taken place.

Suppose you had a requirement to keep updating your imageview of the edittext as you type, you can use this and just remove the first line where I remove the listener. The listener will be called for every character you type even when the cursor blinks.

blessanm86
  • 31,439
  • 14
  • 68
  • 79
  • whats wrong? I jsut tested this code out. Can you put your code up? Btw where are you trying to execute this code (onCreate)? – blessanm86 Oct 14 '11 at 06:36
  • I got the answer from keep edit.buildDrawingCache(); in the onClick but would like to know what is the use of ViewTreeObserver and what it does in this case. – Lalit Poptani Oct 14 '11 at 06:59
  • well removing the listener here is not giving the exact output. I want you you should remove editText.getViewTreeObserver().removeGlobalOnLayoutListener(this); line from the code so that future user's can get help. Thanks – Lalit Poptani Oct 14 '11 at 07:09
  • I tried both ways and its working fine, but would prefer the 1 way to implement. – Lalit Poptani Oct 14 '11 at 07:10
  • One more thing can we hide the cursor pointer of the editText when typing is finished? I know this is off topic to this question. But you can answer if you know Thanks. – Lalit Poptani Oct 14 '11 at 07:14
  • Well to hide the cursur you use the code edit.setCursorVisible(false); When do you use it will be upto you. – blessanm86 Oct 14 '11 at 07:17
  • Please remove this editText.getViewTreeObserver().removeGlobalOnLayoutListener(this); because by this line the content of the edittext is not shown in the image created. Thanks – Lalit Poptani Oct 14 '11 at 07:18
3

Use any ViewGroup to get bitmap:

FrameLayout frame = (FrameLayout) findByViewId(R.id.FrameLayout);  
frame.addView(your edittext object);//add on your layout design
frame.setDrawingCacheEnabled(true);
Bitmap bmp = frame.getDrawingCache();

Hope it will help.

Hare-Krishna
  • 1,425
  • 3
  • 16
  • 26
  • Thanks for answering but I got a simple and easy solution by blessenm, but your answer also is a nice one. +1 for that. – Lalit Poptani Oct 14 '11 at 06:57