i'm trying to find a solution for my problem, but i can't find anywhere, even googling for it.
I'm writing an android application that uses themes, an user can switch them dinamically, and the app restart to apply the choosed one. This is working well.
But, i can't find the way to change an imageview element based on the choosed theme. I can't understand the way android apps uses themes, i can't change the file dinamically but only change the theme name in the application object, and in the theme style tag i can't specify a single imageview's src.
I can satisfy my app requirements also using only the windowbackground property. But, i can't make it strech proportionally using the scaletype property, also using gravity in a bitmapdrawable i can't understand how to make it reduce inside the windows mantaining the image proportions.
Thanks to everyone that reply to my question, and sorry for my english! :)
In the Project pane select Android, go to app > res > values > themes > themes.
<ImageView android:id="@+id/simpleImageView" android:layout_width="fill_parent" android:layout_height="wrap_content" /> 2. src: src is an attribute used to set a source file or you can say image in your imageview to make your layout attractive.
Styles and themes on Android allow you to separate the details of your app design from the UI structure and behavior, similar to stylesheets in web design. A style is a collection of attributes that specify the appearance for a single View .
The DayNight functionality in AppCompat allows your app to easily switch between a dark ⚫ and light ⚪ theme. This has many benefits for your users, from saving power on OLED displays, to increasing usability for people with reduced-vision, and more.
Recently, I had a requirement to change the ImageView
picture to be changed based on the theme. I was using an ExpandableListView
and using the ImageView
as
a group indicator rather than out of the box group indicator. Since my application supported both light and dark theme, I wanted to switch the group indicators, action bar icons to light and dark versions as well.
Here it is how:
Declare custom attributes in res/values/attrs.xml
file for each of the ImageView or ActionBar icon to be changed based on theme:
<declare-styleable name="customAttrs">
<attr name="actionSearchIcon" format="reference" />
<attr name="actionAcceptIcon" format="reference" />
<attr name="actionRefreshIcon" format="reference" />
<attr name="groupIndicatorIcon" format="reference" />
</declare-styleable>
In the theme file which would be res/values/styles.xml
or the suitable one depending upon the version you are targetting, define the same attributes pointing to the
appropriate drawables. For example below, the Black
theme is pointing to drawables related to dark theme and Light
theme is pointing to drawables related to light theme.
<style name="Black" parent="@style/Theme.AppCompat">
<item name="actionSearchIcon">@drawable/ic_action_search</item>
<item name="actionAcceptIcon">@drawable/ic_action_accept</item>
<item name="actionRefreshIcon">@drawable/navigation_refresh</item>
<item name="groupIndicatorIcon">@drawable/group_indicator</item>
</style>
<style name="Light" parent="@style/Theme.AppCompat.Light">
<item name="actionSearchIcon">@drawable/ic_action_search_light</item>
<item name="actionAcceptIcon">@drawable/ic_action_accept_light</item>
<item name="actionRefreshIcon">@drawable/navigation_refresh_light</item>
<item name="groupIndicatorIcon">@drawable/group_indicator_light</item>
</style>
Finally, coming to the actual layouts, point the relavant attributes to the attributes you have defined rather than directly to the drawables. For example in case of ActionBar icons:
<item
android:id="@+id/action_refresh"
android:icon="?attr/actionRefreshIcon"
android:orderInCategory="107"
android:title="@string/action_refresh"
app:showAsAction="always"/>
<item
android:id="@+id/action_accept"
android:icon="?attr/actionAcceptIcon"
android:orderInCategory="108"
android:title="@string/action_accept"
app:showAsAction="always"/>
<item
android:id="@+id/action_search"
android:icon="?attr/actionSearchIcon"
android:orderInCategory="50"
android:title="@string/action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="collapseActionView|always"/>
Or in case of ImageView:
<ImageView
android:id="@+id/group_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:contentDescription="@string/app_name"
android:src="?attr/groupIndicatorIcon"/>
The exciting part is that this concept can be applied to any control or any attribute :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With