M.satti
M.satti

Reputation: 31

Take text from textview and paste it on an image from drawable

I want to share some text on Facebook. But I want that my text should have some frame behind it / or image .

How can I set text on image and then share it?

Upvotes: 1

Views: 75

Answers (3)

Nanites
Nanites

Reputation: 411

Well the right way to do it would be to make a custom control named MyImageTextView extends View and use onDraw method to draw image and text on it using canvas.

public class MyImageTextView extends View {
     String textOnImage;
     Bitmap bitmapBackground;

     public MyImageTextView(Context context) {
         super(context);
         init();
     }

     public MyImageTextView(Context context, AttributeSet attrs) {
         super(context, attrs);
         init();
     }


     public MyImageTextView(Context context, AttributeSet attrs, int defStyleAttr)
     {
         super(context, attrs, defStyleAttr);
         init();
     }

     @Override
     protected void onDraw(Canvas canvas) {
         // TODO Auto-generated method stub
         super.onDraw(canvas);

         int centerx = getWidth() / 2;
         int centery = getHeight() / 2;

         canvas.drawBitmap(bitmapBackground, 0, 0, null);
         drawText(canvas, centerx , centery , textOnImage)
     }

     public void drawText(Canvas canvas, float x, float y, String text) {
         int consumedCalTextSize =      getResources().getDimensionPixelSize(R.dimen.food_circular_graph_text_size);
        Paint canvasTextPaint = new Paint();
        canvasTextPaint.setAntiAlias(true);
        canvasTextPaint.setARGB(255, 255, 255, 255);
        canvasTextPaint.setTextSize(consumedCalTextSize);
        canvasTextPaint.setTextAlign(Paint.Align.CENTER);
        canvas.drawText(text, x, y, canvasTextPaint);
    }
}

You can have full access to modify this custom control and also you can add it in your XML. Android controls are really powerful that way.

Upvotes: 1

Ankit
Ankit

Reputation: 101

Create one layout and set the required background image, and have a textview inside that layout to set your text.

Then inside your java code get this and covert it to bitmap.

FrameLayout view = (FrameLayout)findViewById(R.id.framelayout);

view.setDrawingCacheEnabled(true);

view.buildDrawingCache();

Bitmap bm = view.getDrawingCache();

Then u can share this bitmap to facebook

Upvotes: 1

2D3D
2D3D

Reputation: 353

Use the text view and set background to it

android:background="@drawable/myResouce"

Or from within your code :

  mTextView.setBackgroundResource(R.drawable.myResouce);

Upvotes: 1

Related Questions