Reusable component in Android
android view kotlin Estimated reading time: 4 minutesReuse - the biggest advance. Reuse ask us to make the stuff in a more generic, abstract way. Thanks to it, we think better, wider and we try to think about all cases (even the rare ones).
The best way to learn something - is to try. I already mentioned, that before trying something, I like to make my theory base as large as possible. This time, I want to create a view, that can be reused across all the apps - CirularProgress
.
Yes, very simple component. I choose it because I don’t want to spend a lot of time on the component itself, instead - I would like to spend time on the “reuse” idea.
intro
The problem is trivial - I want to create a view, that contains some logic and can be configured and reused in the app in a few places.
My main background - is iOS, and reusing it is quite easy to do. Android, instead, has a bit more components that need to be created/managed before this option becomes available.
Before doing something, it’s good to have an idea about a few folders in android studio and components that it can hold:
- res/drawable - is a general abstraction for “something that can be drawn.” (according to official doc).
These resources can be used for a few things:
- draw images (from png, SVG, etc)
- draw XML
- draw shapes
- draw layers
- draw state
- scale
- res/values - used to store the values for the resources that are used in many Android projects including features of color, styles, dimensions, etc. In other words - XML files that contain simple values, such as strings, integers, and colors. It can hold:
- colors
- dimensions
- strings
- styles
- attributes
- etc
This is the interesting part of the current task (reusable view). But of cause its better to be familiar with other folders and their purpose (font, XML, etc).
solution
Now, when all the needed background is in place, we can start.
The very first thing that we need - is to define the layer, that can be drawed and shown to the user. To do so - we should create a new drawable resource: right click on folder » New » Drawable Resource File. Let’s name it drawable/cc_progress_bar_circular_determinative.xml
(or any name u like).
I didn’t introduce saving in name length - nowadays we can use a very descriptive name at almost 0 costs.
The content of this file:
layer-list allows using Z-order of objects - when they are drawn on top of another. This is often a great way to organize u’r drawing
This will draw for our progress - we grab color, use it for painting shape (ring), then rotate this shape, making the starting point of drawing at the very top.
As u can see, it’s very easy to read such XML from the inner element to the outer
Next step: create the view layout. For this purpose we should use layout folder. Let’s create a new layout with name circular_progress.xml
. The content - its just a centered progress bar with our drawable source:
The one of the key moment here -
android:progressDrawable="@drawable/cc_progress_bar_circular_determinative"
. This attribute tells the android that it should use our drawable source.
The next step is not always mandatory, but in most cases, it is - allows to use of the custom property as an attribute for XML. To do so we should declare a custom style in /res/values/attrs.xml file (we may need to create one if it does not exist in the project, remember, this file, as mentioned above, can have multiply styles):
U can see the declared style CircularProgressLayout
with the attribute progress
.
And to complete the process, we must to create a component class CircularProgressLayout.kt
:
Then, the usage:
Personally, for me, I see a few downsides here, especially mutability - to change something defined in a drawable source, u need a lot of actions, also u have a layout.
To change color of progress I use this code:
Imagine that I need to have an inactive background or round ends of the progressLine.
Of cause, we have another option - draw everything in code with bigger customizability. But this is a bit another story.
result
I did this component for use in another component - totp code generator. The final result is here:
resources
Share on: