Android DataBinding - Why Data Binding not support #mipmap images - android

Using DataBinding, I trying to access icon from mipmap folder, after write a code it not compile model class and it show error in log - "token recognition error". Look at the below screenshot even it not allowed #mipmap. If anyone know the reason Please let me know
<ImageView
android:id="#+id/img_page1"
android:layout_width="#dimen/splash_slider_circle"
android:layout_height="#dimen/splash_slider_circle"
android:src="#{slider.img1 ? #mipmap/ic_circle_filled : #mipmap/ic_circle_outline}" />

The exact reason for the described behaviour is unknown to me. But if it's inconvenient to move all your icons from mipmap to drawable folders, it's possible to import your generated R class in the layout and refer mipmaps in conditions starting with R.mipmap and using app:imageResource attribute:
<data>
<import type="your.package.R" />
...
</data>
<ImageView
...
app:imageResource="#{someConditionVariable ? R.mipmap.ic_for_true_condition : R.mipmap.ic_for_false_condition}"
... />

Related

Android Studio Template package name

I tried make android studio template like activity template by this instruction
Current package set in globals.xml.ftl
<global id="srcOut" value="${srcDir}/${slashedPackageName(packageName)}" />
and create template file
<instantiate from="src/app_package/LifecycleFragment.java.ftl"
to="${escapeXmlAttribute(srcOut)}/${className}.java" />
It force my template file to src/main/java/myPackageName
But my current package is kotlin
How can i create template in current selected folder?
Sorry if I'm late with my answer, but maybe it will be useful for someone.
To change the source package from java to kotlin try add this line to globals.xml.ftl:
<global id="kotlinMainSourceSet" value="${srcOut?replace('java','kotlin') />
Use it in your receipt.xml.ftl as following:
! Replace SomeClass.kt.ftl with your class in app_package;
! Replace your_needed_path with path, in which you would like to put the file
! Replace someClassName with the id of the className from template.
<instantiate
from="src/app_package/SomeClass.kt.ftl"
to="${escapeXmlAttribute(kotlinMainSourceSet)}/${your_needed_path}/${someClassName}.kt" />
In addition I'll answer on the #AlexeiKorshun question with which I've faced too: applicationIdSuffix is added, while creating a directory. This can be easily fixed by overriding srcOut in your globals.kt.ftl with following:
<global id="srcOut" value="${srcDir}/${slashedPackageName(packageName?replace('.dev|.qa2|.stage', '', 'r'))}" />
.dev/.qa2/.stage are added as an example,please replace them with yours.

Xamarin:Unable to convert instance of type Android.Widget.LinearLayout to type Android.Support.V7.Widget.Toolbar

The app is running on Android 6 or below. But when running on Android 7 (Nougat) it throws runtime exception.
System.InvalidCastException: Unable to convert instance of type 'Android.Widget.LinearLayout' to type 'Android.Support.V7.Widget.Toolbar'.
The error is thrown in Xamarin (Visual studio 2017) at below code:
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
ToolbarResource = Resource.Layout.toolbar;
TabLayoutResource = Resource.Layout.tabs;
base.OnCreate(bundle); //Runtime error here
.... Other code ...
}
}
I've taken latest updates for All Nuget packages but not able to identify the issue. For testing I'm using Moto G4+ (Android 7.0).
Can anybody help please?
Edit 1:
Layout of toolbar is as : (Under resources > layout > toolbar.axml). This was already there when App version with Android 6 released. But Now visited app back for Android 7 issues.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
I solved this by just cleaning the entire solution and closing the emulator. Then deploy again and it should work.
I tried SeƱor Hernandez's answer without much success, then realized I had accidentally deleted Resources/Resource.designer.cs from my Android project. I had meant to delete it from disk only, expecting it to get regenerated on rebuild. It did get regenerated, but that did not add it back to the project. Doing so (right-click, include in project) solved the issue on my part.
It got solved by downloading android_m2repository_r29.zip repository from https://developer.xamarin.com/guides/android/troubleshooting/resolving-library-installation-errors/ with instruction given in same.
I also tested the issue by creating a new fresh solution and got some style "Resource not found error". That was also fixed and then the original problem also got fixed.
You get this kind of error when trying to assign a control (a UI element in your xml) on a variable that is not of the same type. I get this error frequently when I make a mess and try to assign an EditText to a TextView variable or vice versa, i. e
EditText myText = FindViewById<TextView>(Resource.Id.myText);
or
TextView myText = FindViewById<EditText>(Resource.Id.myText);
I'm pretty sure that in your case you are trying to assign an Android.Widget.LinearLayout to a variable of type Android.Support.V7.Widget.Toolbar.
I think in your case one of this variables have been defined as LinearLayout in your xml:
Resource.Layout.toolbar
Resource.Layout.tabs
Meanwhile, one of this variables have beeen defined as Toolbar in your C# code, so te cast is not possible:
ToolbarResource
TabLayoutResource
If the problem persist, you can also try to check the type Android.Support.V7.Widget.Toolbar, instead it could be Android.Widget.Toolbar in your xml or code.

Glide not loading real image and stuck with placeholder

I have a pretty basic load image from server line code:
Glide.with(view.getContext()).load(url).placeholder(R.drawable.default_profile).into(view);
For some reason, I'm always stuck with the placeholder being displayed and never the real image!
I have made sure that a valid and working url is being passed. And, if I use the same code without the placeholder it works fine
Glide.with(view.getContext()).load(url).into(view);
Any ideas why?
Try to add .dontAnimate()
It caused by TransitionDrawable too and it seems so because after scroll there's no animation because it's cached.
The correct code is
Glide.with(view.getContext()).load(url).placeholder(R.drawable.default_profile).dontAnimate().into(view);
I hope it will be helpful for you.
Check if you have added Internet permission in the manifest:
<uses-permission android:name="android.permission.INTERNET"/>
Glide does not fire exception if there is no Internet connectivity.
Add android:usesCleartextTraffic="true" in the application tag in manifest and check the internet permission is mentioned or not in the same manifest file:-
Add this permission below to access resource endpoint/url
<uses-permission android:name="android.permission.INTERNET"/>
If your target endpoint/url only has http add this code below in your manifest.xml. Starting with Android 9 (API level 28), cleartext support is disabled by default.
android:usesCleartextTraffic="true"
Because of that, if you get resource from your unencrypted HTTP API don't forget to add
res/xml/network_security_config.xml
So the code will be like these
network_security_config.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">urweb.id</domain>
<domain includeSubdomains="true">localhost</domain>
...
<domain includeSubdomains="true">111.222.333.444</domain>
</domain-config>
</network-security-config>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
<uses-permission android:name="android.permission.INTERNET"/>
<application
...
android:usesCleartextTraffic="true"
...>
<activity>
...
</activity>
</application>
</manifest>
Use ProgressBar as loading gif
Glide.with(context).
load(url)
.listener(new RequestListener<String, GlideDrawable>() {
#Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
progressBar.setVisibility(View.GONE);
return false;
}
#Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
progressBar.setVisibility(View.GONE);
return false;
}
})
.crossFade(1000)
.into(imageView);
If someone comes across this in the future you may need to add
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Unsure of the exact reason for this change, nonetheless my code didn't work without that permission, and now does with it.
Below is a weird fix that worked for me.
The following always fails for me (12 trials) - I never see the real image:
Glide.with(context)
.load(url)
.asBitmap()
.into(new SimpleTarget<Bitmap>(iconWidth, iconHeight) { ... }
The following always succeeds for me (12 trials) - I always see the real image:
Glide.with(context)
.load(url)
.asBitmap()
.listener(new RequestListener() {
public boolean onException(Exception e,Object o,Target t,boolean b)
{return false;}
public boolean onResourceReady(Object o,Object p,Target t,boolean b,boolean c)
{return false;}})
.into(new SimpleTarget<Bitmap>(iconWidth, iconHeight) { ... }
As you can see, the only difference here is that I added a listener. Makes little sense, but these trials were done pretty carefully, so I'm going with it.
None of the solutions above worked for me. The issue may be at the placeholder you're using. If the view you're using to load the image has a placeholder, it causes some issues. Removing that placeholder for some reason seems to fix it.
So, if the (Image)View you're trying to inflate has a placeholder, such as android:src="#mipmap/placeholder", remove it.
<ImageView
android:id="#+id/imgGallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#mipmap/rugova1" />
like this
<ImageView
android:id="#+id/imgGallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
/>
It worked for me.
If you are tried following steps :
INTERNET permission is already added both in Manifest and Programmatically
usesCleartextTraffic : if already mentioned in Manifest
Even though image is not loading in ImageView
Then try this :
add this line application tag of Manifest
android:usesCleartextTraffic="true"
add following code in the activity or fragment at top in which you are trying to perform the operation
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Seems strange but only thing I could guess is Your URL is valid as You already said. Your remote is getting downloaded even getting applied on your image view but your placeholder is somehow hiding it.
Glide has some bugs related to placeholder stuff.
My suggestion would be to try below:
Glide.with(view.getContext()).load(url).
placeholder(R.drawable.default_profile).fitCenter().into(view);
So the trick is placeholder is set via setImageDrawable() so the ImageView will just display it as usual, but you tell Glide to use the fitCenter explicitly which will fit the loaded image nicely within the ImageView's laid out size via a Transformation and then set it via setImageDrawable(). Since the fitted image is a perfect fit, center will just draw the image covering the whole area of the view.
Give it a try.
Dont use transition or animation for Glide.It will solve your problem
Code :
Glide.with(context)
.load(horizontalHappyHourList.get(position).getImage())
// .transition(DrawableTransitionOptions.withCrossFade(400)) //Optional
.apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.RESOURCE)
.error(R.drawable.no_image))
.into(holder.imageView);
May help someone :)
My issue was network security policy exception. The URL requested by Glide was blocked by android due to security policy.
Fixed by whitelisting desired domain. Check here
com.bumptech.glide.load.engine.GlideException: Failed to load resource
There was 1 cause:
java.net.UnknownServiceException(CLEARTEXT communication to maps.googleapis.com not permitted by the network security policy)
call GlideException#logRootCauses(String) for more detail
The toughest thing is it was not shown in the normal log.
Incase none of the above solves it, and you're setting the 'src' property on the ImageView, it won't work. I was mistakenly setting the 'src' on the ImageView in xml and taking that out solved it for me.
In my case, I had a problem with the layout_height attr that I set in xml
<ImageView
android:id="#+id/image_view_image"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:visibility="gone"
app:layout_constraintBottom_toTopOf="#id/linear_layout_content"
app:layout_constraintDimensionRatio="16:9"
app:layout_constraintLeft_toLeftOf="#id/linear_layout_content"
app:layout_constraintRight_toRightOf="#id/linear_layout_content"
app:layout_constraintTop_toTopOf="#id/linear_layout_content" />
Glide was giving a line of warning in the logcat which I don't remember what it was but it was suggesting to avoid using wrap_content for width and height.
So all I did was modify the layout to avoid using wrap_content and the problem was solved.
I tried to regenerate that problem to get the warning line after the problem was solved but I couldn't. It seems to be a cache problem in the library.
Make sure to check the url (link) as it may be a page link not an exact image address, try copying the link and paste it to the browser to see if it opens just image or a web page and if it opens a web page then no doubt that is the problem it is a page link and again not an image address.
Just uninstall app and reinstall it. It will work

Using Crosswalk in an Android Cordova Project with Embedded WebView

I have an existing Android Cordova project which uses an embedded WebView. What this means is that the Activity does not extend CordovaActivity, but instead embeds the SystemWebView and initializes within the onCreate.
The following is currently how this is being done:
Within the layout XML file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
.... other layout elements not related to Cordova....
<org.apache.cordova.engine.SystemWebView
android:id="#+id/cdvWebView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
Within the Activity's onCreate:
SystemWebView systemWebView = (SystemWebView) findViewById(R.id.cdvWebView);
CordovaWebView cdvWebView = new CordovaWebViewImpl(new SystemWebViewEngine(systemWebView));
ConfigXmlParser parser = new ConfigXmlParser();
parser.parse(this);
cdvWebView.init(this, parser.getPluginEntries(), parser.getPreferences());
Due to the bug in Lollipop versions 5.0.+ missing the "set" button, I want to implement the Crosswalk plugin into the project.
Unfortunately, all the documentation I'm finding assumes that a typical Cordova install is being used. I haven't been able to get the embedding and initialization of the XWalkWebView working correctly and keep getting a blank white screen.
Has anybody has success with a similar scenario?
I'm not sure, but this might answer your question. It seems to show implementing an XWalkWebView outside of a typical cordova project:
https://github.com/kurli/crosswalk-website/wiki/How-to-use-Crosswalk-Embedded-API-on-Android

Admob 6.1.0 for monodroid

I have an app made with monodroid, it's on release stage (ready to get released) but I would like to have a free ad-supported version of my app. I've been searching the web for a tutorial of Admob 6.1.0 implementation on monodroid without any luck. I'd have to mention that I've never worked with ads before.
I was wondering if anyone has been able to use Admob 6.1.0 on monodroid and if you could share your knowledge
I've seen this source code and also this tutorial, but I just can't manage to understand correctly how to implement it. I't would be nice if someone could make an answer as a community wiki so it can help others to get introduced to Admob in monodroid
EDIT:
Tried Greg Shackles sample step by step, i'm now getting this error android.view.InflateException: Binary XML file line #1: Error inflating class com.google.ads.AdView. Any sugestion of how to make it work?
EDIT 2:
Changed the XML file and now getting 2 new errors: Java.Lang.NoClassDefFoundError and System.TypeInitializationException. XML looks like this:
<?xml version="1.0" encoding="UTF-8" ?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/Background2"
>
<com.google.ads.AdView android:id="#+id/ad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="AD_UNIT_ID_GOES_HERE"
ads:testDevices="TEST_EMULATOR,TEST_DEVICE_ID_GOES_HERE"
ads:loadAdOnCreate="true"/>
</RelativeLayout>
And adview is called like this:
public class MyActivity : Activity
{
private View _adView;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
SetContentView(Resource.Layout.AboutActivityLayout);
AdMobHelper.RegisterEmulatorAsTestDevice();
_adView = FindViewById(Resource.Id.Ad);
AdMobHelper.RequestFreshAd(_adView);
loadData();
}
}
The first error you got "android.view.InflateException: Binary XML file line #1: Error inflating class com.google.ads.AdView" is related to the Jar Build Action setting.
You should check two things first:
the jar file has the Build Action property set to "AndroidJavaLibrary"
the java file called "AdMobHelper.java" should have the Build Action property set to "AndroidJavaSource"
It seems to me that the error you are getting now is related to the java file not being correctly configured as an AndroidJavaSource.

Categories

Resources