Codetown ::: a software developer's community
As I near the end of the first season of Kotlin Thursdays I wanted to introduce another fun project I wanted to say thanks for sticking with me as I overhauled my life to the other side of the country.
I decided I may run these Kotlin Thursdays in seasonal bursts. I really enjoy writing these. I will continue to write, I'd like to grow topics with a team and include episodes starting late October. Not only does it reduce my weekly workload, but I can now plan for better tutorials while simultaneously expanding content.
This week, we're going to dive into a time years ago long ago when I matched with an Avocado on Tinder, who faithfully sent his avocado truth.
I will now share a years worth of avocado facts made up by someone named Avocado while we simultaneously learn about Android. As a framework, it can be really overwhelming to look at when you're just trying to get the basics down. I always say stick to the documentation as closely as possible and be weary of what you read on answers from stackoverflow or other sites - it will save you tears down the road, trust me. That's not to say stackoverflow doesn't give good tricks or that there isn't value to stackoverflow; some answers are not always reliable, but as long as you're armed with documentation, you'll be able to leverage both powerfully.
(That's just my how I felt about learning Android, but feel free to share your experiences below)
This week is an introduction to Android development with Kotlin as we create an over-glorified random fact generator. Why Kotlin and Android? Google declared Kotlin as the official language on Android in 2017. It's expressive, concise, and powerful. Best of all, it's interoperable with our existing Android languages and runtime.
These projects are always available on Github, which I recommend looking at to compare to your project.
Gradle is the other side of build automation systems (whereas the other is Maven, as used in TornadoFX earlier). Gradle is open-source and builds upon Apache Ant and introduces a Groovy-based domain-specific language (DSL) instead of the XML form used by Apache Maven for declaring project configurations.
Android Studio offers Gradle actions to be executed at the top in Build, where you can execute actions like:
./gradlew build
./gradlew clean
./gradlew clean build
which is really helpful when you need to attach the --info
flag to understand why your gradle.build may not be compiling. In the below example, Gradle didn't like my json file.Android Studio will help you manage your Gradle file, so be sure your files are in sync.
It is not necessary for you to possess an Android device to be able to run an Android environment. You could always run the application on your personal Android, which tends to run significantly faster, but if you're like me and have no room for ports I'm bound to use a virtual environment. The upper right bar contains an AVD for your use.
You will need to ensure the virtual device you use matches your SDK. When adding a device to the AVD, I went with Nexus 5 running on SDK 26 as the minimum, which may require a download the first time for the software itself.
When you hit 'play' your gradle script will build and compile, and the project will run on the device of your choosing. Once you have created your project, you can quickly spin up an emulator for testing out your application.
Android Studio is the official integrated development environment (IDE) for Google's Android OS, but on JetBrains' IntelliJ IDEA software and designed for the Android platform. Before Android Studio, we had Eclipse for Android development, but as its open source, the platform is free to download.
When you open a new project, call you application however you want. I named my Avocado Facts. For your first activity, just mark it a blank activity. Upon choosing the SDK, you'll want to make sure it's newer for Android able to use conveniently-supported features, but not so new that you are unable run your application on a majority of devices.
Project Studio
Let's take a minute to go through some bells and whistles that make Android Studio more than just a fancy text editor:
In the top left corner, your default may be set to Android instead of Project. The Android view displays the project files for the quickest access to your development files, but you're welcome to choose the editor of your preference. Project reveals all available files in your Android Project.
Layouts
Layouts in Android are comprised of XML files, or eXtensible Markup Language. Android Studio allows you to use either their Design GUI or Text editor located in the lower left corner of the file view.
The right panel displays attributes for a given selected component. It's one way to edit attributes for a certain component.
Note: This activity has been set for NoActionBar in styles.xml.
It's up to you which is your preference for certain situations. I find both editors have its advantages over the other. I will demonstrate how you can use either. Generally, a good practice for UI is to keep your Viewgroups as shallow as possible per page.
Many attributes have GUIs for their custom options as well, such as choosing a color for your text.
The key is to start exploring and find what you like!
Inserting a title with Design view
Inside the Palette in the lefthand side of file editor, components are split into groups of view types to make them easier to find. Be sure to have the Component Tree open for ease of navigation. Feel free to minimize the project folder for maximum screen use for this drag-and-drop feature. All components are specialized views.
Inserting a title with Text view
One perk of using the textual view compared to the Design view is the ease of exerting control over the attributes you wish to affect. The IDE will be really helpful in guiding your best practices - follow their suggestions for cleaner code throughout. To auto-complete possible tools you might be able to use (either in .kt, .xml, .java, and more), press ctrl + [space]. Likewise, your classes will want to import its sources into your files: using alt + [enter] or clicking the yellow lightbulb will apply the IDE's suggestions.
For string values, be sure to use component ids as a good practice. The IDE will help you properly set it up in your strings.xml file.
Here, you might notice a RelativeLayout being used as the base for our main activity: such a layout is called a ViewGroup, which is a special type of view that may hold other views. To keep my text boxes aligned evenly, I mark their width with match_parent, which it stretches to that size of the parent view, and add padding to the left and right only as dictated by android:paddingHorizontal="50dp"
.
Types of ViewGroups
I also had inserted an image and another text for the main fact itself. To add an image, you need to add an image to your res/drawable folder. If you happen to load local images that are high quality, you'll want to ensure the size of the file is not so large the memory cannot run at runtime.
When you compile your app, each XML layout is compiled into a view resource. You load the layout resource from your app code in your Activity.onCreate(...)
callback implementation. By calling setContentView(...)
which tells the activity which layout file you want to use.
class MainActivity: AppCompatActivity( ) {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Assign the Views from the layout
val factTextView = findViewById(R.id.avocadoFact)
}
}
Again, you can use ctrl + [space] to give suggestions to available library components.
findViewById(...)
requires an id referenced as a generated resource class. When we build our project, Android automatically builds a class for us called R (resources). You can view into this generated class with app > build > generated > source > R > debug > [package.name] > R if you're curious to see where the R generates.
You can create a random number generator to generate a random number in a give range. After doing so, we connect button logic to a pool of randomly generated facts. Using onClickListener{ ... }
, our button lists for the element to be clicked or tapped. When the listener detects a click, then it runs the code given inside the function.
class MainActivity: AppCompatActivity( ) {
private val facts = arrayOf(
. . . // Array of Strings
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Assign the Views from the layout
val factTextView = findViewById(R.id.avocadoFact) as TextView
val moarButton = findViewById(R.id.more) as Button
moatButton!!. setOnClickListener {
val randomGenerator =Random( )
val randomGenerator =randomGenerator.nextInt(10)
val fact =facts[randomNumber]
// Update the screen with new avocado fact
factTextView!!.text = facts[randomNumber]
}
}
}
On the action call, the 'random number generator' will call an arbitrary number from 1 to 10 and one of 10 facts semi-randomly and set the textview accordingly.
That's the basics of Android and Android Studio - I hope you found new tips to take advantage of Kotlin and certain IDE features that make coding Android a fun task. As always, there's so much more that can build on this! Please don't hesitate to share any you may have found, or share the ideas you could take with this project?
I'd love to set up a JSON online for the app to read, and then Avocado Guy can post his avocado wisdom on an app for all to see. Crossing my fingers he answers back.
Tags:
Codetown is a social network. It's got blogs, forums, groups, personal pages and more! You might think of Codetown as a funky camper van with lots of compartments for your stuff and a great multimedia system, too! Best of all, Codetown has room for all of your friends.
Created by Michael Levin Dec 18, 2008 at 6:56pm. Last updated by Michael Levin May 4, 2018.
Check out the Codetown Jobs group.
AWS has launched the open-source Model Context Protocol (MCP) Servers, revolutionizing AI-powered code assistants. These servers enhance development speed and security, ensuring adherence to AWS best practices. With features like automated Infrastructure as Code and cost insights, MCP democratizes AWS expertise and empowers developers to optimize cloud solutions effortlessly.
By Steef-Jan WiggersAWS has recently announced the general availability of Amazon VPC Route Server. This new option simplifies dynamic routing in a VPC, allowing developers to advertise routing information via Border Gateway Protocol (BGP) from virtual appliances and dynamically update the VPC route tables associated with subnets and internet gateways.
By Renato LosioIan Hoffman discusses Slack's architectural evolution from workspace-centric to Unified Grid. He explains scaling challenges & Enterprise Grid complexities, and shares lessons learned during this significant architectural shift, drawing insightful parallels to the history of astronomy and emphasizing the importance of questioning foundational assumptions in software development.
By Ian HoffmanIn this podcast, Shane Hastie, Lead Editor for Culture & Methods, spoke with Trisha Gee about the challenges and importance of addressing flaky tests, their impact on developer productivity and morale, best practices for testing, and broader concepts of measuring and improving developer productivity.
By Trisha GeeStefania Chaplin and Azhir Mahmood explain how to navigate the complexities of AI in highly regulated industries. They discuss MLOps pipelines, data security, evolving legislation (GDPR, EU AI Act), and the critical frameworks for responsible, secure, and explainable AI. Learn practical prevention techniques, XAI methods, and future trends in AI for cybersecurity and beyond.
By Stefania Chaplin, Azhir Mahmood
© 2025 Created by Michael Levin.
Powered by