I'm having hard times trying to implement a preference who switch the application theme.
Normally I would be able to do it with XML but with Jetpack Compose I saw that the best practice is to use DataStore (https://developer.android.com/topic/libraries/architecture/datastore)
Someone please, can explain me from scratch how to store a preference and change the whole application theme when the user click on a button for example?
Thank you
I believe that the DataStore documentation already explains in great detail how it works...
Talking about Compose we have the isSystemInDarkTheme function which brings up the current theme of the user's android system (which can be seen in Theme.kt inside the ui package that Android Studio automatically creates in a Compose project).
What needs to be done is whenever you open the app, check if the user has chosen to change the theme manually in your app through a function that accesses the DataStore, in other words, provide a Flow<Boolean?>, with a logic like if null the user has not changed the theme on your app, so you follow with the android system and if have the boolean, pass and apply to the MaterialTheme of Compose.
And you also need to create a function that sends a boolean to the DataStore regarding the theme change on the app.
Here is a repository with example code allowing the user to change the theme between dark and light and saving the option in the DataStore. And here is an article with theoretical and practical explanation about themes in Compose which I think may contribute to the clarification.
Related
I am working on an Android library which contains a few activities and fragments. Aim is to make it plug and play sorts that should easily fit-in in any app. However since we provide theme to every activity and not the complete application I am unsure on how to go about things. For eg: color_primary, color_accent etc are all dependent on app. These need to be set in theme dynamically. I can get these colors from app via a communicator or I can setup backend to send me these colors based on what app is using my library.
However, I am unable to figure out how to set these colors on the fly. Will it be possible to create a theme programatically and then set to activity? Or, is there any other approach to solve my usecase?
I have to create 2 application with almost the same design. But the backend i mean the way i receive data and i make request to my api is not the same. And there are also minor design such as color or bouton placement...
So i wanted to know if it's possible to create and app template i was thinking of creating a library module such as aar that would hold all the design then i would import it in both my application ?
But if i do that, is there a way to to update the design of the library within the app for exemple i assume that i want to change some color i could do it programmatically, but what happen if i want to change some button position or add rounder corner ? Do i have to provide methodes in my library to handle all those cases ? or is there a way to handle those within the app, meaning that the app could change some design of the library it self ?
In the end would like to have the logic in my app then give all information to the template library to render the design. So i would like to know if there is a way to implement such thing and what are the best practices to do it.
I have in my projects two ways.
Library, as you mention. I use it when there are routines that are common to several projects. It is possible to change color patterns and some style details. But it is often plastered.
Flavors - Build Variants https://developer.android.com/studio/build/build-variants
With build variants you can create one project/app with two versions, chandind only wat you need. This option is very good when you want to change color, endpoint, style, show or hide some system functionality. I use it and it's very good.
i have to build a new Android app. Since Jetpack Compose is now stable i want to build the entire UI with that. Further i need also a Preference/Settings screen where the user can specify his preferences. According the doc it is still recommended doing this via Fragments.
https://developer.android.com/guide/topics/ui/settings
I found also an external library witch would provide this functionality in the compose way.
https://github.com/alorma/Compose-Settings
Has anybody done this before? Whats the "cleanest" way for doing that?
Thank you
I have implemented an app in compose and I created the settings screen using the tools available in Jetpack Compose, without fragments.
The way I did this is create a composable for each settings option, that contains a title, and optional subtitle, and a checkbox, which indicates if the option is enabled or not.
These options are then added to a column (or a grid, if you have a tablet, it's easy to support both in compose); you just have to hook the clicks on the checkbox to your viewmodel to change the setting and then refresh the UI.
If you have other kinds of settings entries you can define your own composables for those as well. In my case I have other settings options that open a dialog where the user can configure some parameters, I have another composable function for this kind of settings row as well.
This is personal opinion, but I prefer to stay away from 3rd party libraries unless a) they provide significant value and b) they're from a source that offers some kind of maintenance and bug fixes guarantees. I don't know about that library you mentioned, but if you include 3rd party libraries, you need to be aware you are relinquishing control over parts of your app so you need to balance the cost/benefit ratio.
I have created an Android app using the standard controls available. It seems that most apps on the market have their own set of controls. It generally looks much better that the standard controls. How do I create better looking control? Can I just apply a theme or should I overwrite the onDraw method? For instance, there is a problem with the default android spinner control details here .
adding custom images is the best way to have more eye-catching GUI, use colorful & meaningful images to your application to appeal users. Custom theme's and styles are helpful too.....
You can apply different themes and styles on the one hand or work with 9patch-drawables - have a look here for details and examples
Simple question:
I am using PICK_CONTACT in my Android 3.0 application.
The issue is that the contact app has a light theme while my app uses a dark one.
So the question is:
Is there a way to set a style/theme when using startActivity()?
If I am creating my own library and I want the user to be able to use customize styles, I will need to receive something in the intent? Is there a better way to solve this?
Is there a way to set a style/theme when using startActivity()?
No. You have no right to mess with other apps' user interfaces, any more than they have a right to mess with yours.
In the case of PICK_CONTACT, if you are willing to have the READ_CONTACTS permission, there is nothing stopping you from writing your own contact picker activity, themed as you wish.
If I am creating my own library and I want the user to be able to use customize styles, I will need to receive something in the intent?
Since there is no setStyle() method, dynamically changing an activity's style seems troublesome.
If your library will be shipped as an Android library project, you can provide theme resources and guidance for developers who, when adding your activities to their manifest, can choose which theme to use at compile time.
No.
In general there is no standard method of specifying the theme/style an Activity should launch with: your idea (putting something in the Intent) would actually be an excellent way of doing it, but once again it isn't standard.
Wandering through the standard Contacts app source (https://android.googlesource.com/platform/packages/apps/Contacts
) there is no way to specify the theme in any of the Activity classes that PICK_CONTACT would invoke.
Your best bet would be to build a custom Contact picker and use the content provider. You'd need to ask for permissions and it would be a bit messier but that appears to be the only way to get what you want.