r/androiddev • • Jan 08 '20

How did Telegram implement this theme change effect ? I know I can look at the code but needed the gist of the implementation. It animates the status bar color change as well! I'm fairly impressed.

Post image
216 Upvotes

73 comments sorted by

23

u/AD-LB Jan 08 '20

Probably using some reveal-animation. I think this might help:

https://stackoverflow.com/q/27872324/878126

Or this:

https://android.jlelse.eu/a-little-thing-that-matter-how-to-reveal-an-activity-with-circular-revelation-d94f9bfcae28

Please let me know if you succeed with one of those. Maybe I should use it too.

34

u/DrKLO Jan 08 '20
  1. App draws all UI in Bitmap and setting it inside ImageView.

  2. Changing all elements colors

  3. Reveal animator between ImageView and interface

No idea is it possible or not without all custom UI and custom themes not defined in styles.xml.

7

u/forever_ok Jan 09 '20

Are you really the single Android developer of the Telegram? Nice job!

20

u/Quinny898 Jan 08 '20

So I actually did something similar in one of my apps, DarQ, with this effect. It uses two animations at the same time, both created using createCircularReveal, one on the app content and one on the toolbar. I draw behind the system bars and manually handle the padding to allow it to go behind the status and navigation bars.

As DarQ is open source, you can see the quite messy code of how it works here (it's pretty hacky)

4

u/[deleted] Jan 08 '20

Two views overlayed then masked? To provide the resulting layout... Honestly no idea

5

u/Yikings-654points Jan 08 '20

That's how NSA hides code.

3

u/brookmg Jan 08 '20

I like this 🤣, which dev would read 16k lines of code ( in a single file )

2

u/tinyzord Jan 08 '20

I have been exactly there a few years ago actually... "Telegram is amazing! I should check the code to see how they did this!"

Followed shortly by the realization that one guy just wrote a big pile of unreadable code...

2

u/tttzof351 Jan 09 '20
  1. Get bitmap from root view

  2. Set bitmap from 1. to front of your app

  3. Change activity theme

  4. Start circular reveal animation for hide front bitmap

1

u/Unlucky_Ferret1723 Jul 19 '22

what about status and navigation bar?

2

u/[deleted] Jan 08 '20 edited Jan 08 '20

You can combine circular reveal and color animation to show fragments or activities, like this

https://github.com/enricocid/Reveal-fragment-example

Status bar is translucent when navigation view is expanded, I don't think its color is changed

1

u/brookmg Jan 08 '20 edited Jan 09 '20

This seems plausible, recreate the activity with some style change. Because activities are way more heavy and would be hard to pull that kind of smooth UX

2

u/[deleted] Jan 09 '20

Yeah, in fact I'm applying to a fragment. Circular reveal on activity is not that smooth. :) I'm thinking to apply circular reveal also when theming is applied (on settings fragment), like the post's image.

1

u/tuandao_cp Jan 09 '20

I am working on open source telegram, I feel that code is very hard readable. When I need to change UI or features, I catch problems a lot, they handle "big hand". Fragment, lifecycle or even textview is customview :(

1

u/rezaiyan Jan 12 '20

Don't think it is complex, It takes a screenshot of the screen and shows on the screen and does a reveal animation on it and meanwhile he changes the theme of activity.

The following image shows you what happens.

Imgur

-16

u/ArmoredPancake Jan 08 '20

It's open source, check yourself:

https://github.com/DrKLO/Telegram

8

u/blenda220 Jan 08 '20

"I know I can look at the code, just needed a gist of the implementation"

-10

u/AAZZAAMM Jan 08 '20

I think you can find the code for it on GitHub. They were open source when they started. Lol WhatsApp can't even fix a dark theme 😂

0

u/[deleted] Jan 08 '20

[deleted]

11

u/[deleted] Jan 09 '20

That would be expensive. Pasting the code link for reference (which someone else posted in a comment): https://github.com/DrKLO/Telegram/blob/31736964fa12b5981632ecfd75d5f6d4f91f7135/TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java#L3159

It is rendering the old view in an ImageView first and changing the theme (by manually changing the colors of all the views, not styles.xml) and doing a circular reveal.

1

u/DonUdo Jan 09 '20

so this coudn't be used with a more regular approach to dark/light theme?

2

u/[deleted] Jan 09 '20

Yup. That would need activity recreation and the screen would go blank for some time. We could create a new activity but I think that would be too slow.

-1

u/Mavamaarten Jan 09 '20

That file is 4k+ lines. Yikes.

2

u/Amagi82 Android Developer Jan 09 '20

Think that's bad, have a look at View.java

21

u/altair8800 Jan 09 '20

The fact that my Huawei Mate 20 Pro has a little bit of jank during this transition supports this hypothesis.

0

u/[deleted] Jan 08 '20

[deleted]

89

u/[deleted] Jan 08 '20

44

u/kaadste Jan 08 '20

Jesus, more than 100 lines of imports. Scrolled to line 300 and still didn't see onCreate function. Is the UI team doing all the programming too?

4

u/[deleted] Jan 09 '20

They are not even using layout xmls. All UI seems to be programmatically created.

3

u/alashow Jan 09 '20

Which works faster

3

u/[deleted] Jan 09 '20

But the code is harder to navigate

2

u/itsJoKr Jan 09 '20

The app started in 2013. The state of Android programming was awful back then.

1

u/TheBCX Jan 09 '20

Thats a good thing, once you create views programmatically, you never want to go back to XMLs :D

11

u/[deleted] Jan 09 '20

Legend says that if you pray for Jake Wharton's blessings every day for a month, that code starts to make sense.

1

u/lillgreen Jan 09 '20

Damn that's a name I haven't read in a long time. Still remember the first time I came across action bar sherlock, couldn't believe that shit was running on gingerbread.

3

u/[deleted] Jan 09 '20

Jake is literally a God in the Android community. He even hangs around here quite often on this subreddit.

2

u/average_dota Jan 09 '20

Holy hell, my kilowatt power supply fan kicked on because my CPU started sucking so much juice while that page loaded.

1

u/octothorpe_rekt Feb 06 '20

I have very limited experience as an Android Developer and I'm just poking around. But seriously why the hell not just import org.telegram.messenger.*;, import android.text.*;, etc for the packages you're going to use extensively? It's functionally no different after compilation, right?

0

u/[deleted] Jan 08 '20

[deleted]

22

u/kaadste Jan 08 '20

This thing just gets better and better

10

u/lomoeffect Jan 08 '20

Not that lines of code is a measure of anything particularly interesting but this makes me feel good about my attempts to move to from a god class of 1.5k lines to MVVM.

4

u/sunilson Jan 09 '20

BaseFragment isn't even a Fragment :D

1

u/v123l Jan 10 '20

Everything is a custom view...

39

u/ElladanValdamir Jan 08 '20

That URL actually killed my chrome on Android.

11

u/theinvisiblesquid Jan 08 '20

Use Firefox then

0

u/[deleted] Jan 09 '20

[deleted]

1

u/DonUdo Jan 09 '20

i am currently, but growing tired of firefoxs inability/unwillingness to open urls in apps directly (like chrome and other browsers do). Having to wait for a page to load an then pressing that stupid button just to have it open in an app and wait there again is really annoying.

2

u/crazytosser00 Jan 13 '20

Just stop page loading manually, and firefox will show you that button. I do it all the time and it works

1

u/DonUdo Jan 13 '20

Good idea, I just don't understand why it cant do that by itself like chrome and the others. Is it something hidden so only Google products can use it? At least let it be an option

0

u/[deleted] Jan 08 '20

[deleted]

9

u/omniuni Jan 09 '20

I prefer not to have my information directly sold, thanks.

0

u/[deleted] Jan 08 '20

[deleted]

7

u/tomfella Agency dev Jan 09 '20

ChatActivity extends BaseFragment

22

u/DrSheldonLCooperPhD Jan 08 '20

The code on the other hand...

Proof users care about UX and not architecture.

57

u/zunjae Jan 08 '20

Why the fuck would the end user care about software architecture?

31

u/forever_ok Jan 08 '20

It's not that someone think that end user will care about architecture, it's just some developers care about it so much that they forget about the reason why apps exist.

8

u/DrSheldonLCooperPhD Jan 08 '20

Thanks and that's what I meant.

7

u/frankandsteinatlaw Jan 08 '20

There are over 16,000 lines in this file. Why? Is this like a “built” file?

5

u/tj-horner Jan 08 '20

No, this is the source

26

u/frankandsteinatlaw Jan 08 '20

The source of misery amirite??

4

u/grishkaa Jan 08 '20

Wait until you see TLRPC

0

u/nhoxbypass Jan 09 '20

I'm digging these files for a month (also TLRPC.cpp) just to figure out how Telegram's messages works. But still don't understand...

2

u/drabred Jan 09 '20

That's what you call job security.

1

u/nifhel Jan 09 '20

They obfuscate the code by making it unusable in real life but still readable enough so they can claim to be open source.

0

u/dark_mode_everything Jan 08 '20

Is that activity the entire app?

0

u/bartekpacia Jan 09 '20

I clicked the link and Chrome on my S9 literally freeded. Had to kill the process. Shit.

0

u/[deleted] Jan 08 '20

[deleted]

7

u/campidoctor Jan 08 '20

That's because the code was made by just one guy.

10

u/[deleted] Jan 09 '20

One guy and a lot of Vodka.

3

u/_ALH_ Jan 09 '20

Not really an excuse for bad architecture.

1

u/ikakus Jan 08 '20

I guess is either fake open-source or obfuscated somehow. There is no chance team that can provide that level of app UI, features and stability, will work with this code.

17

u/Maxr1998 Niagara Launcher co-developer, Jellyfin Android Jan 08 '20

One might think that, but: https://core.telegram.org/reproducible-builds

Obviously you don't get 100% matching MD5 sums, but apart from signature and API keys, the Play Store release should match with that source..

0

u/[deleted] Jan 08 '20

[deleted]

21

u/Tusen_Takk Jan 08 '20

Their only tools are hard liquor and a growing desire for death

0

u/[deleted] Jan 09 '20

[deleted]

1

u/ikakus Jan 09 '20

I know, I've even listened to podcasts with him. And for me as an Android developer, this code makes absolutely no sense. I mean there is no reason to have 16k line class. If this guy is evil genius and can comfortably work with this code. This project is screwed, if he leaves, there is no one who will replace him. And all the team in telegram should understand it. So I still think this repo is an onfuscation, very good one.

17

u/[deleted] Jan 08 '20

Yeah. Telegrams quality makes literally all the other messaging apps look like a joke.

2

u/HopefulAssistance Jan 09 '20

Me: "This app works so smoothly, I'm sure they have a very beautiful architecture to back this up... "

*ChatActivity*

Me: "Alright... we should call an exorcist"