Showing posts with label vectorDrawables. Show all posts
Showing posts with label vectorDrawables. Show all posts

android.content.res.Resources$NotFoundException: File res/drawable/try_angle.xml from drawable resource ID

01-29 13:10:13.126 1634-1634/com.elitech.offers.coupons.deals W/System.err: android.content.res.Resources$NotFoundException: File res/drawable/try_angle.xml from drawable resource ID #0x7f0701c6
01-29 13:10:13.130 1634-1634/me.me2.com.myapp W/System.err:     at android.content.res.Resources.loadDrawable(Resources.java:2170)
01-29 13:10:13.130 1634-1634/me.me2.com.myapp W/System.err:     at android.content.res.Resources.getDrawable(Resources.java:710)
01-29 13:10:13.130 1634-1634/me.me2.com.myapp W/System.err:     at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:465)
01-29 13:10:13.130 1634-1634/me.me2.com.myapp W/System.err:     at me.me2.com.myapp.V2.HomeRecyclerViewAdapter.onBindViewHolder(HomeRecyclerViewAdapter.java:1055)
01-29 13:10:13.130 1634-1634/me.me2.com.myapp W/System.err:     at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
01-29 13:10:13.131 1634-1634/me.me2.com.myapp W/System.err:     at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823)
01-29 13:10:13.131 1634-1634/me.me2.com.myapp W/System.err:     at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5752)
01-29 13:10:13.131 1634-1634/me.me2.com.myapp W/System.err:     at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6019)
01-29 13:10:13.131 1634-1634/me.me2.com.myapp W/System.err:     at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
01-29 13:10:13.131 1634-1634/me.me2.com.myapp W/System.err:     at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
01-29 13:10:13.131 1634-1634/me.me2.com.myapp W/System.err:     at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
01-29 13:10:13.131 1634-1634/me.me2.com.myapp W/System.err:     at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
01-29 13:10:13.131 1634-1634/me.me2.com.myapp W/System.err:     at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
01-29 13:10:13.131 1634-1634/me.me2.com.myapp W/System.err:     at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
01-29 13:10:13.131 1634-1634/me.me2.com.myapp W/System.err:     at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3875)
01-29 13:10:13.131 1634-1634/me.me2.com.myapp W/System.err:     at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3639)
01-29 13:10:13.131 1634-1634/me.me2.com.myapp W/System.err:     at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1877)
01-29 13:10:13.131 1634-1634/me.me2.com.myapp W/System.err:     at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:407)
01-29 13:10:13.132 1634-1634/me.me2.com.myapp W/System.err:     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:788)
01-29 13:10:13.134 1634-1634/me.me2.com.myapp W/System.err:     at android.view.Choreographer.doCallbacks(Choreographer.java:591)
01-29 13:10:13.134 1634-1634/me.me2.com.myapp W/System.err:     at android.view.Choreographer.doFrame(Choreographer.java:559)
01-29 13:10:13.134 1634-1634/me.me2.com.myapp W/System.err:     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:774)
01-29 13:10:13.134 1634-1634/me.me2.com.myapp W/System.err:     at android.os.Handler.handleCallback(Handler.java:808)
01-29 13:10:13.134 1634-1634/me.me2.com.myapp W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:103)
01-29 13:10:13.134 1634-1634/me.me2.com.myapp W/System.err:     at android.os.Looper.loop(Looper.java:193)
01-29 13:10:13.134 1634-1634/me.me2.com.myapp W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5333)
01-29 13:10:13.135 1634-1634/me.me2.com.myapp W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
01-29 13:10:13.135 1634-1634/me.me2.com.myapp W/System.err:     at java.lang.reflect.Method.invoke(Method.java:515)
01-29 13:10:13.135 1634-1634/me.me2.com.myapp W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
01-29 13:10:13.137 1634-1634/me.me2.com.myapp W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
01-29 13:10:13.137 1634-1634/me.me2.com.myapp W/System.err:     at dalvik.system.NativeStart.main(Native Method)
01-29 13:10:13.137 1634-1634/me.me2.com.myapp W/System.err: Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #2: invalid drawable tag vector
01-29 13:10:13.138 1634-1634/me.me2.com.myapp W/System.err:     at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:933)
01-29 13:10:13.138 1634-1634/me.me2.com.myapp W/System.err:     at android.graphics.drawable.Drawable.createFromXml(Drawable.java:877)
01-29 13:10:13.138 1634-1634/me.me2.com.myapp W/System.err:     at android.content.res.Resources.loadDrawable(Resources.java:2166)
01-29 13:10:13.138 1634-1634/me.me2.com.myapp W/System.err:  ... 30 more

How to programmatically set drawableLeft on Android button in Kotlin?

MainActivity.kt
package com.example.espl.drawableleftkotlinbutton
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        /**
         * Sets the Drawables (if any) to appear to the left of, above, to the
         * right of, and below the text. Use 0 if you do not want a Drawable there.
         * The Drawables' bounds will be set to their intrinsic bounds.
         * <p>
         * Calling this method will overwrite any Drawables previously set using
         * {@link #setCompoundDrawablesRelative} or related methods.
         *
         * @param left Resource identifier of the left Drawable.
         * @param top Resource identifier of the top Drawable.
         * @param right Resource identifier of the right Drawable.
         * @param bottom Resource identifier of the bottom Drawable.
         */

        button1.setOnClickListener(View.OnClickListener {

            //Programmatically set drawableLeft on Android button - setting the smiley PNG image
            button2.setCompoundDrawablesWithIntrinsicBounds(R.drawable.smiley, 0, 0, 0);
//            or
//            val drawable = ContextCompat.getDrawable(this, R.drawable.smiley)
//            button2.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null)

            //Programmatically set drawableLeft on Android button - setting the andriod VectorDraawable
                        button3.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_android_black_24dp, 0, 0, 0);
//            or
            //            val vectorDrawable = ContextCompat.getDrawable(this, R.drawable.ic_android_black_24dp)
//            button3.setCompoundDrawablesWithIntrinsicBounds(vectorDrawable, null, null, null)
        })

    }
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:padding="20dp"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <Button
            android:layout_gravity="center"
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Click to programmatically set drawableLeft on below buttons"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

    <Button
            android:layout_centerInParent="true"
            android:layout_below="@id/button1"
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="BUTTON 2 -set png image"
            android:drawablePadding="5dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

    <Button
            android:layout_centerInParent="true"
            android:layout_below="@id/button2"
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="BUTTON 3 -set VectorDrawable"
            android:drawablePadding="5dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

</RelativeLayout>

Testing devices:
//Tested on below devices working fine on both devices (tested for above kotlin code)
// HTC Desire 620G - Android OS - Android Version - 4.4.2 - KitKat API 19
// RedMi note 3 - Android OS - Android Version - 6.0.1 - Android – Marshmallow - API level 23
It's working on Kitkat device When you are using an android vector drawable, So the above Kotlin code has backward compatibility for API below 21

Android change vector drawable color programmatically

In the xml-file you can set custom color with the attribute android:fillColor but to change the color of vector drawable in runtime/dynamically. Tere is need to Change fillColor of a vector drawable in android programmatically, you can edit fill Color of a vector-file in Android programmatically using DrawableCompat.setTint() method which change tint color of whole vector drawable.

Here, ImageView is set with drawable vector icon of black color. This example changes android ImageView srcCompat vector drawable tint color to colorAccent as defined in the colors.xml.

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:padding="10dp"
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:textAllCaps="false"
        android:text="Click to Change Image Vector Drawable Color"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="100dp"
        android:layout_height="100dp"
        app:srcCompat="@drawable/ic_settings_black_24dp"
        android:scaleType="fitCenter" />
    <!--android:tint="@color/colorPrimary"-->
</LinearLayout>


MainActivity.java:
package com.example.espl.changedrawablecolor;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    ImageView imageView;
    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button=findViewById(R.id.button);
        imageView=findViewById(R.id.imageView);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DrawableCompat.setTint(imageView.getDrawable(),ContextCompat.getColor(getApplicationContext(), R.color.colorAccent));
            }
        });
    }
}

How to change color of vector drawable on button click android imageview
Default Color of the ImageView Drawable is black color before button click

Android Change Image Vector Drawable Color programmatically
Changed Color of the ImageView Drawable on button click


build.gradle (Module:app)
apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.espl.changedrawablecolor"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        vectorDrawables.useSupportLibrary = true
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}


Popular Posts