Android Intents using Library
| Prashanth Gurunath | - @17_prashanth
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:
- Open a browser link that’s entered by the user
- Open Facebook, Amazon, Twitter on the Browser
- Dial a number that’s entered by the user
- Open and view the developer’s LinkedIn Profile ;)
- Search the meaning of a string/query that’s entered by the user
Create An Android Studio Project #
- Open Android Studio and click on File -> New -> New Project
Enter your application name as: Intent Project or Intent App (whatever you want)
Choose API 23 (Android 6.0 — Marshmallow or above for the Minimum SDK)
Now choose an empty activity.
Click on finish and you will have your application created.
Add the necessary dependencies #
- Add the JitPack repository to your build file
Add: maven { url ‘https://jitpack.io’ } to the build file in repositories
- Add the Intent library dependency
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 #
- Open your activity_main.xml file. The default layout will be constraint layout. But we will be using LinearLayout.
- Create a LinearLayout with android:orientation=”vertical” (Your choice. The layout doesn’t matter tbh! )
- From the below screenshot, Add the list of buttons that are needed to implement our functionality. (Check the XML code for the same)
Add 6 buttons inside the Linear Layout.
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
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.
Use the button setOnClickListener to set the event handler.
Java code explanation #
- To open any link on the browser:
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
- To open FB page:
MyIntent.from(MainActivity.this).openFacebook().display();
- To open Amazon.in:
MyIntent.from(MainActivity.this).openAmazon().display();
- To view my LinkedIn Profile:
MyIntent.from(MainActivity.this).followDeveloperOnLinkedin().display();
- To open Twitter:
MyIntent.from(MainActivity.this).openTwitter().display();
- To search the meaning of any text/query on Google:
Enter any string/query which you want to search
MyIntent.from(MainActivity.this).searchMeaning(“world cup 2019 schedule”).display();
- To Dial a number:
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.