As we said in a previous post most applications today contain multiple screens, which means they are compiled from multiple activities. We have already created an exercise to see in action switching between two simple activities. Now we will see the process of passing data between two activities with validation check.

The Exercise

The point of this exercise is to see in action passing data between two simple activities. In this exercise we will create two activities named ActivityOne and ActivityTwo. The ActivityOne will have a TextView as a title, EditText where we can enter user input and a Button which on click will send the data (text we will enter in the EditText) to the second activity. The user input required will be your name. The ActivityTwo will have a TextView that will be used to display the data (text) we will receive from ActivityOne. We will create a project and the left side of your Android studio in the project view that is set to android, in the app folder you will find the java folder and in the folder named as your package ( the package that contains your main activity java file, but not the test ones) you will create a new Activity, select Empty Activity. Like this:

First we need so set the layout for the activities. In activity_one.xml we will add a TextView, EditText and a Button. Here is the XML code for this elements:

This is how it looks like:

Then in activity_two.xml we will do a very similar creation of an TextView to display the data. Here is the xml code for this TextView:

This is how it looks like:

Now comes the programming part. In this exercise we will be using ButterKnife library, if you are not familiar with this library you can check this post. For passing the date between activities we will be using Intent. An Intent is a messaging object you can use to request an action from another app component. Although intents facilitate communication between components in several ways, there are three fundamental use cases: starting an Activity, starting a service and delivering a broadcast. There are two types of Intents:

  • Explicit intents specify which application will satisfy the intent, by supplying either the target app’s package name or a fully-qualified component class name. You’ll typically use an explicit intent to start a component in your own app, because you know the class name of the activity or service you want to start. For example, you might start a new activity within your app in response to a user action, or start a service to download a file in the background.
  • Implicit intents do not name a specific component, but instead declare a general action to perform, which allows a component from another app to handle it. For example, if you want to show the user a location on a map, you can use an implicit intent to request that another capable app show a specified location on a map.

For more on Intents, you should visit this site.

In ActivityOne we will declare one final static String that we will use as a key for our putExtra() method and bind the TextView using ButterKnife:

After that we will set the on click action on the button. We will create an intent object that will be used to start ActivityTwo, and pass the text entered in the EditText. We will use the getText() and toString() methods to get the text entered in the EditText and convert it to string. Using an if-else condition we will perform a validation check, in a situation where the user does not enter any text and press the send button, ActivityTwo will not be started, instead a toast message will be displayed to the user.

In ActiviryTwo we will get the message sent from ActivityOne and we will set the TextView to show this message.


This is a simple example, but it shows us how to pass data between two activities in Android. You can find the whole project here
Happy coding!




Spread the love

1 Comment

Leave a Reply

Your email address will not be published.