Sitemap
YavarTechWorks

Today a Reader, Tomorrow a Leader

Android-App Development — Part_1

--

Using Kotlin

Press enter or click to view image in full size

Android development using Kotlin involves creating mobile applications for Android devices. Using the Android Studio IDE, you design user interfaces with XML layout files, and each screen is represented by an activity, which corresponds to a Kotlin class. Kotlin’s concise syntax and interoperability with existing Java code make it a popular choice.

Press enter or click to view image in full size

Android:

Android was developed by the Open Handset Alliance, led by Google, and other companies. Android offers a unified approach to application development for mobile devices which means developers need only develop for Android, and their applications should be able to run on different devices powered by Android.

Kotlin:

Kotlin is an open-source, statically typed programming language that supports both object-oriented and functional programming. Kotlin provides similar syntax and concepts from other languages, including C#, Java, and Scala, among many others. Kotlin does not aim to be unique — instead, it draws inspiration from decades of language development. It exists in variants that target the JVM (Kotlin/JVM), JavaScript (Kotlin/JS), and native code (Kotlin/Native).

Certain Android APIs, like Android KTX, are Kotlin-specific, but most are written in Java and can be called from either Java or Kotlin. Kotlin’s interoperability with Java is core to its growth. It means you can call into Java code from Kotlin and vice-versa, leveraging your existing Java libraries.

Java:

JAVA is a programming language that is used in Android App Development. It is class-based and object-oriented programming whose syntax is influenced by C++. The primary goals of JAVA are to be simple, object-oriented, robust, secure, and high-level.

Let’s Get — Started 🏁

The very first step is to install Android Studio on our machine. To install the studio, there are some system requirements, let’s look into it…

System Requirements:

For WindowsOS:

Press enter or click to view image in full size

For MacOS:

Press enter or click to view image in full size

For LinuxOS:

Press enter or click to view image in full size

Installation:

  • To Install the Android Studio, Click the 👉🏻 Android Studio to download and install.
  • Once we click the link given above, we’ll be redirected to the Android Studio Official Download page, We need to click the Download Android Studio🔻 option, accept their Terms and Conditions, and download the Studio.

Setup Studio:

Once we install Android Studio, then open it. For the very first time it took time to open, because the libraries and packages are need to by download, “the internet connections is required”.

After sometime, the studio will show three options:

  1. New Project
  2. Open
  3. Get from VCS

From these select a new project and choose the device we need, we need to select: the “Phone and Tablet” option.

Get Ruban Raghavendar K’s stories in your inbox

Join Medium for free to get updates from this writer.

There are multiple Activity templates; from those, we need to select “Empty Activity”. And click “Next”.

Press enter or click to view image in full size

Set the project name and project location, wherever we need put our project. And click finish to create project.

Press enter or click to view image in full size

Folder Structure:

  • Manifests — Let’s Start with the “manifests” folder, the manifests folder container, and the AndroidManifest.XML file. This is a very important file. Every Android app has an “AndroidManifest.XML” file. This file contains some information, about how to run the app.
  • This XML file acts as an intermediary between the Android OS and our Application.
  • When the user taps on the app icon to launch the application, the Android operating system first reaches the AndroidManifest.XML file
  • By using the information in AndroidManifest.XML file, the system decides what to do next? and what activity to open first.
  • The Manifest file contains details about all the activities.
  • In that, there we’ve an intent filters, for example:
  • This intent says that this is a Launch activity.
  • Using this information, Android OS decides to launch this main activity first.

Create Activity:

For creating an activity, go File → New → Activity → Empty Views Activity.

Press enter or click to view image in full size
  • After that, we need to give our activity and click finish.
Press enter or click to view image in full size
  • And also it’s mainly used for setting permissions in our application, if our application uses any REST-APIs, then the internet is necessary, For that, we need to set the permissions here.
<uses-permission android:name="android.permission.INTERNET" />
  • Java — Look into Java a directory, the Java folder contains all the source code files we create during the development.
  • The Java folder contains three subfolders, the first one contains the source code files. The next two directories are to be used for test and Android test directories are to store test files.
  • We use this directory to create test cases, normal unit test cases.
  • Java-Generated — We can see another Java folder, which is a generated one. This folder contains generated classes.
  • All the time, Android Studio will generate a lot of classes for us to make our life easier whenever we use supporting libraries like Chrome and Retrofit to save time making our work easier they generate a lot of files for us that generated files will be stored inside this generator java folder.
  • We may have a question, We’re using Kotlin but why there are Java files? Because Kotlin language has been created on top of Java language, on top of Java libraries. Kotlin is a more advanced and modern version of Java.
  • Res — we have res a folder, this res folder contains all non-code resources. This folder does not contain Kotlin files but all the other related resource files of the app, such as XML layout files, String resources, and Bitmap Images.
  • This folder is divided into four subfolders named drawable, layout, mipmap, and values.
  • Drawable — The folder called drawable contains all the different types of images we used for the development of the application, every time we add a new image file, we add it to the drawable folder. By default, we’ve two XML files. We can also add any formats to it such as .jpg, .png, .jpeg, and more.
  • And then we have layout folder. The layout folder contains all the XML layout files.
  • For the mainactivity.kt file, the Kotlin class, we have a corresponding layout named activity main. So the layout file is stored in the layout folder.
  • The layout files create the view or a screen that contains all the components like buttons, text, views dropdown menus, and everything for the activities created in the Java folder.
  • These are called view controllers, the activity_main.xml is view, and the MainActivity.kt is a controller of that view.
  • Minmap — The minmap contains icons in different sizes, like hdpi, mdpi, xhdpi, xxhdpi, xxxhdpi, anydpi.
  • In our market, there are different sizes of mobile devices, and for that these are the different sizes of icons.
  • Values — The values contain the Strings, Colors, and Themes of our project in the XML format.
  • Whenever we use colors or strings in our app, we do not need to type the exact string or hex code there. Instead of that, we can use the names.
// colors.xml
<resources>
<color name="purple_200">#FFBB86FC</color>
</resources>
when we're creating application, all the colors should be inside this colors.xml file.
whenever and wherever we need this, we just call the name to use it.


// strings.xml
<resources>
<string name="app_name">My Application</string>
</resources>
when we're creating application, same like colors, all the strings should be inside this strings.xml file.
whenever and wherever we need this, we just call the name to use it.
  • In the future, if we need to change or rewrite something, we no need to change in all the places, we just change in these xml files.
  • Themes —This folder contains the details about the theme of our application.
  • We can change the default primary and secondary colors and we can also insert our custom theme xml files.
  • Gradle-Scripts —These are very important, gradle is an automated build system. So because of the gradle, we don’t have to do the configuration manually. We don’t have to download libraries put them in different folders and write our own configurations. Gradle automatically does all that stuff.
  • There are two build.gradle files, one is Project Level and another one is App Level gradle file. Most of the time we use these two files.
  • Especially in the App Level gradle file, whenever we use plugins or dependencies, we need to define it here.
  • In the Android studio, if we select an activity xml file, we can see three modes: 1. Code, Split, and Design. By the coding mode, we can see only the code alone, by the design mode we can only see the design of the code, by the split mode we can see both the code and designs.

Run:

  • To run our application we’ve multiple ways, The best way to run our app is to click the ▶️ play button.
  • In the top menus, there is an Run option, in that, there is an option called Run App, this is another way to run our application.
  • Or by clicking Shift + F10 to run the app.
  • For the very first time, it took time to run, because the first time, it’ll build the app from scratch but the next time, it won’t take that much time.
  • We can run our application in an Android Emulator or we can our mobile device externally using the USB Debugging option.
  • I recommend using USB Debugging with our mobile device, it is more fastest debugging option.
  • In the layouts, the activity_main.xml contains some default codes for the “Hello World!” text.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.181" />

</androidx.constraintlayout.widget.ConstraintLayout>
  • We can design the screen in two modes, 1. Code mode and Design mode. In the code mode, we need to type the xml code, but the Android Studio presents the design mode to ease our work.
  • While in design mode, it’ll look like this:
Press enter or click to view image in full size
  • On the right side of the design, we can change the attribute values to make our design.
  • On the left side, there is a thing called Palette, which helps us to insert the various view components like buttons, ImageView, Switch, etc.

Thank you so much for spending your time reading our blog patiently😍

Let’s see in the next part of this series, until then Signing off:

Ruban❤️

--

--