Иван
Иван

Reputation: 434

How to override method of internally used class?

There is class KeyboardView in Android that uses internally class Canvas, and specifically method Canvas.DrawText. That method can draw only one line. I need it to draw two line.

It is possible in Java somehow to override Canvas.DrawText, so that KeyboardView will use overwritten method?

Is there any method to achieve requried behavior without full rewrite of KeyboardView from scratch?

Upvotes: 1

Views: 341

Answers (3)

Yogu
Yogu

Reputation: 9455

You would have to override the method of KeyboardView that instanciates the Canvas class to make it creating an instance of your class.

In the KeyboardView class, only the method onBufferDraw uses the canvas, and it instanciates it, too. Poorly, this method is very large.

Upvotes: 1

BRPocock
BRPocock

Reputation: 13934

If there is actually another Canvas instance inside of KeyboardView, you'd have to extend the KeyboardView class directly. (public class MyFancyKeyboardView extends KeyboardView)

However, it looks like it has an implementation of the View.onDraw(Canvas) method that's called by the framework with an external Canvas provided. If you could wrap it inside of a View of your own, you might be able to provide your own onDraw that called the one for the KeyboardView.

     private KeyboardView wrappedKeyboardView;
     public void onDraw (Canvas canvas) {
         wrappedKeyboardView.onDraw (canvas);
         doMoreDrawing (canvas);
     }

(Keep in mind, of course, that every Android device is liable to have an entirely different keyboard layout, so I'm suspicious about drawing over it being particularly effective or portable…)

Upvotes: 1

Dave Newton
Dave Newton

Reputation: 160321

If the class doesn't provide a way to specify a Canvas subclass, or if it doesn't provide a behavioral extension point, then no, however: A full rewrite is not likely to be necessary, if you're aiming at specific functionality.

Instead, subclass the view, and re-implement only the functionality you need changed, and use your view subclass.

Upvotes: 1

Related Questions