androidtextviewandroid-bitmapaddtextchangedlistener

textview.getDrawingCache() not working properly


In my project I have a EditText a TextView and an Imageview. I display in the Textview and in the Imageview what the user entered in the EditText. this is the code:

public class MainActivity extends AppCompatActivity{
    private TextView textView;
    private EditText editText;
    private ImageView imageView;

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


textView= (TextView) findViewById(R.id.flip);
imageView= (ImageView) findViewById(R.id.iv_text_to_image);
editText= (EditText) findViewById(R.id.original);

textView.setText("Hello World");
textView.setDrawingCacheEnabled(true);
textView.buildDrawingCache();

imageView.setImageBitmap(textView.getDrawingCache());



editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //s = "."+s;
                textView.setText(s);
                imageView.setImageBitmap(textView.getDrawingCache());
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
}
}

The code sometimes it works but not perfectly, for example if I uncomment the s = "."+s; it works but do not show in the ImageView the last character entered. If I remove that line the results are unexpected they very depends on what your type. If instead of s = "."+s; I change it to s = "a"+s; or another letter like b or c etc the results are again unspected.

What do you think it could be happening with the .setImageBitmap() and .getDrawingCache()?

By the way at the beginning isn't showing the "Hello world" in the imageView


Solution

  • I fixed it doing this:

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
                        textView.setText(s);
                    }
    
     @Override
     public void afterTextChanged(Editable s) {
    
                        textView.setDrawingCacheEnabled(true);
                        imageView.setDrawingCacheEnabled(true);
                        // this is the important code :)
                        // Without it the view will have a dimension of 0,0 and the bitmap will be null
                        textView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
                        textView.layout(0, 0, textView.getMeasuredWidth(), textView.getMeasuredHeight());
                        Bitmap bm2 = textView.getDrawingCache();
    
                        if (bm2!=null){
                            imageView.setImageBitmap(bm);
                        }else{
                            imageView.setImageBitmap(textView.getDrawingCache());
                        }
    
                    }
    

    I got the idea from here Android View.getDrawingCache returns null, only null