How to code android app

How to Make Android Apps for Beginners

Are you a new developer learning Java? Read ahead to learn how to make your first Java Android app as a beginner.

Creating a mobile application is a big step towards turning your idea into reality. But the primary concern among new developers is how to make an Android app with all the resources at their disposal. If you’re a beginner, you won’t know the purpose of many of them. So it’s important to begin from the basics and know the starting point.

This guide will walk you through the basics of building an Android app and answer your questions about how to make an app with Java. We’ll be creating a Java Android app using Android Studio as our Integrated Development Environment (IDE).

How to Make an App in Java

To start making a Java Android app, you have to start with an IDE. There are several options for IDEs, but two of the most efficient ones are Eclipse and Android Studio.

For this guide, we are using Android Studio 3.1.3. Android Studio is a complete IDE that offers tools to make app development very simple and easy. It has an advanced code editor and several app design templates. There are tools for development, debugging, and testing as well.

You can learn how to install Android Studio in our post on how to get started making Android apps.

Run Android Studio

Let’s begin with a new project on Android Studio. It gives the option to Create New Project on the welcome screen. You can also continue with your current project if you have one.

Once you choose to create a new project, the next step is choosing your activity. You will be provided with several options, but we’ll begin with an Empty Activity for this tutorial on creating a Java app.

The next step is configuring your activity by giving it a name. Let’s name our app MyBasicApp. From the drop-down menu, choose the Java language. Click Finish.

Now we are ready to start creating our app!

Start Working on Java App Development

At this stage, Android Studio has created two folders that are visible in the left corner. They are:

  • A folder for MyBasicApp—this folder carries the code for your app.
  • A folder of Gradle scripts—Gradle is a free and open-source tool used by Android Studio to turn the code into an .apk file for your app.

Since we selected the Basic Activity template, Android Studio has created some preset files for our project. You can expand the folders to view them.

Clicking on the app folder will give a drop-down menu with three to four subfolders: manifests, Java, Java (generated), and res. Expanding each one of them will open more folders. Each folder stores a separate component of your project.

In the MyBasicApp folder, go to the Java folder and click on com.example.mybasicapp. This folder contains the source code of your Java Android app.

Now, click the res folder and open the layout folder. It contains a file activity_main.xml. It is the layout of your app. Click to open it.

Now your project view has opened the source code and the layout in two separate tabs. It looks like this:

The .xml file gives you a layout where you can drag and drop elements to build your file. You can also change it to code editor by clicking on text at the bottom left corner. Now you can edit the source code instead of adding elements to the design layout.

In the layout editor, the left pane contains all the elements that you can add to the layout. If you see the component tree, there is just one element—Hello World—in our app.

The elements we add to our layout will be shown in the component tree and how they are added in relation to each other.

Here, ConstraintLayout is the root of the view hierarchy (it is the dimensions of the page on which components will be added).

The ConstraintLayout has a TextView called Hello World.

The TextView is the component that came with a blank activity. The layout at this stage looks like this:

The XML code for this layout will be this:

It has one root hierarchy with just one text element.

Note that this is the basic layout that Android Studio generated itself. We haven’t made any modifications to it yet.

Читайте также:  Stream champ для андроид

Modify the App Layout

The next step is to modify the layout by adding components of your choice and associating activities with them.

Here you can either start with this ConstraintLayout with TextView in it, or you can clear the ConstraintLayout and begin with a clean slate in terms of layout.

Let’s say you want to change the text on the TextView . You can go to the code editor and view the properties of the TextView element. The code will be like this:

Change the string to whatever you like. For now, I’ll stick with Hello World.

If you have set up an Android Virtual Device (AVD) in the Studio, the app will run on the simulator like this:

This is the basic TextView with Hello World! as its string. You can modify the attributes of this text component. Edit the text, font, size, and color to give a new look to TextView .

The XML code for these changes would be this:

We have changed the font to Sans Serif and made the color darker gray . The font size is also increased to 30sp , and the text is made bold.

The changes as visible in the AVD:

Add Constraints

Up to now, we’ve just modified the stock Empty Activity set up by Android Studio. Now, we will see how to add constraints and views to the user interface.

In the Layout Editor, there is a palette on the right side from where you can choose the constraints you’d like to add.

You can add more than one view and constrain them to top, bottom, left, or right. These views have attributes that can be edited to modify them.

To better understand what an attribute is, select textView in the Component Tree and look at the Constraint Widget in the Attributes panel.

The square represents the constraints.

The rectangular box and each of the four dots represent a constraint. You can increase or decrease their value based on the point where you want to place the view.

Here is how it looks in the Layout Editor.

You can drag and drop the Views from the left palette to add them in the ConstraintLayout . After adding them, you can apply constraints to determine their position. Each new button added to the layout is given a unique identity.

Here is how your layout will look after adding the constraints to the views:

Here is the XML code for the finished layout:

Assigning Activity to the Button

The layout design is the meat of your app. You have added a Button , but it doesn’t do anything when pressed. To make it interactive and responsive to users’ actions, we have to assign activity to it.

Let’s say this is the last step on your app, and upon clicking Next you want people to see the message Finish. Here’s how to do it.

The Next button has an id called @+id/next . Since no other element is using this id, we can use it to find the button and add it to the Java code so that the activity doesn’t get associated with another button.

The id for a view helps you identify it as each id differs from other viewers’ id. With the findViewByID() function, next can be found through its id, R.id.next .

Here is the final code that will display the finish message:

Run the app to see how the Button works.

Conclusion

This guide covered everything from setting up the Android Studio to coding and testing a Button . It’s not hard to get started coding an app—Java mobile app development has become easier since a lot of elements are drag and drop.

Android App Templates and UI Kits From CodeCanyon

You may have noticed that our app looks very plain and simple. That’s because we’re using the default theme, without applying any styles to our views. CodeCanyon is full of Android UI kits that offer beautiful, hand-crafted styles you can apply to your views.

The kits generally also have several custom views and layouts. You can refer to the following articles to learn more about them:

Источник

How to Code a Navigation Drawer for an Android App

The Material Design team at Google defines the functionality of a navigation drawer in Android as follows:

The navigation drawer slides in from the left and contains the navigation destinations for your app.

An example of a popular Android app that implements the navigation drawer menu design is the Inbox app from Google, which uses a navigation drawer to navigate different application sections. You can check it yourself by downloading the Inbox app from the Google Play store if you don’t already have it on your device. The screenshot below shows Inbox with the navigation drawer pulled open.

The user can view the navigation drawer when they swipe a finger from the left edge of the activity. They can also find it from the home activity (the top level of the app) by tapping the app icon (also known as the Android «hamburger» menu) in the action bar.

Читайте также:  Как пользоваться flashtool для прошивки андроид

Note that if you have many different destinations (more than six, say) in your app, it’s recommended that you use a navigation drawer menu design.

In this post, you’ll learn how to display navigation items inside a navigation drawer in Android. We’ll cover how to use Jetpack navigation to perform this task. As a bonus, you’ll also learn how to use the Android Studio templates feature to bootstrap your project with a navigation drawer quickly.

Prerequisites

To be able to follow this Android Studio navigation drawer tutorial, you’ll need:

Create an Android Studio Project

Fire up Android Studio and create a new project (you can name it NavigationDrawer ) with an empty activity called MainActivity . Make sure to also choose the Kotlin language.

Add Project Dependencies

Support for navigation requires some dependencies. Open the app build.gradle file and add the following dependencies.

Also add the material library to the project.

Sync the project files for the changes to take effect.

Create the DrawerLayout

To display the drawer icon on all destinations in our app, we will use the DrawerLayout component. Open main_acivity.xml and add DrawerLayout as the root view. The drawer layout will host two child views, NavHostFragment and NavigationView .

Here we created a DrawerLayout widget with the id drawer_layout . The tools:openDrawer property is used to display the navigation drawer toggle when the XML layout is open in Android Studio design view.

The official documentation says the following about DrawerLayout :

DrawerLayout acts as a top-level container for window content that allows for interactive «drawer» views to be pulled out from one or both vertical edges of the window.

After adding the DrawerLayout widget, we included a child layout, app_bar_main.xml which points to the toolbar layout.

Here is my app_bar_main.xml resource file. This file has a CoordinatorLayout , an AppBarLayout , and a Toolbar widget.

Create a Navigation Graph

A navigation graph is an XML resource file that contains all of your app’s destinations and actions, and these destinations are connected via actions. Below is an example of a navigation graph showing five fragments.

Enter Nav graph showing 5 screens

To add a navigation graph, right-click on the res directory and select New > Android Resource File. In the next dialog, select Navigation as the Resource Type, and click OK. A new XML file, nav_graph.xml, will be created in the Navigation folder, as shown below.

Create navigation graph

Add NavHostFragment

A navigation host fragment acts as a host for the app’s fragments and swaps fragments in and out as necessary when the user moves from one destination to the other. These destinations have to be defined in the navigation graph.

Add NavHostFragment to the main_activity.xml file and reference the navGraph .

Add Fragments to the Destination Graph

Fragments represent all the destinations of your app. In our case, we will add three fragments to the navigation graph. Right-click the navigation folder and open nav_graph.xml. To add a fragment, click on Create New Destination and fill out the rest of the details.

Add fragment to navigation graph

Repeat the same steps and create two additional fragments, the profile fragment and the settings fragment. Your navigation graph should now look like this.

Add a NavigationView Component

Finally, let’s create a NavigationView widget. The official documentation says the following about NavigationView :

NavigationView represents a standard navigation menu for application. The menu contents can be populated by a menu resource file.

Open main_activity.xml and add the NavigationView .

In the NavigationView XML widget, you can see that we added an android:layout_gravity attribute with the value start . This is used to position the drawer—you want the navigation drawer menu design to come out from the left or right (the start or end on platform versions that support layout direction). In our own case, the drawer will come out from the left.

We also included an app:headerLayout attribute, which points to @layout/nav_header_main . This will add a View as a header of the navigation menu.

Here is my nav_header_main.xml layout resource file:

To include the menu items for the navigation drawer, we can use the attribute app:menu with a value that points to a menu resource file.

Here is the res/menu/drawer_menu.xml menu resource file:

Here we have defined a Menu using the which serves as a container for menu items. An creates a MenuItem , which represents a single item in a menu. It’s also important to note that the ids of the menu items correspond to the ids of the matching fragment.

Note that when showing the navigation list items from a menu resource, we could use a ListView instead. But, by configuring the navigation drawer with a menu resource, we get the material design styling on the navigation drawer for free! If you used a ListView , you’d have to maintain the list and also style it to meet the recommended material design specs for the navigation drawer.

Initialization of Components

Next, we are going to initialize instances of all our components. Initialization is going to happen inside onCreate() in MainActivity.kt.

The AppBarConfiguration object is used to manage the behavior of the navigation drawer button.

First, we use the setSupportActionBar() method to set the toolbar as the app bar for the activity.

Читайте также:  Смарт часы android sim

Next, we set all fragments as top-level destinations, this means that they will remain in the back stack when navigating.

The method setupActionBarWithNavController automatically updates the title in the action bar when the destination changes.

Set up the navigation drawer.

Lastly, show the up button that appears at the top left of the app bar. This is done by integrating the navigation controller withe app bar using the onSupportNavigateUp method.

The final code for MainActivity.kt should look like this.

Testing the App

At this point, we can run the app!

Bonus: Using Android Studio Templates

Now that you’ve learnt about the APIs involved to create a navigation drawer, I’ll show you a shortcut that will make it faster next time. You can simply use a template instead of coding a navigation drawer Activity from scratch.

Android Studio provides code templates that follow the Android design and development best practices. These existing code templates (available in Java and Kotlin) can help you quickly kick-start your project. One such template can be used to create a navigation drawer activity.

I’ll show you how to use this handy feature in Android Studio.

For a new project, fire up Android Studio.

Enter the application name and click the Next button.

You can leave the defaults as they’re in the Target Android Devices dialog. Click the Next button again.

In the Add an Activity to Mobile dialog, scroll down and select Navigation Drawer Activity. Click the Next button after that.

In the last dialog, you can rename the Activity name, layout name, or title if you want. Finally, click the Finish button to accept all configurations.

Android Studio has now helped us to create a project with a navigation drawer activity. Really cool! You’re strongly advised to explore the code generated.

You can use templates for an already existing Android Studio project too. Simply go to File > New > Activity > Navigation Drawer Activity.

Top Android App Templates With Navigation Drawers From CodeCanyon

The templates that come included with Android Studio are good for simple layouts and making basic apps, but if you want to kick-start your app even further, you might consider some of the app templates available from Envato Market.

They’re a huge time-saver for experienced developers, helping them to cut through the slog of creating an app from scratch and focus their talents instead on the unique and customised parts of creating a new app.

Here are just a small handful of the thousands of Android app templates available on CodeCanyon. If there’s one that piques your interest, you can easily get started by making a purchase.

Grocery and Vegetable Delivery Android App with Admin Panel

If you or your client have a food delivery business, getting an app up and running is crucial. That’s why you should consider this multi-store grocery service app template. It includes three templates with stunning layouts and Android hamburger menus. There’s no limit to the categories you can add, and you can also use SMS and email order notifications.

Universal: Full Multi-Purpose Android App

Buying the Universal Android app template is just like downloading a Swiss Army knife. It can do it all, from WordPress and Facebook to Twitter and SoundCloud. In fact, there is a list of more than 15 content providers that this template supports. Users can access important information from the slick side menu design and easily make their way around their favorite sites.

MaterialX: Android Material Design UI Components 2.7

MaterialX is a recommended download for any app developer. It includes more than 315 unique UI components across more than 31 categories. Create stunning Android side menu designs, buttons, dialog boxes, and more from this single download. If you want a quick way to add some much-needed style to your new project, get this template.

Universal Android WebView App

Do you have content hosted online that you want to turn into a mobile experience? Then check out the Universal Android WebView App template. It was developed in Android Studio and supports phones and tablets running Android 4.1 and above. The Android navigation drawer menu design is completely customizable, as are other components. It also supports AdMob for monetization.

Android Wallpapers App

Here’s a cool Android app template that’s useful if you want to get your creative designs out into the world. The Android Wallpapers app supports static images, GIFs, and 4K photos. This template also includes useful features like:

  • pinch to zoom
  • push notifications
  • AdMob advertisement support
  • Android Studio code

Conclusion

In this tutorial, you learned how to create a navigation drawer menu design in Android from scratch, using Jetpack navigation. We also explored how easy and quick it is to use Android Studio templates to create a navigation drawer.

I highly recommend checking out the official material design guidelines for navigation drawers to learn more about how to properly design and use navigation drawers in Android.

To learn more about coding for Android, check out some of our other courses and tutorials here on Envato Tuts+!

This post has been updated with contributions from Nathan Umoh. Nathan is a staff writer for Envato Tuts+.

Источник

Оцените статью