I am using vector in ImageView in my activity, the app works just fine on android 7.0 but crashes on android 4.4. Logcat says, Resources$NotFoundException. I have tried solutions posted here on stackoverflow but none of them seems to be working.
These are the solutions I tried.
added this in my gradle file
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
Added this in OnCreate of activity
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
Replaced android:src with app:srcCompat in XML file.
This is my code in XML
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/ic_arrow_back_white_24dp"
android:tint="#color/colorPrimary"
android:id="#+id/details_back"
android:layout_margin="15dp"
android:layout_alignParentStart="true"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/share"
android:tint="#color/colorPrimary"
android:id="#+id/details_share"
android:layout_margin="15dp"
android:layout_alignParentEnd="true"/>
</RelativeLayout>
this is the code in JAVA file
ImageView detail_share;
detail_share = (ImageView) findViewById(R.id.details_share);
detail_share.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// code for sharing item
}
});
Also, the error occurs in second ImageView, not the first one, despite of being exactly same. I am treating both images exactly same in java file too.
I had same issue a few days back. Android 4.4 does not support vectors with (v21) written in faded font after their names in android studio directory tree, these vector graphics are only supported by API 21+. Ideally Android studio should split the vector into PNG files with different sizes but for some unclear reason Android studio does not do it on it's own sometimes.
Looks like you are trying to use the vector graphic for a "Share" icon in the above described scenario, try importing the share icon from Material icons in Android Studio only, this way the vectors imported are supported by Android 4.4 too.
Related
I am in the process of writing an app and wanted to have a Lottie animation as my splash screen, since I am learning java as I code this app I made a test app to see how things would work. I found that almost any Lottie animation displays just as a still image and does not play/loop the animation. I followed the guide on LottieFiles website and also information that I found on other questions but I still did not manage to get the animation playing. The following is exactly what I have added:
Gradle dependency of my app:
dependencies {
implementation 'com.airbnb.android:lottie:3.6.1'
}
To my Layout XML:
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/splashlottie"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:lottie_fileName="opening_book.json" //Even tried placing the file in raw and using app:lottie_rawRes="#raw/opening_book"
app:lottie_autoPlay="true"
app:lottie_loop="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
In my activity:
LottieAnimationView splashscreen;
splashscreen = findViewById(R.id.splashlottie);
splashscreen.animate();
splashscreen.playAnimation();
I am not sure exactly what I am doing wrong as I even tried various different lottie animation files and by placing the same file under raw and assets. Could the API level be the cause of the animation not playing?
Looks like you are doing it right. Is there any chance of animations are completely disabled on your phone?
I was able to run the animation with the following code:
val imageView = view.findViewById<LottieAnimationView>(R.id.image_view)
imagView.playAnimation()
xml code:
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:lottie_fileName="womensDay.json"
app:layout_constraintBottom_toTopOf="#+id/button_first"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textview_first"
app:lottie_loop="true"/>
A few things to remember:
Make sure you're placing your json file in the asset folder, if don't have one create one. Refer: https://stackoverflow.com/a/50907775/5745574
Just to test, try giving your view absolute dimension say 200dp (width & height). It might be a case where your view doesn't have proper dimension.
Try animating a different json file, your file may be corrupted
Try placing your lottieView inside a ViewGroup (Of any type e.g. LinearLayout)
Lottie animation is supported in API level 16 and above
This is work without lottieView.playAnimation() because lottie_autoPlay="true":
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/anim_view"
android:layout_width="256dp"
android:layout_height="256dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:lottie_rawRes="#raw/anim_radio_waves"
app:lottie_autoPlay="true"
app:lottie_loop="true"/>
Also check your animation json: lottie player
I need an bar as like as image.
User can pull it and change value.
I need xml or java code for android studio.
You are looking for a SeekBar
Here's a XML code
<SeekBar
android:id="#+id/simpleSeekBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
Any opdates? I hope my answer helped you
I made an Activity in Android Studio and added a button in there, added the constraints so the error would go away. When I run an App from Android Studio to my phone, it works fine. When I use an AAR file in Unity and call the activity from there, the buttons jump back to 0,0 like the error said if I did not add constraints, which I did. I'm not getting any errors either as to why it's not able to constraint the button.
Here is how I made the button in my activity.
activity_main.xml
<Button
android:id="#+id/button"
android:layout_width="86dp"
android:layout_height="wrap_content"
android:layout_marginBottom="431dp"
android:layout_marginEnd="130dp"
android:layout_marginStart="168dp"
android:layout_marginTop="32dp"
android:text="#string/StringName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
I've never really worked with Android Studio UI until now, so I might have forgotten something important here, just hoping one of you guys knows what could be the problem, thanks in advance.
EDIT
The problem is with "app:", everywhere you use it should be replaced with "android:". However android:layout_constraintBottom_toEndOf="parent" doesn't work. Instead you have to use: android:layout_alignParentEnd="true". Also replace "android.support.constraint.ConstraintLayout" with "RelativeLayout".
Unity doesn't work with "app:" for some reason so replacing with "android:" is always necessary. Thanks Soon Santos for telling me about RelativeLayout, I looked at this: https://developer.android.com/guide/topics/ui/layout/relative.html and remembered I had gotten an app: problem before and fixed by changing to android:.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.Company.MainActivity">
<Button
android:id="#+id/button"
android:layout_width="86dp"
android:layout_height="wrap_content"
android:layout_marginStart="168dp"
android:layout_marginTop="25dp"
android:text="#string/StringName"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="false"/>
</RelativeLayout>
You can actually leave out android:layout_alignParentEnd/Start. With RelativeLayout it seems to not jump back to 0,0 at all, so basically always use android: and RelativeLayout when using Unity (it seems like that at least).
Nothing wrong with the code, it works for me too. Maybe it is because your other way to run the code does not support constraintLayout, you can try RelativeLayout instead and see if it'll work.
I have set the app theme as <itemname="colorControlHighlight">#color/app_accent</item>
and applying it in AndroidMainfest.xml by <application android:theme="#style/NormalTheme">
In other API it is working fine, but when I am running the app in android 8, the color of colorControlHighlight is coming over the screen like this
In activity:
shelfViewPager = (ViewPager) findViewById(R.id.viewPager);
shelfViewPager.setAdapter(newShelfPagerAdapter(getSupportFragmentManager(), data));
I found this part of the view in xml file, is keep on highlighting during the rendering process
<FrameLayout
android:id="#+id/shelf"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar">
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
After some research found a part in Android API 27 or higher, called drawDefaultFocusHighlight at the end of the draw method. As a result, it is applying the DefaultHighlight color automatically.
Setting this value to false worked fine.
You can know more about this in the following Documentation - https://developer.android.com/reference/android/view/View.html#setDefaultFocusHighlightEnabled(boolean)
I've seen most of post # stackoverflow and i've done as told.
Create new xml (tried a few different samples)
But i can't change the background in activity xml, it does not want to find "#drawable/shape"
I have the new xml in right folder and I'm struggling with it since this morning. I'd be more than glad to see some help ;)
Sorry, I've got it like that:
'
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/chest"
android:id="#+id/button"
android:textColor="#android:color/black"
android:layout_gravity="center"
android:textSize="23sp"
android:layout_toStartOf="#+id/maxWagi"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/maxWagi"
android:background="#drawable:button_shape"'
// <----- if I change it to some #color it's working just fine.
and my xml button_shape is in app\src\main\res\xml\ (android studio put it over there by itself) I even added manually drawable folder like in most tutorials but it does not change anything
You have syntax mistake. Your background should be #drawable/button_shape instead of #drawable:button_shape. Also put in a drawable folder in res.