Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8993635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:14:07+00:00 2026-06-15T23:14:07+00:00

I dynamically add a bitmap on the draw of a custom component. Now when

  • 0

I dynamically add a bitmap on the draw of a custom component.

Now when it comes to this line :

    line = Bitmap.createBitmap(line, 0, 0, line.getWidth(), 1);

I’d like to stretch the bitmap to the width of the component. Here I use line.getWidth() but I wish there was a way to say this.getWidth. Unfortunately it’s in the constructor and the view still doesn’t know its width.

Here is the code of the custom component :

package com.adylitica.components;

import com.adylitica.activity.R;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;

/**
 * Defines a custom EditText View that draws lines between each line of text that is displayed.
 */
public class EditTextNotes extends EditText {

private Rect mRect;
private Bitmap line;
public static int nbLines = 0;
private SharedPreferences settings;


public EditTextNotes(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    line = BitmapFactory.decodeResource(context.getResources(), R.drawable.line_thin_new);
    line = Bitmap.createBitmap(line, 0, 0, line.getWidth(), 1);

    mRect = new Rect();
}

public EditTextNotes(Context context, AttributeSet attrs) {
    super(context, attrs);

    line = BitmapFactory.decodeResource(context.getResources(), R.drawable.line_thin_new);
    line = Bitmap.createBitmap(line, 0, 0, line.getWidth(), 1);

    mRect = new Rect();

}

public EditTextNotes(Context context) {
    super(context);

    line = BitmapFactory.decodeResource(context.getResources(), R.drawable.line_thin_new);
    line = Bitmap.createBitmap(line, 0, 0, line.getWidth(), 1);

    mRect = new Rect();
}


@Override
protected void onDraw(Canvas canvas) {

    int count = getLineCount();

    Rect r = mRect;

    int baseline = 0;
    int addSize = 0;
    canvas.drawBitmap(line, 0, baseline, null);

    for (int i = 0; i < count; i++) {
        baseline = getLineBounds(i, r) + addSize;
        canvas.drawBitmap(line, 0, baseline, null);
        nbLines++;
    }

    super.onDraw(canvas);
}

}

PS: the bitmap is 9patched.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:14:08+00:00Added an answer on June 15, 2026 at 11:14 pm

    You should not be doing your measuring in the constructor. In a custom view, it is best to override onSizeChanged(). It will be called when your view is added to the view hierarchy with the current width and height, and again with current and old values if your view is ever resized. See [Handle Layout Events] for details.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to dynamically add links to my pages something like this: foreach (Node
I want to dynamically add some widgets like so: LinearLayout llay = new LinearLayout(this);
I want to add a bitmap to a TMenuItem created dynamically. With this code
I need to dynamically add a script reference, so I do this: jQuery('html head').append(<script
I had this to dynamically add items in html <td id=campus_list> <%= link_to_function image_tag(new.png,
I'm trying to dynamically add a css stylesheet rule using javascript, something like example
I'm trying to dynamically add results to this display and I simply want to
Dynamically add textbox using jquery from the link above i want something like that,
Summary: Trying to dynamically add heading rows to a ListView via a custom adapter
I would like to dynamically add properties to a ExpandoObject at runtime. So for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.