Android Intents using Library 

| Prashanth Gurunath | - @17_prashanth

1.png

The following blog was posted on Medium by Prashanth Gurunath, a Software Research Engineer at Influx.

In this tutorial, we will be learning to integrate our Android App with an Android Intent Library that will simplify the concept of Intents.

By using this library you can use any of the functionalities that’s present in it in a single line of code.

So let’s get started!

Expected Output:

Through our app, we want to implement the following functionalities using the Intent Library:

  1. Open a browser link that’s entered by the user
  2. Open Facebook, Amazon, Twitter on the Browser
  3. Dial a number that’s entered by the user
  4. Open and view the developer’s LinkedIn Profile ;)
  5. Search the meaning of a string/query that’s entered by the user

Create An Android Studio Project #

  1. Open Android Studio and click on File -> New -> New Project

2.png

Enter your application name as: Intent Project or Intent App (whatever you want)

3.png

Choose API 23 (Android 6.0 — Marshmallow or above for the Minimum SDK)

4.png

Now choose an empty activity.

Click on finish and you will have your application created.

Add the necessary dependencies #

5.png

Add: maven { url ‘https://jitpack.io’ } to the build file in repositories

6.png

ADD: *‘com.github.imprashanthguru:MyIntentApp:v1.3’** build.gradle(app) file*

‘com.github.imprashanthguru:MyIntentApp:v1.3’ — This is the Library Dependency that we are going to use. Make sure to add this in the build.gradle(app) file.

TIP: Make sure to do a proper gradle sync.

We are now done with adding the required dependencies.

XML Code #

  1. Open your activity_main.xml file. The default layout will be constraint layout. But we will be using LinearLayout.
  2. Create a LinearLayout with android:orientation=”vertical” (Your choice. The layout doesn’t matter tbh! )
  3. From the below screenshot, Add the list of buttons that are needed to implement our functionality. (Check the XML code for the same)

6.png

Add 6 buttons inside the Linear Layout.

7.png

With this, we have successfully created the XML layout.

TIP: Make sure to assign the XML ID’s to all buttons/views and as a convention, we use small casing for XML ID’s.

JAVA #

Type the following code in your MainActivity.java

8.png

Create Button Objects for all the 6 buttons and make sure to use the findViewById function to map the XML ID’s of the Button views with their corresponding Java Object.

9.png

Use the button setOnClickListener to set the event handler.

10.png

Java code explanation #

Enter any string url of your choice to open that link in the browser. Ex:

String url = “ https://github.com/”; MyIntent.from(MainActivity.this).openBrowser(url).show();

MainActivity.this denotes the context

MyIntent.from(MainActivity.this).openFacebook().display();

MyIntent.from(MainActivity.this).openAmazon().display();

MyIntent.from(MainActivity.this).followDeveloperOnLinkedin().display();

MyIntent.from(MainActivity.this).openTwitter().display();

Enter any string/query which you want to search
MyIntent.from(MainActivity.this).searchMeaning(“world cup 2019 schedule”).display();

Enter a number of your choice.
MyIntent.from(MainActivity.this).dispDialNumber(“9999999999”).display();

TIP: Have an EditText where the user can enter the browser link URL, the query to google and also the phone number.

So what have we done here? #

We have basically used libraries to perform intent actions and functionalities with lesser lines of code. Without the library, we have to write the code for this separately. And this can be easily injected into your app.
With this , we have successfully completed the tutorial.

If you have any questions, feel free to mail me at prashanthguru.17@gmail.com

Source Code: https://github.com/imprashanthguru/MyIntentApp

Article was originally published on Medium: https://link.medium.com/uSrvcD7jVX

Prashanth Gurunath is Software Research Engineer at Influx. Passionate about mobile app development especially on the Android platform, Prashanth has an engineering degree from SRM University. The above article was originally published on Medium.

 
13
Kudos
 
13
Kudos

Now read this

Artificial Intelligence: where is it taking us?

Prasanna Venkatesh What is Artificial Intelligence? What can it do? What can it not? Will it take over our jobs and make humans redundant? If you’re like me, you used to think artificial intelligence was a silly sci-fi concept, but have... Continue →