Guys! Who faced this? How can I implement drawing on the canvas on the widget, I apologize for the pun. The task of how to draw a line on the widget, I tried to do it in the old-fashioned way, created a view and drew it on it, but I can’t insert it into the widget, I need another way.

UPD :

I found one blog, which describes how to draw on the widget, but there is not all the code, and it is not clear where to write it. I wrote in the class successor AppWidgetProvider , in the method onUpdate() , but nothing has changed, there is no picture ...

UPD2:

I looked for similar questions on windowskerker and came across one and remade it for myself, it also does not work, maybe someone can fix me. Here is how I saddle:

 public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { RemoteViews views = new RemoteViews(context.getApplicationContext().getPackageName(), R.layout.main); Paint paint = new Paint(); paint.setStyle(Paint.Style.FILL); paint.setColor(Color.RED); paint.setTextSize(16); paint.setAntiAlias(true); paint.setTypeface(Typeface.MONOSPACE); Bitmap mybitmap = Bitmap.createBitmap(100, 16, Bitmap.Config.ALPHA_8); Canvas c = new Canvas(mybitmap); c.drawColor(Color.BLACK); c.drawText("fdgfdgfdgfdfdfdgGFDFGFDDDDG", 0, 0, paint); views.setImageViewBitmap(R.id.imageView1, mybitmap); } 

What needs to be fixed?

    1 answer 1

     new View(context) { @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // ваши манипуляции с канвасом } } 

    And don't forget to override the onMeasure method.

    • I understand this to write to the onUpdate () method? Override the method in what way? onMeasure () {super.onMeasure (); }? - dajver 7:26 pm
    • How then can I make this twist draw? In Maine.xml how to register? or where? - dajver
    • one
      So you redefined the onDraw method or not? - angry
    • so redefine onMeasure () or go onDraw ()? And what do you mean by override? To inherit a class of view? and pass it to the widget class using it? - dajver
    • 2
      Yes, the class must be inherited from View. In your case, this view will most likely be AppWidgetHostView, but you need to determine for yourself which class you want to inherit from. Redefine both onDraw and onMeasure. You asked where to insert the drawing code, I told you - in the onDraw method. But at the same time, onMeasure also needs to be redefined in order for the view to expose itself to the correct dimensions. - angry