How to add apps to android

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.

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:

Источник

Android Widgets

This article covers a complete tutorial about how to add Android Widgets into your application. Understanding the logic and depth diving into Android Widgets with 4 sample projects. You will learn how to code your widget from scratch to advanced. Afterward, at the end article we are going to see what the common mistakes.

Before getting started, what is an Android Widget?

Widgets are an essential aspect of home screen customization. You can imagine them as “at-a-glance” views of an app’s most important data and functionality that is accessible right from the user’s home screen.

You can also read this article in Chinese(中文) .

So what we are going to achieved?

  • Widget Creation Steps with a basic example (‘Simple Widget’ — Opening a website on widget clicked)
  • Broadcast Widget Example (‘Broadcast Widget’ — Counting clicks on widget)
  • Configurable Widget Example (‘Configurable Widget’ — Getting data before creation and using the data when it’s clicked)
  • Updating Widget by a Service (‘Updating Widget’ — Updating every minute brings random numbers on widget)

Widget Creation Steps :

  1. create layout for the widget.
  2. create XML for defining the widget properties.
  3. create class for the widget actions.
  4. add all these to AndroidManifest.xml file.

With that knowledge, our first example is a widget that will be opening a website on widget clicks.

Step 1: Creating a very simple layout for a widget.

This layout is displayed as a widget on user’s home-screen.

Step 2: create an XML that defines widget properties.

In res folder create a new XML folder.

Quick look for these properties

  • initialLayout: reference for the widget layout(that we already created).
  • minHeight and minWidth: Every 60dp means 1 cell in android home-screen. For this example, the widget takes min 1×1 cell(s).
  • previewImage: The image that will be shown on android’s widget select screen. We can not draw a layout for preview. We have to set an image.
  • resizeMode: The configuration for resizing the widget.
  • updatePeriodMillis: The widget’s update method is called when the specified time is reached in a millisecond.
  • widgetCategory: home_screen or keyguard.

Android Studio has an amazing interface for creating widgets.

Step 3: Create a class for the widget lifecycle.

AppWidgetProvider extends BroadcastReceiver. SimpleAppWidget is indirectly a child of BroadcastReceiver. So our widget class is a receiver class.

Step 4: Adding into AndroidManifest as a receiver.

Yay! You have implemented a widget for your app 🙂

Let’s take it to the next level.

RemoteView

Let’s continue with learning RemoteView.

A class that describes a view hierarchy that can be displayed in another process. The hierarchy is inflated from a layout resource file, and this class provides some basic operations for modifying the content of the inflated hierarchy.

RemoteView supports only these layouts:

RemoteView supports only these views:

If you use another view, RemoteView has no operation for the view.

Let’s add some codes to our widget class.

As we can see there is an override method onUpdate which is called every updatePeriodMillis has reached.

This example is basic and fast dive for the widget.

Understanding Override Methods

Before going to the next example, we need to understand widget classes that override the following methods.

  • onUpdate(): This is called to update the App Widget at intervals defined by the updatePeriodMillis attribute.
  • onAppWidgetOptionsChanged() : This is called when the widget is first placed and also whenever the widget is resized.
  • onDeleted(Context, int[]): This is called every time an App Widget is deleted from the App Widget host.
  • onEnabled(Context): This is called when an instance the App Widget is created for the first time.
  • onDisabled(Context): This is called when the last instance of your App Widget is deleted from the App Widget host.
  • onReceive(Context, Intent): This is called for every broadcast and before each of the above callback methods.

Confused? No problem. Below video shows you when these methods will calls.

Читайте также:  Откуда звонили писали для андроид

Let’s see these fellas in action.

This knowledge gives you full control of on your widget.

Broadcast Widget

We have learned all steps of widget creation. We will not be repeating these steps.

Every click on the widget will send a broadcast and onReceive increases the counter.

In previous example we used .getActivity() , this time we are going to use .getBroadcast() .

BroadcastWidget is our widget class. Let’s see what is inside?

Using static fields with widgets is not recommended. But I am trying to make the example as simple as possible. You can use SharedPreferences instead of static fields.

Here is the result.

You can send whatever you want with broadcast and you can catch the broadcast wherever you want.

Configurable Widgets

Some widgets can be configurable at creation. In this example, we are getting a link from the user and whenever it’s clicked we open this link on the browser.

Create ConfigureActivity for user configuration.

The Basic layout for activity.

In activity’s onCreate method, the first thing we do is setting setResult(RESULT_CANCELED) . Why? Android triggers the configure activity that belongs to your widget and awaits result data. If the user did not configure as we expected, let’s say she pressed back button without entering a data, we don’t need to create a widget.

The last thing we do is modify the XML of the widget. With that modification, the Android Operating System will know this widget has configuration activity. So before creating the widget, it will trigger the activity.

As you notice we didn’t talk about widget class yet. We do not need to add any code for widget class because all actions done by ConfigurableWidgetConfigureActivity . But we have to create anyway.

Here is the result.

Updated By a Service Widget Example

This project will generate random numbers in every minute and displays it on a widget. First of all, we need a service to generate random numbers.

And we have to add this to AndroidManifest.

Service does not start by itself. We need to start the service(in every minute for this example).

But why we do not just use ‘ updatePeriodMillis ’?

If the device is asleep when it is time for an update (as defined by updatePeriodMillis ), then the device will wake up in order to perform the update. If you don’t update more than once per hour, this probably won’t cause significant problems for the battery life. If, however, you need to update more frequently and/or you do not need to update while the device is asleep, then you can instead perform updates based on an alarm that will not wake the device. To do so, set an alarm with an Intent that your AppWidgetProvider receives, using the AlarmManager . Set the alarm type to either ELAPSED_REALTIME or RTC , which will only deliver the alarm when the device is awake. Then set updatePeriodMillis to zero ( «0» ).

In Previous examples, we used .getActivity() and .getBroadcast() methods. This time we are going to use .getService() method.

The minimum interval time is 60000 millis for AlarmManager. If you need to call your service less than 60 sec with an alarm manager, there are some workarounds like this. But I have to warn you, this action drains the battery and makes users delete your app.

Here is the result. I cropped the video to not keep you wait 2 minutes.

Common questions / mistakes about Widgets

Question:

Programmatically update widget from activity or service

Answer:

Simple, you have to reach your widgets. Afterwards send broadcast that has information about your widgets. See the code below,

Question:

Every widget instances not doing their job? or doing same job?

Answer:

PendingIntent operations(.getActivity, .getBroadcast etc.) need a parameter requestCode for identification.

You have to set the requestCode with your appW idgetId. With that way, your pending intent becomes unique. Every instance of your widget will use their PendingIntent.

More about Widgets

How the material design should be on widget?

Read further? Helpful links

If you made it at this far, personally thank you for reading. Here is the promised Github link includes whole projects in this article. And feel free for pull requests.

To help others to reach the article don’t forget to recommend 💚 . It means a lot to me.

Источник

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