Why onDraw is not called after invalidate()?
By default all ViewGroup sub-classes do not call their onDraw
method, you should enable it by calling setWillNotDraw(false)
link
Android Design: onDraw() not called after invalidate()
I figured it out by myself, maybe the solution will help anybody.
The problem was that I added the custom views to different layouts. I removed the two LinearLayouts and added my custom views directly to the FrameLayout and it works fine.
Why is the onDraw Not Being Called in Custom View for Android?
You can remove View_path = new Drawpath_view(MainActivity.this); from your code,
as you already attached view in xml.
Invalidate does not call onDraw
Add an id
<labs.example.function.Draw
android:id="@id/draw"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</labs.example.function.Draw>
Replace
final Draw draw = new Draw(this);
by
final Draw draw = (Draw) findViewById(R.id.draw); // need to refer to the view in xml.
If you use final Draw draw = new Draw(this);
you need to add that view to your view hierachy.
Custom View class' onDraw isn't called by Invalidate
invalidate()
does not cause onDraw()
to be called directly. It just tells the system that the view needs to be redrawn on the next drawing frame. Otherwise on the next drawing frame the system can use what was drawn previously since nothing has changed. In other words, your invalidate()
call and the eventual onDraw()
call are asynchronous.
Also, putting an infinite while
loop in your constructor will cause the UI thread to never execute code beyond that point, so the system will never even complete a single layout pass and your view will never be drawn even once. Needless to say, do not do this.
Invalidate not calling onDraw()
instead of
dv = new DrawView(this);
in your onCreate use:
dv = (DrawView)findViewById(R.id.drawView1);
your not using the drawview that is in the layout that you set to the content view
Related Topics
Performing Action After Fragment Transaction Animation Is Finished
How to Change the Android App Package Name When Assembling with Gradle
Setrotation(90) to Take Picture in Portrait Mode Does Not Work on Samsung Devices
Dialogfragment with Clear Background (Not Dimmed)
Android:Static Variable Null on Low Memory
How Does Bitmap Allocation Work on Oreo, and How to Investigate Their Memory
Android: Appwidget with Custom View Not Working
How to Customize List Preference Radio Button
Materialcomponents Theme Alert Dialog Buttons
Admob Shows Test Ads But Not Real Ads
Calling Startintentsenderforresult from Fragment (Android Billing V3)
Firebase Cloud Messaging - How to Validate Tokens
Error:Execution Failed for Task ':App:Transformclasseswithdexfordebug' in Android Studio
Using Gson with Proguard Enabled
How to Have a Gridview That Adapts Its Height When Items Are Added
What Is the Benefit of Using Fragments in Android, Rather Than Views