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 7840363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:45:26+00:00 2026-06-02T15:45:26+00:00

I have created a custom view to draw a line on the screen. This

  • 0

I have created a custom view to draw a line on the screen. This view is included in a fragment’s xml layout and retrieved like the following in the fragment’s onCreateView method:

MyCustomView mMyCustomView;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate view
    View v = inflater.inflate(R.layout.fragment, container, false);

    mMyCustomView = (MyCustomView) v.findViewById(R.id.my_custom_view);
    ...
}

When I pass the mMyCustomView variable to a custom listener in the fragment’s onCreateView method and call something like mMyCustomView.drawLine in the listener class everything works fine.

When I call mMyCustomView.drawLine in the fragment’s onResume() method, however, nothing happens, though it is the same variable and method.

The only reason I could think of is that the listener calls the method when the user interacts with the fragment, which is even later than the onResume() is called, as far as the lifecycle is concerned. However, within the fragment I cannot call the method any later than in onResume() AFAIK.

EDIT 1:

This is what my custom view looks like:

public class ConnectionLinesView extends View {
// Class variables
Paint mPaint = new Paint(); // Paint to apply to lines
ArrayList<float[]> mLines = new ArrayList<float[]>(); // Array to store the lines


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

    mPaint.setColor(Color.BLACK);
    mPaint.setStrokeWidth(2);
}


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

    mPaint.setColor(Color.BLACK);
    mPaint.setStrokeWidth(2);
}


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

    mPaint.setColor(Color.BLACK);
    mPaint.setStrokeWidth(2);
}


protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    // If lines were already added, draw them
    if(!mLines.isEmpty()){
        for(float[] line : mLines){
            canvas.drawLine(line[0], line[1], line[2], line[3], mPaint);
        }
    }

}


// Method to add a line to the view
public void addLine(View v1, View v2) {
    float[] line = new float[4];
    line[0] = v1.getX() + v1.getWidth()/2;
    line[1] = v1.getY() + v1.getHeight()/2;
    line[2] = v2.getX() + v2.getWidth()/2;
    line[3] = v2.getY() + v2.getHeight()/2;

    mLines.add(line);
    this.invalidate();
}


public void removeLines() {
    mLines.clear();
    this.invalidate();
}

}

When I call addLine(…) in onResume(), the line is not drawn, even though the inside of the for loop in the onDraw() method is reached. When I add another line later in the listener class (which responds to some user interaction), the both lines are drawn on the canvas. Somehow the canvas.drawLine() doesn’t work in the onResume() of the view’s parent fragment.

EDIT 2:

I have added a handler, that calls the custom view’s invalidate method repeatedly after the fragment has been added to the layout of the parent activity. The line still doesn’t get drawn!

  • 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-02T15:45:28+00:00Added an answer on June 2, 2026 at 3:45 pm

    In the end I solved the problem by creating a handler, which calls the addLine method after 50 milliseconds after the fragment has been added. A pretty dirty solution, but it works…

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

Sidebar

Related Questions

I have created a custom view by extending Relative Layout and it looks like
friends, i have created custom title bar using following titlebar.xml file with code <?xml
I have created a custom view named MyDraw ,this is my MyDraw code, public
I have a layer-hosting view set up like this in a custom NSView subclass:
I have created a custom list view, each row has it's own custom selector,
I have created a custom annotation view. In the setSelected method i have implemented
I have created a Dynamic View Panel custom control and want to add a
I have a custom tableview cell created programmatically that consists of: a background view
I have a custom view called DrawView created in the main activity. I have
I have created a custom alertdialog by the following code: AlertDialog.Builder builder; AlertDialog alertDialog;

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.