Forum Xamarin.Android

Want to change ImageView src (icon) based on theme dynamically?

What I have currently as follows not working.....

in attrs.xml file

<resources>
    <attr name="speaker_icon" format="reference"/>
    <attr name="footer_background" format="reference"/>
</resources>

in style.xml file

<resources>
    <style name="MyTheme" parent="Theme.AppCompat">     
            <item name="speaker_icon">@drawable/speaker_icon_dark</item>
            <item name="footer_background">@color/footer_background_dark</item>
    </style>
    <style name="MyTheme.Light" parent="Theme.AppCompat.Light"> 
        <item name="speaker_icon">@drawable/speaker_icon</item>
        <item name="footer_background">@color/footer_background</item>
    </style>
</resources>

in my layout.axml file

<?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="wrap_content"
    android:minHeight="?attr/actionBarSize"
    app:background="?attr/footer_background">

    <RelativeLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center">
        <LinearLayout
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:orientation="vertical"
            android:layout_alignParentLeft="true">
            <ImageView
                android:id="@+id/footer_ImageviewSpeakers"
                android:layout_width="wrap_content"
                android:layout_height="25dp"
                android:layout_gravity="center"
                android:adjustViewBounds="false"
                 app:src="?attr/speaker_icon"
                android:contentDescription=""/>
    </LinearLayout>
</RelativeLayout>
</android.support.v7.widget.Toolbar>

app:background="?attr/footer_background" in root working fine but, in app:src="?attr/speaker_icon" in ImageView not working. It shows build error.

## ERROR MESSAGE:

attribute src (aka com.xxx.abc:src) not found.

Tagged:

Answers

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    Please change attrs.xml file like following code format.

    <?xml version="1.0" encoding="utf-8" ?>
    <resources>
        <declare-styleable name="customAttrs">
          <attr name="speaker_icon" format="reference" />
          <attr name="footer_background" format="reference" />
        </declare-styleable>
    </resources>
    

    Xamarin forums are migrating to a new home on Microsoft Q&A!
    We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
    For more information, please refer to this sticky post.

  • Arif-Uz-ZamanArif-Uz-Zaman BDMember ✭✭

    @LeonLu said:
    Please change attrs.xml file like following code format.

    <?xml version="1.0" encoding="utf-8" ?>
    <resources>
      <declare-styleable name="customAttrs">
        <attr name="speaker_icon" format="reference" />
        <attr name="footer_background" format="reference" />
      </declare-styleable>
    </resources>
    

    Nothing changed, got the same error. Please help me. Stuck here for almost 2 days.

  • Arif-Uz-ZamanArif-Uz-Zaman BDMember ✭✭
    edited February 22

    If I change app:src="?attr/speaker_icon" to android:src="?attr/speaker_icon" it will also give me runtime error like below.

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    Did you try it about all of my code?

  • Arif-Uz-ZamanArif-Uz-Zaman BDMember ✭✭
    edited February 22

    Yes, your demo app works fine. But, same code show the above runtime error from my project. Do you have any idea why it show that error?

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    You can try to open your project folder. Delete bin and obj folder, then rebuild your project, if you have same issue. If you still have this issue, due to cannot reproduce your issue. Could you share a demo?

Sign In or Register to comment.