android - how to draw text with background color using canvas

Android - how to draw text with background color using canvas

To draw text with a background color using a Canvas in Android, you can use the drawText() method for the text and drawRect() for the background. Here's how you can do it:

Example Code

  1. Custom View Class: Create a custom view by extending View.

    import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.util.AttributeSet; import android.view.View; public class CustomTextView extends View { private Paint textPaint; private Paint backgroundPaint; private String text = "Hello, World!"; private Rect textBounds; public CustomTextView(Context context) { super(context); init(); } public CustomTextView(Context context, AttributeSet attrs) { super(context, attrs); init(); } private void init() { textPaint = new Paint(); textPaint.setColor(Color.WHITE); // Text color textPaint.setTextSize(50); // Text size textPaint.setAntiAlias(true); // Smooth edges backgroundPaint = new Paint(); backgroundPaint.setColor(Color.BLUE); // Background color textBounds = new Rect(); textPaint.getTextBounds(text, 0, text.length(), textBounds); // Calculate text bounds } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // Draw background rectangle canvas.drawRect( 0, 0, textBounds.width() + 20, // Add padding textBounds.height() + 20, // Add padding backgroundPaint ); // Draw the text canvas.drawText(text, 10, textBounds.height() + 10, textPaint); // Draw text with padding } } 
  2. Using the Custom View in Layout: Add your custom view to your XML layout file.

    <your.package.name.CustomTextView android:layout_width="wrap_content" android:layout_height="wrap_content" /> 

Explanation

  • Paint: Two Paint objects are created: one for the text and another for the background.
  • Rect: A Rect is used to calculate the bounds of the text, which helps to size the background rectangle appropriately.
  • onDraw: In the onDraw method, first, the background rectangle is drawn, followed by the text.

Summary

This approach allows you to draw text with a background color on a Canvas. You can adjust the colors, sizes, and positions according to your needs.

Examples

  1. Android Canvas: Draw text with background color

    • Description: Drawing text on a Canvas with a background color using Paint.
    • Code:
      Paint paint = new Paint(); paint.setColor(Color.BLACK); // Text color paint.setTextSize(30); // Text size Rect rect = new Rect(100, 100, 300, 200); // Background rectangle paint.setStyle(Paint.Style.FILL); paint.setColor(Color.YELLOW); // Background color canvas.drawRect(rect, paint); paint.setColor(Color.BLACK); // Reset text color canvas.drawText("Hello, World!", 120, 170, paint); // Draw text 
  2. Android Canvas: Draw text with rounded background

    • Description: Drawing text with a rounded background using Canvas and Paint.
    • Code:
      Paint paint = new Paint(); paint.setColor(Color.WHITE); // Text color paint.setTextSize(30); // Text size RectF rectF = new RectF(100, 100, 300, 200); // Background rounded rectangle paint.setStyle(Paint.Style.FILL); paint.setColor(Color.BLUE); // Background color canvas.drawRoundRect(rectF, 10, 10, paint); paint.setColor(Color.WHITE); // Reset text color canvas.drawText("Hello, World!", 120, 170, paint); // Draw text 
  3. Android Canvas: Draw text with background gradient

    • Description: Drawing text with a gradient background using Canvas and Paint.
    • Code:
      Paint paint = new Paint(); paint.setColor(Color.WHITE); // Text color paint.setTextSize(30); // Text size LinearGradient gradient = new LinearGradient(0, 0, 0, 200, Color.RED, Color.YELLOW, Shader.TileMode.CLAMP); paint.setShader(gradient); Rect rect = new Rect(100, 100, 400, 200); // Background rectangle paint.setStyle(Paint.Style.FILL); canvas.drawRect(rect, paint); paint.setColor(Color.WHITE); // Reset text color canvas.drawText("Hello, World!", 120, 170, paint); // Draw text 
  4. Android Canvas: Draw text with background using Path

    • Description: Drawing text with a background shape using Path and Canvas.
    • Code:
      Paint paint = new Paint(); paint.setColor(Color.BLACK); // Text color paint.setTextSize(30); // Text size Path path = new Path(); path.addCircle(200, 150, 100, Path.Direction.CW); // Circle background paint.setStyle(Paint.Style.FILL); paint.setColor(Color.YELLOW); // Background color canvas.drawPath(path, paint); paint.setColor(Color.BLACK); // Reset text color canvas.drawText("Hello, World!", 150, 170, paint); // Draw text 
  5. Android Canvas: Draw text with transparent background

    • Description: Drawing text with a transparent background using Canvas and Paint.
    • Code:
      Paint paint = new Paint(); paint.setColor(Color.WHITE); // Text color paint.setTextSize(30); // Text size Rect rect = new Rect(100, 100, 300, 200); // Background rectangle paint.setStyle(Paint.Style.FILL); paint.setColor(Color.argb(128, 255, 0, 0)); // Transparent red background canvas.drawRect(rect, paint); paint.setColor(Color.WHITE); // Reset text color canvas.drawText("Hello, World!", 120, 170, paint); // Draw text 
  6. Android Canvas: Draw text with background using PorterDuff

    • Description: Drawing text with a background using PorterDuff mode in Paint.
    • Code:
      Paint paint = new Paint(); paint.setColor(Color.WHITE); // Text color paint.setTextSize(30); // Text size Rect rect = new Rect(100, 100, 300, 200); // Background rectangle paint.setStyle(Paint.Style.FILL); paint.setColor(Color.BLUE); // Background color canvas.drawRect(rect, paint); paint.setColor(Color.WHITE); // Reset text color paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); canvas.drawText("Hello, World!", 120, 170, paint); // Draw text 
  7. Android Canvas: Draw text with background using Path and LinearGradient

    • Description: Drawing text with a gradient background using Path and LinearGradient.
    • Code:
      Paint paint = new Paint(); paint.setColor(Color.WHITE); // Text color paint.setTextSize(30); // Text size Path path = new Path(); path.addRect(100, 100, 300, 200, Path.Direction.CW); // Rectangle background paint.setStyle(Paint.Style.FILL); int[] colors = {Color.RED, Color.YELLOW}; float[] positions = {0, 1}; LinearGradient gradient = new LinearGradient(0, 0, 0, 200, colors, positions, Shader.TileMode.CLAMP); paint.setShader(gradient); canvas.drawPath(path, paint); paint.setColor(Color.WHITE); // Reset text color canvas.drawText("Hello, World!", 120, 170, paint); // Draw text 
  8. Android Canvas: Draw text with rounded background using Path

    • Description: Drawing text with a rounded background shape using Path and Canvas.
    • Code:
      Paint paint = new Paint(); paint.setColor(Color.WHITE); // Text color paint.setTextSize(30); // Text size Path path = new Path(); RectF rectF = new RectF(100, 100, 300, 200); // Background rounded rectangle path.addRoundRect(rectF, 20, 20, Path.Direction.CW); paint.setStyle(Paint.Style.FILL); paint.setColor(Color.GREEN); // Background color canvas.drawPath(path, paint); paint.setColor(Color.WHITE); // Reset text color canvas.drawText("Hello, World!", 120, 170, paint); // Draw text 
  9. Android Canvas: Draw text with background using RadialGradient

    • Description: Drawing text with a radial gradient background using Canvas and RadialGradient.
    • Code:
      Paint paint = new Paint(); paint.setColor(Color.WHITE); // Text color paint.setTextSize(30); // Text size RadialGradient gradient = new RadialGradient(200, 150, 100, new int[]{Color.RED, Color.YELLOW}, new float[]{0, 1}, Shader.TileMode.CLAMP); paint.setShader(gradient); Rect rect = new Rect(100, 100, 400, 200); // Background rectangle paint.setStyle(Paint.Style.FILL); canvas.drawRect(rect, paint); paint.setColor(Color.WHITE); // Reset text color canvas.drawText("Hello, World!", 120, 170, paint); // Draw text 
  10. Android Canvas: Draw text with background using LinearGradient

    • Description: Drawing text with a linear gradient background using Canvas and LinearGradient.
    • Code:
      Paint paint = new Paint(); paint.setColor(Color.WHITE); // Text color paint.setTextSize(30); // Text size LinearGradient gradient = new LinearGradient(0, 0, 0, 200, new int[]{Color.RED, Color.YELLOW}, new float[]{0, 1}, Shader.TileMode.CLAMP); paint.setShader(gradient); Rect rect = new Rect(100, 100, 400, 200); // Background rectangle paint.setStyle(Paint.Style.FILL); canvas.drawRect(rect, paint); paint.setColor(Color.WHITE); // Reset text color canvas.drawText("Hello, World!", 120, 170, paint); // Draw text 

More Tags

mbstring ntfs mule-esb automata pydicom webpack-loader lm microsoft-cdn dyld git-merge-conflict

More Programming Questions

More General chemistry Calculators

More Genetics Calculators

More Statistics Calculators

More Electronics Circuits Calculators