locked
How to display an ImageSpan in a SpannableString from Vector Drawable RRS feed

  • Question

  • User366895 posted

    Im trying to create a spannable text that contains an ImageSpan, but the vector drawables has infinite size so we have to scale it some how, i have try this code with no luck, in some forums they say it should work:

    ``` var icon = AppCompatResources.GetDrawable(Activity, Resource.Drawable.ic_info); icon.SetBounds(0, 0, 20, 20); DrawableCompat.SetTint(icon, Color.White); var textView = layout.FindViewById(Resource.Id.FeaturesContentTextView); var imageSpan = new ImageSpan(icon, SpanAlign.Baseline); //Find your drawable.

            var spannableString = new SpannableString(textView.Text); //Set text of SpannableString from TextView
            spannableString.SetSpan(imageSpan, textView.Text.Length - 1, textView.Text.Length, SpanTypes.InclusiveInclusive); 
            textView.TextFormatted = spannableString;
    

    ```

    Now this code display the icon with the maximum width and height allowed from the parent layout I also try to convert the vector drawable to bitmap with this code with no luck (i dont get any icon)

    ``` public static Bitmap GetBitmapFromVectorDrawable(Context context, int drawableId) { Drawable drawable = AppCompatResources.GetDrawable(context, drawableId);

             Bitmap bitmap = Bitmap.CreateBitmap(drawable.IntrinsicWidth,
                     drawable.IntrinsicHeight, Bitmap.Config.Argb8888);
             Canvas canvas = new Canvas(bitmap);
             drawable.SetBounds(0, 0, canvas.Width, canvas.Height);
             drawable.Draw(canvas);
    
             return bitmap;
         }
    

    Bitmap bitmap = GetBitmapFromVectorDrawable(Activity, Resource.Drawable.ic_info); Drawable d = new BitmapDrawable(Resources, Bitmap.CreateScaledBitmap(bitmap, 80, 80, true)); ```

    Sunday, August 30, 2020 2:01 PM

Answers

  • User369978 posted

    @Exoskeletor said: In the layout.xml, the element you use to add the icon, is it a simple TextView?

    i used this

    ```

      <style name="CommonAlertDialogTextStyle" parent="@android:style/TextAppearance.Small">
      <item name="android:textColor">@android:color/white</item>
      </style>
    

    ```

    Yes , i test with a simple TextView . Could you post a basic project here so that i can test it ,just zip and upload in comment.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, September 7, 2020 9:36 AM

All replies

  • User369978 posted

    Could you provide the Vector image so that i can test on my side ?

    Monday, August 31, 2020 9:30 AM
  • User366895 posted

    i'm using this image: ```

    ```

    Monday, August 31, 2020 10:08 AM
  • User369978 posted

    I test with your code but it seems no problem on vector size ...

    Thursday, September 3, 2020 9:34 AM
  • User366895 posted

    With only setbounds you have make the icon small? What did you use? I end up using png icon which with the same code was displayed correctly

    Thursday, September 3, 2020 9:49 AM
  • User369978 posted

    @Exoskeletor said: With only setbounds you have make the icon small? What did you use? I end up using png icon which with the same code was displayed correctly

    Yes, only set bounds it display correctly .

    icon.SetBounds(0, 0, 50, 50);
    
    Friday, September 4, 2020 1:23 AM
  • User366895 posted

    In the layout.xml, the element you use to add the icon, is it a simple TextView?

    i used this

    ```

    <style name="CommonAlertDialogTextStyle" parent="@android:style/TextAppearance.Small">
        <item name="android:textColor">@android:color/white</item>
    </style>
    

    ```

    Friday, September 4, 2020 3:44 PM
  • User369978 posted

    @Exoskeletor said: In the layout.xml, the element you use to add the icon, is it a simple TextView?

    i used this

    ```

      <style name="CommonAlertDialogTextStyle" parent="@android:style/TextAppearance.Small">
      <item name="android:textColor">@android:color/white</item>
      </style>
    

    ```

    Yes , i test with a simple TextView . Could you post a basic project here so that i can test it ,just zip and upload in comment.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, September 7, 2020 9:36 AM
  • User366895 posted

    ok i will I have also another issue now, the first time the icons appear the DrawableCompat.SetTint(icon, Color.White); seems to do nothing but if you close the alertdialog and open it again they appear white. do we know why this is happening? i try to also set tint after the dialog.show but i get the same result

    this is my code:

    ``` var layout = LayoutInflater.Inflate(Resource.Layout.AlertDialogUserGuide, null); var icon = AppCompatResources.GetDrawable(Activity, Resource.Drawable.ic_info); icon.SetBounds(0, 0, 20, 20); DrawableCompat.SetTint(icon, Color.White);

            var textView = layout.FindViewById<TextView>(Resource.Id.FeaturesContentTextView);
            var imageSpan = new ImageSpan(icon, SpanAlign.Baseline); //Find your drawable.
    
            var spannableString = new SpannableString(textView.Text); //Set text of SpannableString from TextView
            spannableString.SetSpan(imageSpan, textView.Text.Length - 1, textView.Text.Length, SpanTypes.InclusiveExclusive); //Add image at end of string
            textView.TextFormatted = spannableString;
    
            AlertDialog.Builder alert = new AlertDialog.Builder(Activity, Resource.Style.CommonAlertDialog);
            alert.SetTitle(GetString(Resource.String.Info_UserGuide));
            alert.SetIcon(icon);
    
            alert.SetView(layout);
            alert.SetPositiveButton(GetString(Android.Resource.String.Ok), (s, ex) => { });
            alert.Show();
    
            DrawableCompat.SetTint(icon, Color.White);
    

    ```

    Monday, September 7, 2020 12:43 PM
  • User366895 posted

    Here is an example project that does not display the vector icon at all at my tests. Also sometimes with the same code vector drawables was able to set the tink programmatically but for me it works very often (i check on emulator android 9 and real device samsung galaxy s10e android 10)

    ``` Microsoft Visual Studio Community 2019 Version 16.7.2 VisualStudio.16.Release/16.7.2+30413.136 Microsoft .NET Framework Version 4.8.04084

    Installed Version: Community

    ADL Tools Service Provider 1.0 This package contains services used by Data Lake tools

    ASA Service Provider 1.0

    ASP.NET and Web Tools 2019 16.7.532.28833 ASP.NET and Web Tools 2019

    ASP.NET Core Razor Language Services 16.1.0.2035807+72d099b977d3a85e65fa3b0614ca8cfc803fef02 Provides languages services for ASP.NET Core Razor.

    ASP.NET Web Frameworks and Tools 2019 16.7.532.28833 For additional information, visit https://www.asp.net/

    Azure App Service Tools v3.0.0 16.7.532.28833 Azure App Service Tools v3.0.0

    Azure Data Lake Node 1.0 This package contains the Data Lake integration nodes for Server Explorer.

    Azure Data Lake Tools for Visual Studio 2.4.6000.1 Microsoft Azure Data Lake Tools for Visual Studio

    Azure Functions and Web Jobs Tools 16.7.532.28833 Azure Functions and Web Jobs Tools

    Azure Stream Analytics Tools for Visual Studio 2.4.6000.1 Microsoft Azure Stream Analytics Tools for Visual Studio

    C# Tools 3.7.0-6.20412.3+d3c3a44a4e7ad31cc75c59be0d3df4a19ff33878 C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

    Common Azure Tools 1.10 Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

    Extensibility Message Bus 1.2.6 (master@34d6af2) Provides common messaging-based MEF services for loosely coupled Visual Studio extension components communication and integration.

    IntelliCode Extension 1.0 IntelliCode Visual Studio Extension Detailed Info

    Microsoft Azure HDInsight Azure Node 2.4.6000.1 HDInsight Node under Azure Node

    Microsoft Azure Hive Query Language Service 2.4.6000.1 Language service for Hive query

    Microsoft Azure Stream Analytics Language Service 2.4.6000.1 Language service for Azure Stream Analytics

    Microsoft Azure Stream Analytics Node 1.0 Azure Stream Analytics Node under Azure Node

    Microsoft Azure Tools 2.9 Microsoft Azure Tools for Microsoft Visual Studio 2019 - v2.9.30701.1

    Microsoft Continuous Delivery Tools for Visual Studio 0.4 Simplifying the configuration of Azure DevOps pipelines from within the Visual Studio IDE.

    Microsoft JVM Debugger 1.0 Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines

    Microsoft Library Manager 2.1.79+ge3567815aa.RR Install client-side libraries easily to any web project

    Microsoft MI-Based Debugger 1.0 Provides support for connecting Visual Studio to MI compatible debuggers

    Microsoft Visual Studio Tools for Containers 1.1 Develop, run, validate your ASP.NET Core applications in the target environment. F5 your application directly into a container with debugging, or CTRL + F5 to edit & refresh your app without having to rebuild the container.

    Mono Debugging for Visual Studio 16.7.5 (112c7bc) Support for debugging Mono processes with Visual Studio.

    NuGet Package Manager 5.7.0 NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/

    Project System Tools 1.0 Tools for working with C#, VisualBasic, and F# projects.

    ProjectServicesPackage Extension 1.0 ProjectServicesPackage Visual Studio Extension Detailed Info

    SQL Server Data Tools 16.0.62008.03130 Microsoft SQL Server Data Tools

    ToolWindowHostedEditor 1.0 Hosting json editor into a tool window

    TypeScript Tools 16.0.20702.2001 TypeScript Tools for Microsoft Visual Studio

    Visual Basic Tools 3.7.0-6.20412.3+d3c3a44a4e7ad31cc75c59be0d3df4a19ff33878 Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

    Visual F# Tools 10.10.0.0 for F# 4.7 16.7.0-beta.20361.3+3ef6f0b514198c0bfa6c2c09fefe41a740b024d5 Microsoft Visual F# Tools 10.10.0.0 for F# 4.7

    Visual Studio Code Debug Adapter Host Package 1.0 Interop layer for hosting Visual Studio Code debug adapters in Visual Studio

    Visual Studio Container Tools Extensions (Preview) 1.0 View, manage, and diagnose containers within Visual Studio.

    Visual Studio Tools for Containers 1.0 Visual Studio Tools for Containers

    VisualStudio.DeviceLog 1.0 Information about my package

    VisualStudio.Foo 1.0 Information about my package

    VisualStudio.Mac 1.0 Mac Extension for Visual Studio

    Xamarin 16.7.000.440 (d16-7@358f3c6) Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

    Xamarin Designer 16.7.0.495 (remotes/origin/d16-7@79c0c522c) Visual Studio extension to enable Xamarin Designer tools in Visual Studio.

    Xamarin Templates 16.7.85 (1bcbbdf) Templates for building iOS, Android, and Windows apps with Xamarin and Xamarin.Forms.

    Xamarin.Android SDK 11.0.2.0 (d16-7/025fde9) Xamarin.Android Reference Assemblies and MSBuild support. Mono: 83105ba Java.Interop: xamarin/java.interop/d16-7@1f3388a ProGuard: Guardsquare/proguard/proguard6.2.2@ebe9000 SQLite: xamarin/sqlite/3.32.1@1a3276b Xamarin.Android Tools: xamarin/xamarin-android-tools/d16-7@017078f

    Xamarin.iOS and Xamarin.Mac SDK 13.20.2.2 (817b6f72a) Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support. ```

    Monday, September 7, 2020 3:55 PM