Android app: Switch images based on theme from code

Nimesh Madhavan

I am trying to show a favorite button which as two states (pressed/not pressed). I have 2 sets of images I want to use depending up on the theme selected (Light/Dark).

Although i am able to switch the image based on theme I am not sure how to get the Url to the image from the code so that I can show the image according to the button state. I tried using Selectors too, but I don't seem to be able to put ?attr values in selector to switch images by theme.

Style definition:

<attr name="star_on_image" format="reference"/>
<attr name="star_off_image" format="reference"/>

<style name="LightTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="star_on_image">@drawable/ic_action_important</item>
    <item name="star_off_image">@drawable/ic_action_not_important</item>
</style>

<style name="DarkTheme" parent="Theme.AppCompat">
    <item name="star_on_image">@drawable/ic_action_dark_important</item>
    <item name="star_off_image">@drawable/ic_action_dark_not_important</item>
</style>

Button Definition:

<ImageButton
            android:layout_width="@dimen/button_size"
            android:layout_height="@dimen/button_size"
            android:id="@+id/favorite_button_on"
            android:src="?attr/star_off_image"
            android:scaleType="center"
            android:visibility="gone"
            />

I want to switch this image's src attribute to "?attr/star_on_image" from code. Any ideas on how to do this?

ByteHamster

Use this:

TypedValue typedValue = new TypedValue();
getTheme().resolveAttribute(R.attr.star_on_image, typedValue, true);
ImageButton star = ((ImageButton)findViewById(R.id.favorite_button_on));
star.setImageResource(typedValue.resourceId);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Dark theme android app

From Dev

Android Manifest app:theme

From Dev

Defining theme in Android APP

From Dev

Upload multiple images to server from Android app

From Dev

Upload multiple images to server from Android app

From Dev

How can we switch the theme in Ionic App from Lighter version to darker version ?

From Dev

Android Change Theme Inside App

From Dev

Change Android App Theme Permanently

From Dev

Modify android theme from appcompat theme

From Dev

AutoCompleteTextView click event on Android based from code

From Dev

Array Adapter Load Images from Res Folder (Android App)

From Dev

Failed uploading larger (size) images from an android app to database

From Dev

Android app uses a lot of data when retrieving images from firebase

From Dev

Shopify App Necessary? Hard-code into Theme?

From Dev

Theme is not set by pressing Switch. App doesn't crash

From Dev

Android Wear How to dynamically switch Android Watch Face by code like 'Beautiful Watches' app does?

From Dev

Android Wear How to dynamically switch Android Watch Face by code like 'Beautiful Watches' app does?

From Dev

Android : using images on android app

From Dev

Adding images from within the app based on json response from url in a UICollectionView

From Dev

Styling Images based on array of images from JSON

From Dev

How to change the color theme of android app?

From Dev

changing app material theme colorPrimaryDark programatically in android?

From Dev

android- apply holographic theme to my app

From Dev

Why is my android app "black theme"?

From Dev

Setting the theme for an android app dynamically with setTheme()

From Dev

How to change the color theme of android app?

From Dev

Customize the entire theme of android app on a single click

From Dev

Android, Change App:theme value of SwitchCompat programmatically

From Dev

Android code folding the Switch block

Related Related

  1. 1

    Dark theme android app

  2. 2

    Android Manifest app:theme

  3. 3

    Defining theme in Android APP

  4. 4

    Upload multiple images to server from Android app

  5. 5

    Upload multiple images to server from Android app

  6. 6

    How can we switch the theme in Ionic App from Lighter version to darker version ?

  7. 7

    Android Change Theme Inside App

  8. 8

    Change Android App Theme Permanently

  9. 9

    Modify android theme from appcompat theme

  10. 10

    AutoCompleteTextView click event on Android based from code

  11. 11

    Array Adapter Load Images from Res Folder (Android App)

  12. 12

    Failed uploading larger (size) images from an android app to database

  13. 13

    Android app uses a lot of data when retrieving images from firebase

  14. 14

    Shopify App Necessary? Hard-code into Theme?

  15. 15

    Theme is not set by pressing Switch. App doesn't crash

  16. 16

    Android Wear How to dynamically switch Android Watch Face by code like 'Beautiful Watches' app does?

  17. 17

    Android Wear How to dynamically switch Android Watch Face by code like 'Beautiful Watches' app does?

  18. 18

    Android : using images on android app

  19. 19

    Adding images from within the app based on json response from url in a UICollectionView

  20. 20

    Styling Images based on array of images from JSON

  21. 21

    How to change the color theme of android app?

  22. 22

    changing app material theme colorPrimaryDark programatically in android?

  23. 23

    android- apply holographic theme to my app

  24. 24

    Why is my android app "black theme"?

  25. 25

    Setting the theme for an android app dynamically with setTheme()

  26. 26

    How to change the color theme of android app?

  27. 27

    Customize the entire theme of android app on a single click

  28. 28

    Android, Change App:theme value of SwitchCompat programmatically

  29. 29

    Android code folding the Switch block

HotTag

Archive