r/androiddev • • Mar 07 '24

create a UI without XML or Compose

Example:

        with(target) {
            addLinearLayout {
                setOrientationVertical()
                setBackgroundColorId(R.color.TitleBackground)
                addLinearLayout {
                    setOrientationHorizontal()
                    setGradientBackgroundVertical(
                        R.color.black,
                        R.color.TitleBackground
                    )
                    setGravityBottom()
                    addTextView {
                        setPaddingsDP(10, 0)
                        setTextSizeSP(40)
                        setTextColorID(R.color.white)
                        setFontId(R.font.nunito_bold)
                        setTextId(R.string.app_name)

                        setOnClickListener {
                            content.set(StackList())
                        }
                    }
                    addFiller()
                    addImageView {
                        setDrawableId(R.drawable.owl)
                        setSizeDP(40)
                        setMarginsDP(0, 0, 5, 5)
                    }
                }
                addView {
                    setHeightDP(3)
                    setWidthMatchParent()
                    setBackgroundColorId(R.color.WhiteTransparent)
                }
            }
        }
    }

full App: https://github.com/koje71/com.koje.cards

3 Upvotes

36 comments sorted by

58

u/F__ckReddit Mar 07 '24

Ok. Why?

6

u/koje1971 Mar 07 '24

for fun :-)

10

u/AwkwardShake Mar 07 '24

People are too tight arse on this sub brother. Don't mind the downvotes, keep experimenting and enjoying.

35

u/vyashole Mar 07 '24

Why, God, why?

22

u/Zhuinden Mar 07 '24

If only people had asked that whenever they used Anko

7

u/sebjapon Mar 07 '24

So Anko is like a precursor to Compose that creates xml views from declarative syntax?

And what OP is doing is similar I guess.

Would have Compose if Anko never happened?

12

u/Zhuinden Mar 07 '24

So Anko is like a precursor to Compose

Pre-dates it, but not really a pre-cursor.

that creates xml views from declarative syntax?

It's kinda the other way around... "XML views" are actually just views described in XML. Anko layouts were also just views described in Kotlin with T.() -> Unit functions.

Honestly it is not in any way any more declarative than an XML layout. Something closer to Compose would have been like https://github.com/anvil-ui/anvil which was also not required by Compose to create Compose.

Would have Compose if Anko never happened?

yes

3

u/sebjapon Mar 07 '24

Thanks for taking the time to answer

3

u/CrisalDroid Not the droid you're looking for Mar 08 '24

The real horror is this stuff:

fun getUsers(db: ManagedSQLiteOpenHelper): List<User> = db.use {
    db
        .select("Users")
        .whereSimple("family_name = ?", "John")
        .doExec()
        .parseList(UserParser)
}

Devs need to stop being afraid of SQL statements.

1

u/Zhuinden Mar 08 '24

doExec() reads like clean code clean api design

6

u/vyashole Mar 07 '24

Don't remind me of that. Anko layouts were a nightmare.

6

u/TheTomatoes2 Mar 07 '24

Imperative UI declaration is luckily a thing of the past

6

u/[deleted] Mar 07 '24

I endorse this kind of experimentation, best luck to you cocaine-fueled dev :)

3

u/CrisalDroid Not the droid you're looking for Mar 08 '24

I swear it's just sugar and caffeine!

13

u/CommunicationFun2962 Mar 07 '24

I think this idea deserves attentions. When it is used with runtime interpreters, UI can be controlled by server responses without app updates, which could be a good competitor of web apps. Compose cannot do this, because it is coupled with compilers. But well, ease of utilizing this stuff and the tool sets to support are issues.

3

u/CharaNalaar Mar 07 '24

This sounds vulnerable to code injection attacks.

2

u/CommunicationFun2962 Mar 08 '24

The code is supposed to be transmitted over a TLS-protected connection, not an user input. Also the security depends on how the developer configures the callable whitelist.

2

u/codersaurabh Mar 07 '24

So once I took interview of a guy , who worked for X government company ( pretty big name in India ) and asked about the feature which was dynamic for everyone, so they were getting which layouts to render from back end , like let's say textview followed by imageview etc and so on , and via that they created some logic , so basically form back-end they get json which they discussed some pattern and dynamically developed UI for same. I was pretty impressed. ( Btw even i after i selected him , he joined another firm with greater package)

2

u/ColdHeart653 Mar 08 '24

I'm doing the same thing where I work, helped in implementation of such a system where we get jsons as the api response and it's used to directly render the UI.

1

u/codersaurabh Mar 08 '24

Cool , btw why it was your need to do? Wat kind of application

1

u/ColdHeart653 Mar 08 '24
  1. It was the need because business deemed it so, such that they don't want to depend on the frontend/app release cycle to deploy the required changes, simply put, if things are directly changed from the backend you don't have to wait for the Play Store releases.

  2. It's the app for one of the biggest telecom companies in the US

1

u/codersaurabh Mar 08 '24

Great thanks for information

3

u/[deleted] Mar 07 '24

No thank you

2

u/4Face Mar 07 '24

Sounds like “Create an app without using the keyboard”

1

u/afleshner Mar 08 '24

I mean ok, doesn't sound like fun but 100% possible. I've done it building dynamic UIs from a web response

1

u/AsdefGhjkl Mar 20 '24

I always liked this approach in iOS as well, with one big issue: lack of a preview.

It makes it a no-go for most circumstances. Can't efficiently and quickly develop/iterate on something you cannot see immediately (especially if it's nested somewhere deep in the user flow).

0

u/StartComplete Compose Hater Mar 07 '24

Love it bro. Keep it up and ignore the compose lovers.

-5

u/jarjoura Mar 07 '24

Only comment I’ll add, android view initialization performance is highly tuned for XML and is actually worse when done in code.

Jetpack Compose uses its own view system to avoid any performance penalties there, so something to keep in mind as you build out your library.

17

u/Dimezis Mar 07 '24

android view initialization performance is highly tuned for XML and is actually worse when done in code

Any proof for that claim? The XML needs to be parsed and corresponding views "inflated" via reflection, calling (almost) the same constructors you would call manually. So it sounds highly unlikely that XML could be faster.

10

u/romainguy Android Mar 07 '24

Note that it's not actual XML that's parsed, but a binary version of XML which helps speed up parsing. It won't be faster than creating Views directly though.

13

u/Zhuinden Mar 07 '24

Only comment I’ll add, android view initialization performance is highly tuned for XML and is actually worse when done in code.

Citation needed, the layout inflater literally calls the same constructors.

3

u/Still_Potential_8043 Mar 08 '24

As we know, Jetpack Compose didn't avoid performance issues, but only multiplied them))

1

u/Zhuinden Mar 08 '24

Beware, you know too much lol