We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Using a SfChart in an Android widget

This is a straightforward query: is it possible to have a SfChart embedded in an Android widget? I ask because when I include a SfChart, the widget is unable to load. And if it is possible, are there any specific steps that need to be taken in order to allow for it?



26 Replies

DD Devakumar Dhanapoosanam Syncfusion Team April 17, 2019 03:39 AM UTC

Hi Bardi, 
 
Greetings from Syncfusion. We are analyzing the feasibility of adding the SfChart in the android widget and will update you the status on April 17th, 2019. 
 
Thanks, 
Devakumar 



BK Bharathiraja K Syncfusion Team April 17, 2019 12:04 PM UTC

Hi Bardi, 
  
The AppWidget Layout is a one of the RemoteViews, which does not support every kind of layout or views. It had some restriction. We cannot add the Custom views in the AppWidget layout.  
  
  
We are trying to find any other feasible solution for this, we will update you the status in two business days 23rd April 2019. 
  
Regards, 
Bharathi. 



BK Bharathiraja K Syncfusion Team April 23, 2019 12:52 PM UTC

Hi Bardi, 
 
There is no direct approach to add custom layout as android widget 
 
Reference link: 
 
Maybe we can try to add custom layout as image by below link.  
 
Regards, 
Bharathi. 



BG Bardi Golriz replied to Bharathiraja K October 2, 2020 09:49 AM UTC

Hi Bardi, 
 
There is no direct approach to add custom layout as android widget 
 
Reference link: 
 
Maybe we can try to add custom layout as image by below link.  
 
Regards, 
Bharathi. 


Hi there,

Bringing this back up again.

I'm following the advice provided to create a bitmap of the chart and display that instead. However, this doesn't seem to work. I hope you are able to figure a workaround, as perhaps there is something I may have missed in my implementation.

So, I create an object that will instantiate a SfChart within, available as a field "chart":

                    var data = new Hour_Range(Application.Context);
                    Bitmap bitmap = getBitmapFromView(data.chart);
                    widgetView.SetImageViewBitmap(Resource.Id.chart, bitmap);

The getBitmapFromView function gets passed in a view and returns it as a bitmap (from https://stackoverflow.com/questions/52642055/view-getdrawingcache-is-deprecated-in-android-api-28 – the link you shared last consisted of a deprecated method to achieve the same, which didn't work either):

        public Bitmap getBitmapFromView(View view)
        {
            Bitmap bitmap = Bitmap.CreateBitmap(view.Width, view.Height, Bitmap.Config.Argb8888);
            Canvas canvas = new Canvas(bitmap);
            view.Draw(canvas);
            return bitmap;
        }
This however runs into an exception:

{Java.Lang.IllegalArgumentException: width and height must be > 0
  at Java.Interop.JniEnvironment+StaticMethods.CallStaticObjectMethod (Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x0006e] in <…}
Debugging clarifies that the width and height of data.chart SfChart is still 0 at the time the bitmap is trying to be created. 

I've tried explicitly setting the width and height:

                    data.chart.Measure(400, 400);
                    data.chart.Layout(0, 0, 400,400);

As well as:

Bitmap bitmap = Bitmap.CreateBitmap(400,400, Bitmap.Config.Argb8888);

Although this means no exception occurs, nothing gets drawn.

To be clear, I know the chart itself has been setup fine because it's using the same code that creates charts in-app which display absolutely fine.

Any ideas?


BG Bardi Golriz October 2, 2020 10:30 AM UTC

Okay, I have a positive update but there are still issues.

If I do the following:

chart.Measure(View.MeasureSpec.MakeMeasureSpec(400, MeasureSpecMode.Exactly), View.MeasureSpec.MakeMeasureSpec(400, MeasureSpecMode.Exactly));
chart.Layout(0, 0, 400,400);

And then:

Bitmap bitmap = Bitmap.CreateBitmap(400,400, Bitmap.Config.Argb8888);
Canvas canvas = new Canvas(bitmap);
chart.Draw(canvas);

This does show the chart on the widget! However, it's not displaying the chart annotations (which are essential).

This is what it looks like:



It should look like this, which it does in-app, running the same exact code (ignore the values being different):



See attached for screenshots

The annotation for reference is as follows:

ViewAnnotation annotation = new ViewAnnotation()
{
    X1 = x,
    Y1 = y,
    VerticalAlignment = ChartAnnotationAlignment.Center,
};

// icon
AppCompatImageView iconImage = new AppCompatImageView(context);
iconImage.SetImageResource(Convertor_Icon_Droid.parseXS(icon));

// value
TextView textView = new TextView(context);
textView.Text = value;
Helper_Text.SetFont_ChartValue(textView);
textView.TextSize = 12;
textView.TextAlignment = Android.Views.TextAlignment.Center;
textView.Gravity = Android.Views.GravityFlags.CenterHorizontal;
textView.SetPadding(0, 8, 0, 0);
textView.SetTextColor(Convertor_Colour_Droid.convert(Helper_Colour_Droid.GetText()));


// container
LinearLayout linearLayout = new LinearLayout(context);
linearLayout.Orientation = Orientation.Vertical;
linearLayout.SetPadding(5,5,5,5);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent);
linearLayout.SetBackgroundColor(background); 
layoutParams.SetMargins(0, 0, 0, 0);
linearLayout.LayoutParameters = layoutParams;

linearLayout.AddView(iconImage);
linearLayout.AddView(textView);

annotation.View = linearLayout
chart.Annotations.Add(annotation)

Attachment: Archive_abdc68fc.zip


SM Saravanan Madheswaran Syncfusion Team October 5, 2020 11:47 AM UTC

Hi Bardi, 
 
Based on the provided code snippet, we understand that you are converted the chart as a bitmap to export it into a widget. Then we are suspecting it will be resolved by creating a bitmap from the chart drawing cache.    
 
. . . 
chart.DrawingCacheEnabled = true; 
. . . 
Android.Graphics.Bitmap bitmap = null; 
using (bitmap = chart.DrawingCache) 
{ 
. . . 
try 
{ 
    //Update bitmap to widget 
} 
} 
finally 
{ 
    chart.DrawingCacheEnabled = false; 
} 
. . . 
} 
 
 
Note: Due to facing some unhandled exceptions, we were unable to check this solution in sample. 
 
Please check this and let us know with more details of either issue reproducing sample or else more details in case of misunderstanding.  
 
Regards, 
Saravanan.  



BG Bardi Golriz October 22, 2020 10:21 AM UTC

I've tried a couple of things but no luck unfortunately.

Firstly:

    var chartBuilder = new Chart_Range_Hour_Droid(Application.Context, range); // sets up builder
    chartBuilder._chart.DrawingCacheEnabled = true; 
    chartBuilder.build(); // builds chart i.e. axis and line w/ annotation
    chart._chart.Measure(View.MeasureSpec.MakeMeasureSpec(400, MeasureSpecMode.Exactly), View.MeasureSpec.MakeMeasureSpec(400, MeasureSpecMode.Exactly));
    chart._chart.Layout(0, 0, 400, 400);
    Android.Graphics.Bitmap bitmap = null;

    using (bitmap = chart._chart.DrawingCache)
    { 
        try
        {
            widgetView.SetImageViewBitmap(Resource.Id.chart, bitmap);
        }
        finally
        {
            chart._chart.DrawingCacheEnabled = false;
        }
    }

This actually doesn't draw anything. Perhaps I misunderstood the instructions?

Also:

    using (bitmap = chart._chart.DrawingCache)
    { 
        try
        {
            bitmap = Helper_Widget.getBitmapFromView(chart._chart);
            widgetView.SetImageViewBitmap(Resource.Id.chart, bitmap);
        }
        finally
        {
            chart._chart.DrawingCacheEnabled = false;
        }
    }

Draws the chart just like before i.e. the line and x-axis labels, but not the annotations.

For reference, this is the chart control I'm updating the bitmap of:

<!-- Chart -->
<LinearLayout
android:id="@+id/data"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
android:layout_below="@+id/summary">
<ImageView
   android:id="@+id/chart"
   android:layout_width="match_parent"
   android:layout_height="match_parent"/>
</LinearLayout>


SM Saravanan Madheswaran Syncfusion Team October 23, 2020 04:09 PM UTC

Hi Bardi,    
  
We have validated provided code snippet and we feel it will be better to share your application/mock project to test at our end with the provided suggestion to achieve the solution in better and possible ways at earlier. 
 
Regards, 
Saravanan. 



BG Bardi Golriz October 23, 2020 04:11 PM UTC

Okay, I will do that next week. Thanks for testing, and your continued support.


BG Bardi Golriz October 24, 2020 08:15 PM UTC

See attached for a sample project. No matter what I try the DrawingCache seems to be null. I'm perhaps missing something obvious! Although the chart is rough, it is visible when you load the app i.e. there's nothing wrong with the chart itself, but a problem with getting it to draw on the widget.

You'll need to look at Widget.cs and Widget.axml.

Thanks for your support!

Attachment: playground_d20c748d.zip


SM Saravanan Madheswaran Syncfusion Team October 26, 2020 12:26 PM UTC

Hi Bardi,  
  
Thanks for providing sample, we will validate and update you the complete status on or before 28th October 2020. We will appreciate your patience until then.   
  
Regards,  
Saravanan.   



SM Saravanan Madheswaran Syncfusion Team October 28, 2020 11:21 AM UTC

Hi Bardi, 
 
Thanks for your patience.  
  
We would like to let you know that without rendering the chart in UI, we were unable to properly convert that as a bitmap. Hence achieved it by already rendered chart instance and convert as a bitmap as per in below  
   
Please check the code snippet and sample below. we hope this approach will resolve your issue.    
 
Android.Graphics.Bitmap bitmap = null
 
MainActivity._chart.DrawingCacheEnabled = true
 
using (bitmap = MainActivity._chart.DrawingCache
    try 
   
        //bitmap = getBitmapFromView(_chart); 
        widgetView.SetImageViewBitmap(playground.Droid.Resource.Id.chart, bitmap); 
   
. . . 
 
 
 
Regards, 
saravanan.


BG Bardi Golriz October 28, 2020 02:07 PM UTC

Thanks for getting back.

So, my first reaction (and after a quick test) is that this seems to work if the app was running in the background (i.e. MainActivity was setup previously in the foreground, and so there's a chart that can be referenced), but how do you deal in a scenario when the app is not suspended in the background (i.e. there's no MainActivity that can be referenced)?


YP Yuvaraj Palanisamy Syncfusion Team October 29, 2020 03:57 PM UTC

Hi Bardi, 
 
Currently we are trying to achieve the possible workaround for your requirement and we will update you the complete details on or before 30th October 2020. 
  
Regards, 
Yuvaraj


BG Bardi Golriz November 2, 2020 12:08 PM UTC

Hello. Any updates now that it's passed your target date of October 30 (or earlier)? Thanks!



YP Yuvaraj Palanisamy Syncfusion Team November 2, 2020 04:13 PM UTC

Hi Bardi, 

Thanks for your patience. 

As per your requirement we have prepared the sample for without rendering the chart in UI create a chart as an image source. Please find the sample from the below link. 

  
Note: In the sample choose export tap and click generate snapshot to show the chart as an image. 

Please let us know if you have any concern. 

Regards, 
Yuvaraj 



BG Bardi Golriz November 2, 2020 06:01 PM UTC

Thanks so much for the sample. I'll need to spend more time with this (it's a .Forms project rather .Android but I think I should be able to work around that) before confirming whether it does the job. That said, my first impression/question is if you can confirm whether I basically need for my widget to run ExecuteGenerateSnapshot and use what it returns as the image to display?


YP Yuvaraj Palanisamy Syncfusion Team November 4, 2020 04:40 PM UTC

Hi Bardi, 
 
We have checked with Android widget application for without rendering the chart in UI and create chart as an image image and we are facing the problem for converting the chart to image source. Hence, we will check with this and try to achieve the possibility, will update you the complete details on or before 6th November 2020. 
  
Regards, 
Yuvaraj 



BG Bardi Golriz November 5, 2020 11:35 AM UTC

Thank you so much for the continued investigative work and support!


YP Yuvaraj Palanisamy Syncfusion Team November 8, 2020 06:16 PM UTC

Hi Bardi, 

Thanks for your patience. 

We are checked with multiple scenario to achieve your requirement “To render chart image as in android widget without render the chart in UI” and we are not able to get the image view in the Android Widget. Also, we have tried with the following link  


Regards,  
Yuvaraj 



BG Bardi Golriz November 11, 2020 06:02 PM UTC

Sorry, I'm confused. Are you saying this isn't going to be possible to achieve? If not, any plans to make changes to enable this to be supported? Widgets are a big deal, and I expect them to become an even bigger deal on Android again after iOS 14.


YP Yuvaraj Palanisamy Syncfusion Team November 12, 2020 01:27 PM UTC

Hi Bardi,  
  
We really regret and as we said earlier, the Android widget is not supported to have a custom view in it. As per your request, we tried all the possibilities to achieve it through a supportable view as a bitmap image. But itself having difficulties like without rendering into UI, unable to achieve it.  For this time taken is only to provide your request in a better way and satisfy you.  
  
Hence, we have considered this as a feature request, and it has been tracked through below feedback link  
  
  
We will prioritize the features every release based on the demands and we do not have an immediate plan to implement this since we committed with already planned work. So, this feature will be available for any of our upcoming releases   
          
Please cast your vote to make it counts and If you have any more specifications/suggestions to the feature request, you can add it as a comment in the portal.   
 
Regards, 
Yuvaraj. 



BG Bardi Golriz November 13, 2020 01:26 PM UTC

That's disappointing. Thank you for turning it into a feature though, one that will hopefully get added sooner rather than later, because as I said widgets are a big deal and surely you'd want your great work to be displayed on users' home screens :)

Best,
Bardi


YP Yuvaraj Palanisamy Syncfusion Team November 16, 2020 10:48 AM UTC

Hi Bardi,  
  
We apologies for that. Surely, we will implement this feature in any of upcoming release. Please keep track the feedback link  
  
   
Regards, 
Yuvaraj. 



LE Lefteris February 18, 2022 11:01 PM UTC

Hi there
Are there any updates on this?



YP Yuvaraj Palanisamy Syncfusion Team February 21, 2022 02:24 PM UTC

Hi Lefteris, 
 
We do not have an immediate plan to implement this feature. Since, we have committed with already planned work and will prioritize the features every release based on the demands. So, this feature will be available for any of our upcoming releases. 
 
Please cast your vote to make it count and If you have any more specifications/suggestions to the feature request, you can add it as a comment in the feedback portal. 
 
Thanks, 
Yuvaraj. 


Loader.
Live Chat Icon For mobile
Up arrow icon