top of page

Android - Creating Intents: New Note

Hello all,

I really enjoyed the Google Android Basics Course by Udacity lesson Practice with Intents so I am taking some time to fully explore each of the intents in the Android Common Intents documentation. The one I have most recently looked at is New Note.

First I just created a simple button to activate the intent. The app looks like this at first:

Android App screen showing a single button called NOTE.

And the code in activity_main.xml looks like this:

<Button

android:id="@+id/note"

android:layout_width="@dimen/button_width"

android:layout_height="@dimen/button_height"

android:layout_gravity="center"

android:layout_margin="@dimen/margin"

android:onClick="newNote"

android:text="Note"/>

Then I created an activity in MainActivity.java to call the intent when the button is clicked.

public void newNote(View view) {

String label = "Title";

String main = "This is the body of the note";

Intent intent = new Intent();

intent.setAction(NoteIntents.ACTION_CREATE_NOTE)

.putExtra(NoteIntents.EXTRA_NAME, label)

.putExtra(NoteIntents.EXTRA_TEXT, main);

if (intent.resolveActivity(getPackageManager()) != null) {

startActivity(intent);

} else {

Toast.makeText(this, "No App Available", Toast.LENGTH_SHORT).show();

}

}

If you do this you will find at first that the words “NoteIntents” is highlighted in red. To fix this problem what you need to do is add a line to the build.gradle file. Now, this is NOT the one to be found in or underneath the gradle folder! It is actually found in the app folder below the src folder. See below image…

Android Studio sub-menus showing that you should select the build.gradle file in the app then src folders NOT the gradle folder.

Thanks to my incredibly well designed annotations you should see that you need to pick the file marked with the blue tick, not the one with a red cross. Clicking on this will take you to the file you can see below…

The gradle.build file when you first open it.

Your file should look similar to this one. Don’t worry if some lines are different. The line we need to add is the one highlighted in yellow: compile 'com.google.android.gms:play-services:11.6.2'. This version of play-services should work for now but as it is updated you will need to change this version number: 11.6.2 to whatever the latest version of google play services your version of Android Studio is running. Then you need to go back to your MainActivity.java file and near the top click on the + sign next to “import …” to open up all the imports for your file.

The import section at the top of the MainActivity.java with ... to show it needs to be opened.

That should open up a list of files imported to your app that looks something like this:

Opened imports section of the MainActivity.java file with all imports shown.

Again, don’t worry if your list is slightly different to the one here. The only line you need to add is: import com.google.android.gms.actions.NoteIntents; What you also may need to do is install Google Play Services. To do that follow these steps:

  1. Go to Tools > Android > SDK Manager, as seen in the image below) and click on it.

  1. Then, go to Appearance and Behaviour > System Settings > Android SDK; click the SDK Tools tab and ensure that the “Google Play Services” checkbox is ticked. Then click “Apply” and wait for it to download.

View of the Android Studio SDK manager with the Android SDK and SDK Tools sub-menus highlighted.

The final thing you need is to get permission to create a note. To do this you need to go to Apps > Manifest > AndroidManifest.xml…

Sub-menus of Android Studio that show how to get to the AndroidManifest.xml file.

The code originally looks like this:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.example.android.intentspractise"

<application

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:roundIcon="@mipmap/ic_launcher_round"

android:supportsRtl="true"

android:theme="@style/AppTheme">

<activity android:name=".MainActivity">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

</application>

</manifest>

You need to add this extra line of code INSIDE the section but OUTSIDE all other sections of this file. That new line looks like this:

<uses-permission android:name="com.android.permission.ACTION_CREATE_NOTE" />

The updated file should therefore look like this, with comments:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.example.android.intentspractise">

<!--The line below has been changed to give this app permission to create a note

N.B. The permission line HAS to be put here, inside the manifest section but outside the

other sections -->

<uses-permission android:name="com.android.permission.ACTION_CREATE_NOTE" />

<application

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:roundIcon="@mipmap/ic_launcher_round"

android:supportsRtl="true"

android:theme="@style/AppTheme">

<activity android:name=".MainActivity">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

</application>

</manifest>

This should remove all of the red text and make your intent ready to use.

However, there is one more setback. Intents require your phone to have installed on it an app that can handle the intent. Unfortunately, I have been unable to find a Note app that can handle this intent. I have tried my own phone’s QuickMemo+

Icon for LG's QuickMemo+ app

and Google’s own Keep app .

Icone for Google's "Keep" app

To find this out I had to add an “else” statement with a line of code to make a Toast pop up: Toast.makeText(this, "No App Available", Toast.LENGTH_SHORT).show(); This Toast only appears if the code successfully runs but there is no app able to handle the intent.

If anyone finds an app that can handle this intent please post the name of it in a reply below.

Please feel free to question, correct or further explain anything I have covered here. I am by no means an expert on this topic and would love anything that will help me learn more.

Links:


Featured Posts
Recent Posts
Archive
Search By Tags
Follow Us
  • LinkedIn Social Icon
  • Pinterest Social Icon
  • Facebook Basic Square

© 2023 by Coming Soon

Proudly created with Wix.com

bottom of page