Custom Layout in Homescreen Widget - java

I am developing a homescreen widget that has a number of different sized buttons. To maximize the use of space, I want to use a flow layout for this widget (which for some crazy reason, android has no native implementation of).
To solve this, I have tried using third party implementations and implemented the layout myself, however, I always get an exception when loading the widget:
W/AppWidgetHostView: updateAppWidget couldn't find any view, using error view
android.view.InflateException: Binary XML file line #1: Binary XML file line #1: Error inflating class [redacted].FlowLayout
Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class [redacted].FlowLayout
Caused by: java.lang.ClassNotFoundException: Didn't find class "[redacted].FlowLayout" on path: DexPathList[[zip file "/system/priv-app/Velvet/Velvet.apk"],nativeLibraryDirectories=[/system/priv-app/Velvet/lib/arm64, /system/priv-app/Velvet/Velvet.apk!/lib/arm64-v8a, /system/lib64, /vendor/lib64, /system/lib64, /vendor/lib64]]
My layout for the widget looks only like this:
<[redacted].FlowLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#09C"
android:padding="#dimen/widget_margin"
android:id="#+id/widgetRoot"
android:orientation="horizontal">
</[redacted].FlowLayout>
Assuming that the redacted namespace is correct, is there any way to do what I want or is it impossible to use a custom layout with a homescreen widget?

Home screen widgets are wery limitted things, they doesn't support custom layouts: https://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout

Related

Caused by android.content.res.Resources$NotFoundException: Drawable (missing name) with resource ID even the resource is there

On some Android devices, I am getting "Resources$NotFoundException: Drawable (missing name) with resource ID" error, e.g.:
On LGE Android 8.1 devices it is complaining
Unable to start activity ComponentInfo{SplashActivity}: android.view.InflateException: Binary XML file line #20: Binary XML file line #20: Error inflating class ImageView
For the following part. In particular, it is complaining "android:src="#drawable/red_logo""
<ImageView
android:id="#+id/logoImageView"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_marginStart="50dp"
android:layout_marginEnd="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:src="#drawable/red_logo" />
I checked the source code directory, "red_logo.png" already exists in the following folders:
drawable
drawable-hdpi
drawable-ldpi
drawable-mdpi
drawable-xhdpi
drawable-xxhdpi
drawable-xxxhdpi
Here is the Java code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash); <---- Fails here
}
The dimension of red_logo.png in drawable folder is 4864 × 2692, same size as the red_logo.png in drawable-xxxhdpi folder. Is it too big?
I did a lot of research but it seems I cannot find a valid answer.
Thank you.
Best regards.
It happens due to scale issues, hence, always use a vector asset instead of an image in such scenarios. Worked for me.

Android Java unable to find resource ID

On some devices there is no problem but on some such exception appears when opening src of ImageView.
The stack trace looks like this:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{pl.jawegiel.endlessblow/pl.jawegiel.endlessblow.activities.MainActivity}:
android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class
ImageView at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2666) at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2727) at android.app.ActivityThread.-
wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1478) at
android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at
android.app.ActivityThread.main(ActivityThread.java:6121) at java.lang.reflect.Method.invoke(Native
Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889) at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779) Caused by: android.view.InflateException:
Binary XML file line #11: Binary XML file line #11: Error inflating class ImageView Caused by:
android.view.InflateException: Binary XML file line #11: Error inflating class ImageView Caused by:
android.content.res.Resources$NotFoundException: Drawable (missing name) with resource ID #0x7f0800cd
Caused by: android.content.res.Resources$NotFoundException: Unable to find resource ID #0x7f0800cd at
android.content.res.ResourcesImpl.getResourceName(ResourcesImpl.java:228) at
android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:687) at
android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:571) at
android.content.res.Resources.loadDrawable(Resources.java:858) at
android.content.res.TypedArray.getDrawable(TypedArray.java:928) at android.widget.ImageView.
(ImageView.java:162) at android.widget.ImageView.(ImageView.java:150) at
androidx.appcompat.widget.AppCompatImageView.(AppCompatImageView.java:74) at
androidx.appcompat.widget.AppCompatImageView.(AppCompatImageView.java:69) at
androidx.appcompat.app.AppCompatViewInflater.createImageView(AppCompatViewInflater.java:199) at
androidx.appcompat.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:119) at
androidx.appcompat.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1551) at
androidx.appcompat.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1602) at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:769) at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727) at
android.view.LayoutInflater.rInflate(LayoutInflater.java:858) at
android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821) at
android.view.LayoutInflater.inflate(LayoutInflater.java:518) at
android.view.LayoutInflater.inflate(LayoutInflater.java:426) at
android.view.LayoutInflater.inflate(LayoutInflater.java:377) at
androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696) at
androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170) at
pl.jawegiel.endlessblow.activities.MainActivity.onCreate(MainActivity.java:50) at
android.app.Activity.performCreate(Activity.java:6682) at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2619) at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2727) at android.app.ActivityThread.-
wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1478) at
android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at
android.app.ActivityThread.main(ActivityThread.java:6121) at java.lang.reflect.Method.invoke(Native
Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889) at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
MainActivity.java:50 looks like this:
setContentView(R.layout.activity_main);
ImageView that causes problem looks like this:
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/splash2"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:contentDescription="TODO" />
this splash2.png is located in res\drawable, res\drawable-hdpi, res\drawable-mdpi, res\drawable-xhdpi, res\drawable-xxhdpi and res\drawable-xxxhdpi so I guess in correct folders. So how to solve that?
Thank you in advance!
You may be able to solve this problem with a more generalistic approach:
Try using scalable vector graphics (SVG) instead of .PNG-images. VectorDrawable is more flexible if you need to match different screen resolutions.
PNGs can be transformed into scalable vector graphics with Inkscape or online PNG converter (https://convertio.co/de/png-svg/)
After that you can create a new VectorDrawable by loading the SVG. VectorDrawables can be assigned with app:srcCompat="#drawable/ic_VectorDrawableName"
Documentation:
A VectorDrawable is a vector graphic defined in an XML file as a set
of points, lines, and curves along with its associated color
information. The major advantage of using a vector drawable is image
scalability. It can be scaled without loss of display quality, which
means the same file is resized for different screen densities without
loss of image quality. This results in smaller APK files and less
developer maintenance. You can also use vector images for animation by
using multiple XML files instead of multiple images for each display
resolution.
VectorDrawable also requires much less RAM than .PNG.
You may find the cause of your errors in this thread:
Resources$NotFoundException drawable-xhdpi from drawable resource

Vector images imported from SVG images not being recognised in ImageView

The SVG that I was attempting to import was not correctly formatted, this lead to the xml for the vecto image not being correctly formatted either Try the following samples here as these are all correctly formatted SVGs.
I am attempting to add a SVG image into a ImageView but i keep on getting an error every time I try to call its #drawable/ value. The file seems to be in the right location since when I use a PNg image it works, but it does not get recognised when its an SVG image.
The XML to add the image is :
<ImageView
android:id="#+id/statusImage"
android:layout_width="304dp"
android:layout_height="391dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/statusText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_temp" />
and the location of the image file is:
I have tried to modify the Gradle file so that it contains:
vectorDrawables.useSupportLibrary = true
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.android.support:design:28.0.0'
But that does not seem to solve the issue I'm having.
The error log seems to print that :
E/AndroidRuntime: FATAL EXCEPTION: main
Process: sc17dpc.individualproject, PID: 3467
android.view.InflateException: Binary XML file line #8: Binary XML file line #8: Error inflating class ImageView
Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class ImageView
Caused by: android.content.res.Resources$NotFoundException: Drawable sc17dpc.individualproject:drawable/ic_temp with resource ID #0x7f07006d
Caused by: android.content.res.Resources$NotFoundException: File res/drawable/ic_temp.xml from drawable resource ID #0x7f07006d
at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:1166)
at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:917)
at android.content.res.Resources.getDrawableForDensity(Resources.java:1074)
at android.content.res.Resources.getDrawable(Resources.java:1013)
at android.content.Context.getDrawable(Context.java:630)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:463)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:203)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:191)
at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:102)
at android.support.v7.widget.AppCompatImageHelper.loadFromAttributes(AppCompatImageHelper.java:59)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:78)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:68)
at android.support.v7.app.AppCompatViewInflater.createImageView(AppCompatViewInflater.java:182)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
at android.support.v7.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1266)
at android.support.v7.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1316)
at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:189)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:783)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:874)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:835)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at sc17dpc.individualproject.HomeFragment.onCreateView(HomeFragment.java:23)
and points to my class HomeFragment on the line:
View view = inflater.inflate(R.layout.fragment_home, container, false);
Any help is appreciated since the solutions that I've followed currently have not solved the problem.
*********EDIT*********
The SVG file is imported using the "New -> Vector Asset" built in feature in Android Studio and by using a local file.
The image file looks like:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="640dp"
android:height="640dp"
android:viewportWidth="640"
android:viewportHeight="640">
<path
followed by a load of information about the image path.
I have attempted to change app:srcCompat to android:src and this still gives the same error as before.
Can you try again?
android:src="#drawable/ic_temp"
You can't use viewportHeight and viewportWidth as 640
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="640dp"
android:height="640dp">
Source VectorDrawable
Issue has been sumarised at the top of the article. Thanks for the help.

Error inflating class ImageView in android

whenever i hit the imageview i get this error, this happens to me when i switch to test my app on different screen sizes, here is my xml
<ImageView
android:id="#+id/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="28dp"
android:layout_marginRight="28dp"
android:layout_marginTop="20dp"
app:srcCompat="#drawable/search"/>
and here is the error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ahmed.electionadmin, PID: 30789
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ahmed.electionadmin/com.example.ahmed.electionadmin.Search}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class ImageView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
I have tried so many solutions that asks me to replace src with srccompat and so on, but still is working, ant help would be appreciated
To use srcCompat you need to add vectorDrawables.useSupportLibrary = true to your build.gradle file:
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
Or you can use src instead of srcCompat to resolve the error.
android:src="#drawable/search"
Replace this:
app:srcCompat="#drawable/search"
With:
android:src="#drawable/search"
Pay attention, not only src with srcCompat, also app with android.
Let me know if it works.
Explanation: You're not using a support library ImageView, but a regular one. app namespace is usually used for support library, same as srcCompat. So you needed to change those 2 things, not just one. That's why merely changing the srcCompat with src yielded no results.
i fixed it by uploading my image inside draawable no 24
you are doing like fitting image to different resolutions , you should have image with different resolutions like hdpi xhdpi , you you can use vector drawable image

android.widget.TextView.setTextAlignment(): java.lang.NoSuchMethodError

Situation:
I have a TextView that have the property
android:textAlignment="center"
I am generating another TextView dinamically, based on my TextView from XML Layout, using a clone, cloning all the basic properties to work the way above.
Problem:
To do this i need to use this method:
this.myTextView.setMyTextViewProperty(MyTextView.getMyTextViewProperty());
for example:
this.MyTextView.setText(MyTextView.getText());
Note that this.MyTextViewis a local variable and MyTextView is a private var declared on the top of the file, under class name.
I do this on all the properties of the TextView but when i hit the following line of code from the TextAlignment property...:
this.myTextView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
I tried to set it to a Custom Aligment instead of getting from my XML TextView It gives me an error:
11-20 15:27:04.460: E/AndroidRuntime(9185): FATAL EXCEPTION: main
11-20 15:27:04.460: E/AndroidRuntime(9185): java.lang.NoSuchMethodError: android.widget.TextView.setTextAlignment
But i see on Ctrl + Space that the method exists, so i cant understand what is happening.
A second try was, to set my TextView property to the property that comes from my TextView:
this.myTextView.setTextAlignment(MyTextView.getTextAlignment());
With no success, too.
Obs: i do not want a Android XML Layout solution, i want a solution on code, because i generate the TextView dinamically on the Activity
I'm using API Level 15
Any help?
The setTextAlignment method was added in API level 17. Maybe your compiler in the IDE is above 17 and the device/emulator which you are testing is less than that. Here the link to setTextAlignment.
Added from the comments:
For API level 15 and below, you want setGravity, per this question.

Categories