• Breaking News

    Thursday, June 3, 2021

    Android Dev - Compose is the future: my experience, why I love it and tips on getting started

    Android Dev - Compose is the future: my experience, why I love it and tips on getting started


    Compose is the future: my experience, why I love it and tips on getting started

    Posted: 02 Jun 2021 12:29 PM PDT

    I've been an Android dev for more than 10 years.

    I've followed compose for a long time. Among the other things by following Leland Richardson streams, which are really nice to see how to use it.

    I've done the 3rd compose challenge, the one with the provided design by google (without actually trying to win it). I manage to reproduce it in 2 days of work more or less. And it was the first time i wrote some compose. I really enjoyed the experience!

    Than, 3 weeks ago i started to work on my first real project, for a customer, 100% in compose and I've been using it daily since than.

    It's easier. There is no question about it. Everything you write is basically a custom view. It's not scary, it is fairly straightforward once you get the hang of it.

    Someone say XML is more intuitive. I say it is just what you are used to.

    Compose has issues, but they are more bug-related than anything. And there's still some work to do with the performances.

    But why I think it's the future?

    Many reasons.

    It is stateless. And this makes all the difference in the world: given some parameters it spit out some UI. Every interaction is given to you and it's up to you to change the state. This alone give you an unprecedented customization power on the behavior of any component.

    It also have some drawback, you are used to plug in an EditText (or the material version of it) and it works. Yeah, no: the TextField does a lot of stuff, but managing the state is your job now:

    kotlin // you usually want to keep the state in a viewmodel, not like this var fieldState = remember { mutableStateOf(TextFieldValue()) } TextField( value = fieldState.value, onValueChange = { fieldState.value = it } // callback )

    If you do not implement onValueChange and you do not trigger some update to fieldValue you will type in it and nothing will happen.

    This may look a bit annoying at first but it is actually WAY better. Do you want to prevent the user to input some character? Just manipulate what you get onValueChange and do whatever you need.

    Also the cursor position and selection is part of the state now, so if you want to play with that you are free to do so.

    Basically your code describe the UI at any given time.

    What I mean by that?

    The compiler knows that you are passing fieldState.value to the TextField. So if that changes it recompose === call your code again.

    So when you do fieldState.value = it in your callback this trigger a recomposition of that part and your TextField is redraw.

    The remember { } let you compute something that is kept between recomposition, so that you can remember the state you set. But for a TextField you usually want to keep the state in a viewmodel instead.

    Some clarification on what i actually mean by stateless: you can still build state full ui components with compose... But framework widgets are stateless. While most view system widgets were stateful.

    Now, the fact that your code describe your UI at any given times means...

    Animations are SOOO much easier. You can access a sort-of clock and do math on it to decide whatever you want. But there are many tools that do that for you. No more checking what's the current state: is it mid-animation? Need to cancel the old one and recompute from the current state? No. Just a target state and some interpolation on how to get there.

    Here a small example of what I mean by "sort-of" clock

    ```kotlin @Composable fun MyFunnySwitch(on: Boolean) { val fluidOnOff: Float by animateFloatAsState(if (on) 1f else 0f) // now fluidOnOff will automatically transition from 1 to 0 and vice-versa // whenever I call it with a different value of on/off // so here I can do math to show how I want my UI to look when fluidOnOff // is 0.76 or 0.3 etc...

    // you can use those numbers to compute a padding, an alpha value, // even a color or anything really...

    // and the default animation between 0 and 1 is a spring animation but // you can change it to a tween animation or whatever you want } ```

    It's not the only way to animate, it's a low level powerful way to do so. And far more intuitive. Also, your code using your widget doesn't have to know about its internal animation at all.

    It's composable. Really, it's all function. There's no ViewGroup with complex measure, layout and children taking layoutParams... It's a composable callback you just invoke where you want to insert something else. It you need to pass arguments they are parameters or you expose them with functions.

    ```kotlin @Composable fun MyCoolContainer( content: @Composable (someParameter: Whatever) -> Unit ) { // do whatever you want here, and than when you want content(myParameter)

    // need it again? why not content(someOtherParameter) }

    @Composable fun Usage() { MyCoolContainer { param: Whatever -> // composable code here } } ```

    There, you made a composable widget. What it does is up to you, but it's not rocket science, it's just callback and function calls.

    Today there aren't many libraries. But it is way easier to write libraries and reusable UI. Doesn't take a deep knowledge of the view system to write a custom UI and share it. There aren't many gotchas.

    All is dynamic, no more compile time theming.

    ```kotlin val appColors = viewModel.colorsComingFromBackend.observeFlowAsState()

    MyTheme(colors = appColors) { MyApp() } ```

    All is a library: no more getting stuck on old capabilities of the UI system not yet supported by the OS.

    It gives you access to lower level functions, so it's easier to just draw on the screen if you feel like it.

    ```kotlin LiterallyAnyWidget( modifier = Modifier.drawBehind { // oh! look, basically a canvas where I can draw stuff and // it will be behind my widget!

    size.height // here's the height of this widget // let's draw some rectangle.. drawRect(color = ..., topLeft = ..., size = ...) 

    } ) ```

    Touch handling is easier too, cause you have Coroutines build in. Instead of writing code that listen to touch events, save which pointer has been pressed, than wait for it to be released, but without moving and perform all kind of checks you just write 1 line to wait for it... It suspend and get back to it when it happened.

    There is some raw edges still, current UI components are not very customizable. Some documentation and examples are missing.

    It's different? Completely?

    It's easy? No, it's easier tho. But you need to learn it first.

    It's better? Absolutely. I've no doubt about it.

    If you ask specific questions I'll try to answer them.

    To get proficient fast these are my suggestions:

    STEP 1

    Watch this YouTube video on learning compose by examples by Nick Butcher. It's a bit outdated in some part but it gives you a good idea of the design.

    STEP 2

    Clone this repository: https://github.com/android/compose-samples

    Compile and run those apps, then try then out while looking at the code to see how they did stuff.

    If you want more advance stuff and you have more time check out the dogfooding videos from Leland Richardson. He's one of the lead developer of compose trying to reproduce some random design live in compose.

    STEP 3

    This is important important: get your hands dirty in it. If you don't know where to start just grab this challenge and try to do it: "Android Developers Blog: Android Dev Challenge: Week 3 - Speed round"

    Doesn't matter which one of the 3 you pick. It's full of repositories out there of people that did this, so you can search on how others did if you get stuck and you start with a well done design. If you pick EMEA you can also check my github repository I linked above.

    But do not forget to stray off the google Desing and try some stuff yourself.

    Some small but important suggestions:

    • In your compose always separate the "wiring" compose from the "ui" compose. The ui should not depend on any viewmodel, it should just receive parameters and callbacks: you can preview it and reuse anywhere. The "wiring" just connect your viewmodel to your ui.
    • compose has the concept of content color vs background color. Material widgets use this with your MaterialTheme to automatically match content color if you use Surface
    • adding on that: providers are what give you access to contextual data, and since they are also functions you can use it to switch theme or some other contextual settings: it's how you get access to themes, device density and context
    • accompanist is an essential library

    (I've copied my answer to another post) improving on it to create this post)

    If you guys have specific questions I'll try to answer them

    submitted by /u/borninbronx
    [link] [comments]

    Starting in late 2021, when a user opts out of interest-based advertising or ads personalization, the advertising identifier will not be available. You will receive a string of zeros in place of the identifier.

    Posted: 02 Jun 2021 06:13 PM PDT

    How can I secure my game from fraud and abuse?

    Posted: 03 Jun 2021 06:43 AM PDT

    I have a game uses Firestore, When the player complete the certain level the game will give him 50 points and will be save the data in Firestore.

    Let's suppose someone did reverse engineering for my game and made change from 50 points to 1000 points in code and he rebuild the APK and play my game with the same Firestore database, Now when the player complete certain level the game will give him 1000 points and will be save the data in Firestore and that considered hacked data.

    I don't care if someone did reverse engineering of my game and republish it as new game with his own Firestore, But I care about hackers who changed the data on my Firestore

    How can I secure my game from fraud and abuse.

    submitted by /u/DismalCall5534
    [link] [comments]

    Synchronize stored data locally with the Rest API continuously

    Posted: 03 Jun 2021 05:14 AM PDT

    Hi there, this is the first question for me here so I'm excited to hear your opinions.

    Now I'm wokring on an app that comunicate with Rest API, There are some data that I need for multiple screen always sush as (user info) so I will save them into Room(local database), but these data maybe changed in Rest API at any time, then the data which exists in Room will be Mismatched with the API data.

    So I'm wondering what is the best practise to make the data in Room matched Rest Api data always, in other words, I want to reflect any change oocurr in Rest Api to Room database as soon as occurred in somehow.

    sorry for bad english and big thank to google translate :)

    submitted by /u/AmrJyniat
    [link] [comments]

    Android Resource Manager tool

    Posted: 03 Jun 2021 08:12 AM PDT

    Android Resource Manager tool

    JavaFX tools for Android Developer to help them resize images with many sizes as Drawables or Mipmaps in Parallel, also provide a very fast keyword searching in the project, and analysis the XML files to hell you if there are any Dimens or colors values in the layouts and you should move them into values XML files, you can also analyze the number of lines or files for each language.

    Github: https://github.com/AmrDeveloper/androidresourcemanager

    If you have suggestions or issues you can easily create a new issue or PR on the repository

    Image Resizing

    Color Analysis

    Dimens Analysis

    Keyword Searching

    Source files Analysis

    submitted by /u/AmrDeveloper
    [link] [comments]

    Trying to download internal test track, keep getting live version instead

    Posted: 03 Jun 2021 08:05 AM PDT

    Hi friends, I'm not a developer, but I'm trying to download an internal test track version of an app.

    I was shared the link, and it clearly says on the goolge playstore screen Appname (Internal Beta), but when I click download, I keep getting the live version. (my email is added on the console as a tester)

    What do you think may be the issue? thanks

    submitted by /u/anthonynej
    [link] [comments]

    How to draw a 3d barchart?

    Posted: 03 Jun 2021 01:01 AM PDT

    How to draw a 3d barchart?

    Hey there, I want to draw a 3d bar chart in Android Studio.

    Do you know any good free library to use?

    The result I want is this (Done with Python)

    Thank you very much!

    https://preview.redd.it/vd53j0c9c0371.png?width=1920&format=png&auto=webp&s=9ce5bcbfbc49f93685b742594eb8c3bca6ae9f43

    submitted by /u/MrNasico
    [link] [comments]

    Your suggestions about a recipe app

    Posted: 02 Jun 2021 08:10 PM PDT

    Your suggestions about a recipe app

    Hello,I'm developing a recipe app without API. Basically there'll be a clickable list in a fragment and that lead us to details fragment.I'm very beginner level to MVVM,Room,Hilt etc.I'm planning to use Firebase to save my recipe datas and images.

    I want to know your suggestions like "This isn't best way to do this so try this one" or "You can't do this with MVVM" because I'm confused right now.I watched a instructor and he was using model class to fetch data from API but I don't know how to fetch data and show in a fragment in MVVM.

    Thanks(I'm Kotlin user)

    https://preview.redd.it/yb1xakifwy271.png?width=456&format=png&auto=webp&s=a30d6b865913ed153851e68c01cd7c0b5b59bbdf

    submitted by /u/f527h94
    [link] [comments]

    On Android is there an equivalent to this drag-down-to-dismiss modal view on iOS?

    Posted: 02 Jun 2021 11:29 AM PDT

    Library for translations

    Posted: 02 Jun 2021 11:10 AM PDT

    Is there a library for commonly used translations for Strings like "yes" (androids "yes" is actually "ok"), "no", "next", "privacy policy", "settings", ...

    I'm tired to translate them again for every app.

    submitted by /u/olitv
    [link] [comments]

    Bubble Chat idea

    Posted: 02 Jun 2021 11:09 AM PDT

    Bubble Chat idea

    Hi! I am a software developer and i just had this idea in mind and I would want to ask if it is an interesting idea. Thanks in advance for reading!

    The app would be a chat app where you could create a chat room with a word that would describe the theme or the subject that would be discussed.

    The active rooms would be seen as bubbles in your home screen like the agar.io game. By pressing on them you could enter that room and engage on the topic proposed.

    Does this sound appealing?

    Here is a concept of the idea:

    https://preview.redd.it/ic6tnaon7w271.png?width=568&format=png&auto=webp&s=a964c34063f919ae5dba9994abb33ee6b1854ee4

    View Poll

    submitted by /u/RedJohn27
    [link] [comments]

    Buying a Mac M1 with 8gb RAM for android dev is enough?

    Posted: 02 Jun 2021 10:04 AM PDT

    Hi guys I want to change my working laptop and a friend is trying to sell his Mac so he gave me a really good offer. Should i take it? I've had really good expirences working with Mac before and I'll start studying IOS development.

    How is the performance of Android Studio with the M1 chips? Any recomendations?

    Big note here: I live in Uruguay so finding Apple hardware at good price is really hard specially during covid times.

    Thanks!!

    submitted by /u/PegsTheEmitter
    [link] [comments]

    No comments:

    Post a Comment