Xamarin - working with VectorDrawable in Imageview and Edittext - android

I face issue to apply vectordrawable at my imageView and Edittext
here is my tries to fix the issue
First I am using Xamarin.Android.Support.Vector.Drawable Version 23.4.0
Second I add AppCompatDelegate.CompatVectorFromResourcesEnabled = true; in onCreate of my Application
public class MyApplication : Application
{
public MyApplication(IntPtr handle, JniHandleOwnership ownerShip) : base(handle, ownerShip)
{
Log.Debug("App", "I am in oncreate application");
}
public override void OnCreate()
{
base.OnCreate();
AppCompatDelegate.CompatVectorFromResourcesEnabled = true;
}
}
*Third * usingAppCompatActivity to make sure my activity used AppCompatImageView instead of normal ImageView and try set drawable manually but it return with null (I dont know why this happen )
[Activity( Label = "Activity1", Theme = "#style/AppTheme")]
public class Activity1 : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.test);
ImageView testImage = FindViewById<ImageView>(Resource.Id.imageViewTest);
Drawable drawable = VectorDrawableCompat.Create(Resources, Resource.Drawable.ic_arrow_back_white_24dp, null);
testImage.SetImageDrawable(drawable);
}
}
Fourth in my project .csproj I add build tools version 23.0.3
<AndroidSdkBuildToolsVersion>23.0.3</AndroidSdkBuildToolsVersion>
here is my full file content
?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<AndroidSdkBuildToolsVersion>23.0.3</AndroidSdkBuildToolsVersion>
<AndroidDesignerPreferredDevice>Nexus 4</AndroidDesignerPreferredDevice>
<SelectedDevice>Nexus_5_API_21_x86</SelectedDevice>
<AndroidDesignerPreferredTheme>Theme.DeviceDefault</AndroidDesignerPreferredTheme>
</PropertyGroup>
</Project>
Fifth I reference my vector image app:srcCompat
but it still not working any one could help me fix this issue

I fix first half of my issue Simplly update to
Xamarin.Android.Support.Vector.Drawable Version 23.4.0.1
make sure you reference your vector images by this way app:srcCompat
related to EditText Image I think you cant set it from xml you need to use it via code here is the method
SetCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom);
in my case I used leftdrawable so my method look like
SetCompoundDrawablesWithIntrinsicBounds(Resource.Drawable.MyDrawable, 0, 0, 0);
hope this help any one face same issue

Update: Forget the Old Solution. Just update to the latest Verison of Xamarin (Xamarin.Android 7.2.0.7) and the latest version of the support libs (25.3.1).
Old Solution:
Downgrade to Xamarin 4.2.0.680, use only the support libs version 23.3.0 (Latest stable 24.2.1 crashes on Android 4.4.4 but works on Android 6)
Also ensure this:
Put this static constructor in your Activity:
static Activity1()
{
AppCompatDelegate.CompatVectorFromResourcesEnabled = true;
}
Or add AppCompatDelegate.CompatVectorFromResourcesEnabled = true; to your Application class´ OnCreate method.
<AndroidSdkBuildToolsVersion> in your project shouldn't be nessessary anymore.
Use app:srcCompat, like you already do.
Have a look at the Dependencies of each support lib at Nuget
I have:
Xamarin.Android.Support.Animated.Vector.Drawable.dll 23.3.0
Xamarin.Android.Support.Design.dll 23.3.0
Xamarin.Android.Support.v14.Preference.dll 23.3.0
Xamarin.Android.Support.v4.dll 23.3.0
Xamarin.Android.Support.v7.AppCompat.dll 23.3.0
Xamarin.Android.Support.v7.Preference.dll 23.3.0
Xamarin.Android.Support.v7.RecyclerView.dll 23.3.0
Xamarin.Android.Support.Vector.Drawable.dll 23.3.0
Also make shure you reference Java.Interop.dll
Try out another vector drawable, for example from Material Design Icons (Click on an icon, chose Icon Package, .XML Vector Drawable).

Related

SVG on Android below API 23 [duplicate]

I am working on an Android project and I chose <vector> to display icon because it is adaptable and dynamically, however, I just can run this app on devices running Android, which have API 21 or higher. My question is how can I use <vector> on lower Android version i.e. API 14 or kind of. Thanks!
<!-- drawable/ic_android_debug_bridge.xml -->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="48dp"
android:width="48dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path android:fillColor="#color/primaryColorDark"
android:pathData="M15,9A1,1 0 0,1 14,8A1,1 0 0,1 15,7A1,1 0 0,1 16,8A1,1 `0 0,1 15,9M9,9A1,1 0 0,1 8,8A1,1 0 0,1 9,7A1,1 0 0,1 10,8A1,1 0 0,1 9,9M16.12,4.37L18.22,2.27L17.4,1.44L15.09,3.75C14.16,3.28 13.11,3 12,3C10.88,3 9.84,3.28 8.91,3.75L6.6,1.44L5.78,2.27L7.88,4.37C6.14,5.64 5,7.68 5,10V11H19V10C19,7.68 17.86,5.64 16.12,4.37M5,16C5,19.86 8.13,23 12,23A7,7 0 0,0 19,16V12H5V16Z" /></vector>
With the support library 23.2, the true support for Vector Drawables has been provided all the way down to API v7.
It is recommended to disable the previous version of the support, which rendered PNG during build-time, by adding
// Gradle Plugin 2.0+
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
to the build.gradle file.
The implementation is fairly simple. Just use the new srcCompat attribute on Drawables (under app namespace!):
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/ic_add" /> // <= this is new
Vector Drawables are also supported in cases like TextView's drawableLeft property.
Source: library announcement
However, I would still recommend something like Iconics library, AndroidSVG, or another font-icon or SVG solution for the full SVG-standand vector support.
VectorDrawable are supported pre-Lollipop via the Support Library, but the way to use them depends on the version of Support Library you have. And it may not work in all cases.
I've made this diagram to help (valid for Support Library 23.4.0 to - at least - 25.1.0).
I found solution! For those who search solution with TextView and other "android" namespace attributes.
First of all this is necessary:
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
And in application class define this:
#Override
public void onCreate() {
super.onCreate();
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
Now you can use app:srcCompat="#drawable/ic_add" but if you try to use android:background= or android:drawableLeft= it will crash app with "Error inflating" exception.
We can create wrapped drawable ic_add_wrapped.xml for this vector:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_add"/>
</layer-list>
And now it will work with any property like drawableLeft or background. Just set android:drawableLeft="#drawable/ic_add_wrapped.xml".
WARNING! THIS IS A WORKAROUND SOLUTION. So you use it for your own risk.
Vector Drawables are now backward compatible, it's just a matter of upgrading your gradle version to 1.4.0-beta3 or higher, and upgrade your IDE :
We are also excited to offer backwards compatibility for your vector
assets in Android Studio 1.4. Once you have a vectorDrawable image in
your res/drawable, the Gradle plugin will automatically generate
raster PNG images for API level 20 and below during build time. This
means you only need to update and maintain your vector asset for your
app project and Android Studio can take care of image conversion
process.
http://android-developers.blogspot.com.uy/2015/09/android-studio-14.html
you need use android Support Repository 30+ if you using android studio
and need android support library 23.2.1+ if using Eclipse.
check your build.gradle (project) if using version 2.0+ add below code in your build.gradle (app)
// Gradle Plugin 2.0+
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
and: if using version 1.5 add below in your build.gradle(app)
// Gradle Plugin 1.5
android {
defaultConfig {
generatedDensities = []
}
// This is handled for you by the 2.0+ Gradle Plugin
aaptOptions {
additionalParameters "--no-version-vectors"
}
}
here is sample code for use vector icon:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/ic_add"
tools:ignore="MissingPrefix" />
or
<ImageButton
android:layout_width="wrap_content"
android:background="#android:color/transparent"
app:srcCompat="#drawable/camera"
tools:ignore="MissingPrefix"
android:layout_height="wrap_content"/>
Vector Drawables are also supported in cases like TextView's drawableLeft property. but it worked api 22+ to me and i still dont know how it will work for low api.
Also keep in mind if you want to be compatible below API 21:
you cannot use the android:background property in xml or View.setBackgroundResource() function. You need to use the View.setBackground().
you cannot use the svg-s in StateListDrawable xml-s or other xml drawables, you have to generate them programmatically.
you cannot use svg-s in case of notifications.
When you need to add VectorDrawable (created from SVG) programatically, you can do it like this:
icon = VectorDrawableCompat.create(resources, R.drawable.ic_map_black_24dp, null)
For lower version compatible,
add the below in gradle,
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
add the below code in onCreate() in your application class,
#Override
public void onCreate() {
super.onCreate();
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
In xml for imageView,
<ImageView
android:id="#+id/imageViewMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/transparent"
app:srcCompat="#drawable/ic_success"/>
If you want to change the image source programmatically use this,
imageView.setImageResource(R.drawable.ic_launcher);
I had an issue where my vector images would show up but would be completely black, this was due to an issue where you can't reference color resources in the vector xml file.
So instead of #color/primaryColorDark you have to use the actual color e.g. #212121
Just to make #2Dee's answer complete:
VectorDrawable cannot be used on API 20 and lower. The official method generates pngs, which then become BitmapDrawables, which breaks the idea of vector graphics.
Personally I prefer svg's over xml vectors:
such graphics can be exported right from Illustrator or other popular software
svg supports transformations, text, masks and other things properly
true scallable vector graphics on all platforms
smaller size and faster builds
To use vector graphics you can try https://github.com/BigBadaboom/androidsvg . It's an svg reader and svg-compatible ImageView.
If you are using Android Studio 3.0.0 you can set
android.enableAapt2=false
in gradle.properties
https://www.reddit.com/r/androiddev/comments/6mj8di/android_studio_30_canary_6_released/
You can use programatically ..to set drawableLeft to your editText or textView
like
Drawable tick_drawable = VectorDrawableCompat.create(getResources(), R.drawable.green_tick, null);
if (tick_drawable != null) {
tick_drawable.setBounds(0, 0, tick_drawable.getIntrinsicWidth(),tick_drawable.getIntrinsicHeight());
}
And to drawable left like this..
editText.setCompoundDrawables( tick_drawable , null, null, null );
after setting vectorDrawables.useSupportLibrary = true in gradle default and AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); in activity on create
To avoid crash with android:drawableleft in Textview, set drawble left to the textview programitically
for examlple:
textview.setCompoundDrawablesWithIntrinsicBounds(R.drawable.movie, 0, 0, 0);
I also found the same issue. And I did:
vectorDrawables.useSupportLibrary = true
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
textview.setCompoundDrawablesWithIntrinsicBounds(R.drawable.movie, 0, 0, 0);
It is good. But after that, I got an error that the resource not found.
So I found it's better to add a vector image according to the SDK version.
Finally, this solution became good is for me:-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
textview.setCompoundDrawablesWithIntrinsicBounds(null, null, AppCompatResources.getDrawable(this, R.drawable.movie), null);
} else {
textview.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.movie, 0);
}
I hope, it will help someone.

Set a values folder for multiple android sdk versions

I would like to set a values folder for multiple sdk verions to set a theme for everything under sdk 19. I have tried doing:
protected void onCreate(Bundle savedInstanceState) {
if (Build.VERSION.SDK_INT < 19) {
getApplication().setTheme(R.style.HomeTheme4);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_launch_screen);
[.....]
}
But this doesnt seem to set the theme For the whole app.
Thank you in advance.
you could place your theme that you want to use for API-19 and above inside the folder:
/values-v19
and the default values folder then will handle everything below API-19:
/values
see related android resource qualifiers documentation.

how to set background xml file in drawable for a view?

how can I set an xml background file that placed in drawable for a view without using #SuppressLint("NewApi") ?
for example I created a drawable xml file for my textview
when I call TV.setBackground(getResources().getDrawable(R.drawable.tv_pic_back)); eclipse automatically add #SuppressLint("NewApi") at the first of my function.
how can I use that without #SuppressLint("NewApi") ?
I have a class where I put a lot of code to handle the different APIs, so that you use one line of code for one API, and another line of code for another API.
public static void setBackgroundDrawable(View view, Drawable drawable) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
view.setBackground(drawable);
}
else {
view.setBackgroundDrawable(drawable);
}
}
This will still give you a warning because setBackgroundDrawable is deprecated, but if you instead would use setBackground(drawable) for all versions then your application would crash on API levels lower than Jelly Bean (API 16).
However, in your case all you need to do is actually setBackgroundResource(R.drawable.tv_pic_back); because you don't need to get the drawable from the resource id yourself, Android will do that for you if you give it your resource id when you call the right method.
The Android developer reference will tell you which methods are deprecated and which methods are implemented in which API version.

Action Bar Sherlock has deprecated methods

I downloaded the ActionBarSherlock version 4.1.0.0, and added a the Android Project library into Eclipse 4.2 on my Win7 java 1.6 update 24 machine. I used an Eclipse Project Build Target of Android 4.1 in Project | Properties | Project Build Target. I want to incorporate the ActionBar functionality into an existing app that has minSdkVersion="7". I noted the code from the ActionBarSherlock library appears to have some deprecated methods, and an error:
Example 1: ActionBarContainer.java, ActionBarContextView.java, ScrollingTabContainerView.java - uses setBackgroundDrawable - The method setBackgroundDrawable(Drawable) from the type View is deprecated
public ActionBarContainer(Context context, AttributeSet attrs) {
super(context, attrs);
setBackgroundDrawable(null);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.SherlockActionBar);
mBackground = a.getDrawable(R.styleable.SherlockActionBar_background);
mStackedBackground = a.getDrawable(
R.styleable.SherlockActionBar_backgroundStacked);
if (getId() == R.id.abs__split_action_bar) {
mIsSplit = true;
mSplitBackground = a.getDrawable(
R.styleable.SherlockActionBar_backgroundSplit);
}
a.recycle();
setWillNotDraw(mIsSplit ? mSplitBackground == null :
mBackground == null && mStackedBackground == null);
}
Quick fix shows as Add #SupressWarnings 'deprecation' for ActionBarContainer
Example 2:
IcsProgressBar.java uses animationResolution which shows as deprecated, same quick fix as above
private static final int[] ProgressBar = new int[] {
...
android.R.attr.animationResolution
Also, I have an error in ActivityChooserView.java:
private static class SetActivated {
public static void invoke(View view, boolean activated) {
view.setActivated(activated);
}
}
Error is on SetActivated - Call requires API 11 (current min is 7). This makes sense based on the manifest:
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="15"/>
Shouldn't API 7 be fine, since Action Bar Sherlock should work at at Andriod 2.x? Have others experienced this, and if so, what is the recommended action? Supress/ignore the deprecation? What about the error on SetActivated? I reviewed the readme and did some net searches but did not come up with anything on this. Thanks for any suggestions.
Thanks!
It is just fine. The code is 2.x compatible and even some methods are deprecated as of 4.x, they are still there. It also do not mean on 4.x these deprecated methods will be used. The source is 2.x-4.x so there's no other way (not to mention reflection, but that'd hurt performance, and is for now not necessary). So it is safe to just ignore this. It would probably be better to turn depreciation off for certain files but it is not there. So do not worry.
This is doesn't really answer the question but I ran into the same issue when I had to have a minSdkVersion 9 and I wanted to have ActionBarSherlock but I was getting the same error of the actionBar requires minSdkVersion 11.
Apparently in ADT 21.1 you make a values-v11/styles.xml and put the actionBar in there.
https://code.google.com/p/android/issues/detail?id=48283

Android: Any idea why cannot load drawable object?

There are several custom-made graphic objects (.png files) included in the project inside res/drawable map.
All elements are normally loaded and displayed in the user interface except two icons and so far haven't figured out what causes the problem.
The code which doesn't affect the user interface as it should is the following:
if (settings.isMute()) {
muteIcon.setIconImage(R.drawable.ic_volume_off_small);
} else {
muteIcon.setIconImage(R.drawable.ic_volume_small);
}
In both cases there is only ic_volume_small displayed on the screen and the Variables window in the IDE displays the following:
R.drawable.ic_volume_small = Class not
loaded : net.client.android.R$drawable
R.drawable.ic_volume_of_small = Class
not loaded :
net.client.android.R$drawable
The method (member of IconImage class) which should change the icon image is the following:
public void setIconImage(int imageFromResources) {
iconImage = BitmapFactory.decodeResource(getResources(), imageFromResources);
iconWidth = iconImage.getWidth();
iconHeight = iconImage.getHeight();
invalidate();
}
Does anyone know what could cause the described problem?
Thanks!
Assuming what you want is the drawable from the Android framework and not your own, you'd want to use:
android.R.drawable.ic_volumne_off_small
rather than
R.drawable.ic_volume_off_small
(notice the android prefix).
If you recently added file, please refresh (press F5) the drawable directory and R is going to be generated again. Once R is regenerated, you can use the newly added resource.

Categories

Resources