Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
February 28, 2023 01:44 am

Getting Started With Android Studio


Android Studio has come a long way since its first stable build was released in 2014. It is completely free to use and has long been the official and recommended tool to develop apps for Android devices.


Android Studio is a huge and complicated application which can be intimidating for beginners. I have already written some tutorials on this topic for beginners such as this tutorial on Working with Android Studio which covers the download, install and updates of the application as well as a brief overview of the user interface and what different tool windows do. Anyone who has just started using Android Studio will also benefit from the tutorial on Managing Virtual and Physical Devices in Android Studio.


In this tutorial, I will focus on some other aspects of Android studio such as different configuration options and the steps to follow to create, develop and run an android app.


Android Studio Welcome Screen


Android Studio greets you with a welcome screen when you open it for the first time. After the first time, it directly takes you to any open project. However, you can also open the welcome screen yourself by closing the current project by navigating to File > Close Project.


Once you have downloaded and installed Android Studio, click on the launch icon and you should see something similar to the image below.


Android Studio Welcome Screen Recent ProjectsAndroid Studio Welcome Screen Recent ProjectsAndroid Studio Welcome Screen Recent Projects

The pane on the right shows a list of recent projects in my case, it will look a bit different if you have never created a project in Android Studio before.


Organizing Projects in Groups


You can organize your projects into groups by right clicking in the right window pane and then selecting New Project Group as highlighted. I have created a new project group titled Tutorials in this case.


Android Studio New Project GroupAndroid Studio New Project GroupAndroid Studio New Project Group

You can now click on any of the recent projects and then navigate to Move to Group > Tutorials. All those projects will then be available under Tutorials.


Changing the Appearance of Android Studio


It is also possible to change the theme and font size etc. in Android Studio right form the welcome screen by heading over to Customize. There are three color themes available for you to use by default.


The light theme is called IntelliJ Light and the dark theme is called Darcula. There is also a theme called High Contrast that improves the contrast between different UI elements for better visibility. You can see it applied to Android Studio in the image below.


It is also possible to set a font size for the UI elements in the IDE. The default font size is 12. However, you can also select any other predefined value or set one of your own. I have set my font size to 14.


Android Studio CustomizationAndroid Studio CustomizationAndroid Studio Customization

You can also check the box for adjusting colors for better visibility for red-green vision deficiency. Clicking on the All Settings link will take you to a new window where you can configure Android Studio to behave and look exactly the way you want.


Installing Plugins and Themes


Not everyone will be satisfied with just the three themes that Android Studio offers by default. Also, it is impractical to expect Android Studio to have all the functionality that you need.


This is why the marketplace in Android Studio is a great way to find new themes as well as plugins to enhance the built-in functionality of the IDE. You can access the marketplace by clicking on Plugins on the welcome screen.


Android Studio MarketplaceAndroid Studio MarketplaceAndroid Studio Marketplace

There are two tabs on the plugins screen. You can use the Marketplace tab to find the plugins and themes that you want to install. The Installed tab shows all the currently installed plugins. At the top of the list are plugins that you downloaded and installed from the marketplace.


Android Studio Installed PluginsAndroid Studio Installed PluginsAndroid Studio Installed Plugins

As you can see, I have installed Dart, Flutter, Gerry Themes and Key Promoter X. You can also disable these plugins individually if you like.


Creating a New Project


You can create a new project by clicking on the New Project button at the top of the welcome screen as shown in the image below.


Android Studio New ProjectAndroid Studio New ProjectAndroid Studio New Project

Once you click the New Project button, you will be taken to the next screen where you need to pick an activity template for your app. I will go ahead with an Empty Activity. Activities are an important component of an Android app and a single app can contain multiple activities.


Android Studio Empty ActivityAndroid Studio Empty ActivityAndroid Studio Empty Activity

You can click on the Next button after selecting the Empty Activity. You will be asked some information about your new app on the next page. Fill out a Name that you like. The name can be something common like Alarm. I have set mine to My Empty Activity. Fill out a Package Name which has to be unique and different from all other apps on Play Store. Using your domain name as part of the package name can help keep it unique.


New Project DetailsNew Project DetailsNew Project Details

I have set the Language to Kotlin. It is now the preferred language for android development and allows you to write safer code quickly. Choose a Minimum SDK version that you want to support. Android Studio will gives you a rough indication of supporting devices for the specified SDK version.


Finally click the Finish button and Android Studio will set up the project for you. This can take a while so please be patient.


Understanding How it All Works Together


At this point, you will see a couple of files in the Android Studio editor window called activity_main.xml and MainActivity.kt. The activity_main.xml file is used to define the layout of your activity while the MainActivity.kt file is used to define the behavior of the activity.


The code inside activity_main.xml file will look something like this:











































































1
<?xml version="1.0" encoding="utf-8"?>
2
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="https://schemas.android.com/apk/res/android"
3
    xmlns:app="http://schemas.android.com/apk/res-auto"
4
    xmlns:tools="http://schemas.android.com/tools"
5
    android:layout_width="match_parent"
6
    android:layout_height="match_parent"
7
    tools:context=".MainActivity">
8

9
    <TextView
10
        android:layout_width="wrap_content"
11
        android:layout_height="wrap_content"
12
        android:text="Hello World!"
13
        app:layout_constraintBottom_toBottomOf="parent"
14
        app:layout_constraintEnd_toEndOf="parent"
15
        app:layout_constraintStart_toStartOf="parent"
16
        app:layout_constraintTop_toTopOf="parent" />
17

18
</androidx.constraintlayout.widget.ConstraintLayout>

We are using ConstraintLayout to lay out our views or elements on the activity screen. This type of layout allows us to place different views or elements with respect to some other view or parent layout based on a constraint. The above snippet simply adds a TextView that contains the text Hello World! to our layout.


There are three ways in which you can work with the layout of an activity in the Layout Editor of Android Studio. You can use the Code view if you like working directly with XML. You can also use the Design view if you like to create the layout using drag-and-drop. The Split view will give you access to both the XML editor as well as the Visual Design Editor.


I personally prefer the split view which looks like the image below.


Android Studio Split ViewAndroid Studio Split ViewAndroid Studio Split View

The behavior of the activity will be controlled by the MainActivity.kt file which contains the following code:















































1
package com.tutsplus.myemptyactivity
2

3
import androidx.appcompat.app.AppCompatActivity
4
import android.os.Bundle
5

6
class MainActivity : AppCompatActivity() {
7
    override fun onCreate(savedInstanceState: Bundle?) {
8
        super.onCreate(savedInstanceState)
9
        setContentView(R.layout.activity_main)
10
    }
11
}

Here, we are overriding the onCreate() method to set the activity content from a layout resource with the help of the setContentView() method. The onCreate() method is called when the activity is first initialized. We also have another tutorial which covers the Android Activity Lifecycle in more detail. You can read it to learn more about different lifecycle methods like onCreate() and to learn how to start activities explicitly.


This particular app doesn't do anything interesting at this point. However, it is possible for you to add new views or elements to your activity layout such as a text input field and a button etc. After that, you could write some code inside MainActivity.kt in order to respond to user actions.


Another one of my tutorials titled Building Your First Android Application does exactly this and helps users greet themselves instead of the whole world by entering their name and clicking the Greet Me button.


Running the Android App


Android is a very popular operating system that runs on all kinds of devices from smartphones to watches and TVs. It is very important that you test your app on as many devices as possible to make sure that it is working as expected for everyone.


Android Studio does an excellent job of providing you with all the necessary tools to thoroughly test your app. You can either run your app on connected physical devices or on an Android Virtual Device also called AVD. The emulator tool window shows you the output of running an app on an AVD.


You can read another one of my tutorials to learn How to Manage Virtual and Physical devices in Android Studio. It covers the basics of creating an AVD as well as installing your apps on the AVD. Running your app on a physical device will require you to make some configuration changes on the device and computer.


Try following any of those tutorials to run your android app and see a simple activity that says "Hello World".


Final Thoughts


Android Studio is an incredible piece of software. However, it does have a steep learning and that can be intimidating when you are first starting out. In this tutorial, I have tried to get you started with Android Studio by covering its different aspects one section at a time.


You should now have a better understanding of the welcome screen itself as well as creating a new project of your own. Follow the other linked tutorials to understand how you can write your own code and run your app on virtual and physical devices.



Original Link: https://code.tutsplus.com/tutorials/getting-started-with-android-studio--mobile-22958

Share this article:    Share on Facebook
View Full Article

TutsPlus - Code

Tuts+ is a site aimed at web developers and designers offering tutorials and articles on technologies, skills and techniques to improve how you design and build websites.

More About this Source Visit TutsPlus - Code