My First Android App

Android Studio Tutorial by Marcio Valenzuela
Android Studio Tutorial

Now we are going to receive the input of this message and use the button to send it.

To do so, edit your Button declaration to look like this:





android:onClick=”sendMessage” />

We are simply telling it to respond to the onClick button action by calling the sendMessage method.

So we must declare this method in code, of course.  Open your file and add the following:

/** Called when the user clicks the Send button */

public void sendMessage(View view) {

// Do something in response to button


We are declaring a public method that returns void, is called sendMessage and takes a View type to be referenced locally as view.

In order to do this you will need imports such as:

import android.view.View;

Now we are going to have this method declare what is called an Intent.  An intent is an action that we wish to carry out.  We do so by inserting this line into the method:

Intent intent = new Intent(this, DisplayMessageActivity.class);

Here we create a ‘new’ intent for this class to call the DisplayMessageActivity class and we assign it to an Intent type called intent! 🙂

Ok great, but what is this intent going to do?!  Add this code right below:

EditText editText = (EditText) findViewById(;

String message = editText.getText().toString();

intent.putExtra(EXTRA_MESSAGE, message);

First we get a reference to our EditText and assign it to a new variable called, rightly so, editText.  We get to our EditText view by finding the view through its id, which is edit_message.  Now we get that editText object and call its getText method concatenated to its toString method.  This converts the editText’s text value into a string.  We are assigning it to a String type variable called message.

Finally we call an intent object’s putExtra method to send that message variable along with an EXTRA_MESSAGE value.

You need 2 more imports here:



Now let’s define the EXTRA_MESSAGE inside our MainActivity by adding this line right below the MainActivity public class declaration:

public class MainActivity extends Activity {

public final static String EXTRA_MESSAGE = “com.example.myfirstapp.MESSAGE”;


Now add the line that actually calls the new activity and your code should look like this:

public void sendMessage(View view) {

Intent intent = new Intent(this, DisplayMessageActivity.class);

EditText editText = (EditText) findViewById(;

String message = editText.getText().toString();

intent.putExtra(EXTRA_MESSAGE, message);



Let’s create the second activity by right clicking on our java folder and selecting new Android Activity like so:

Screenshot 2014-02-08 15.49.32

Once again select a Blank Activity, the click Next.  Now fill in the following window as follows:

Android Studio Tutorial Adding New Activity by Marcio Valenzuela
Android Studio Tutorial Adding New Activity

The Activity Name is self explanatory.  The layout file name is provided for you as are the others but remember that we are not using fragments.  So replace the fragment name again, with the same value as above in the Layout Name.  Leave the Title as is but for Hierarchichal Parent add in the name of the calling activity (MainActivity) preceded by your package name (which you can find in the AndroidManifest.xml in case you forgot.

Also remember to remove that fragment method created by default, Only If Its There!

Trim off some other unused stuff so that the final code looks like this:

public class DisplayMessageActivity extends Activity {


protected void onCreate(Bundle savedInstanceState) {





public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {



return true;


return super.onOptionsItemSelected(item);



We are doing the same thing here, which is call super.onCreate just to make sure something gets created.  Then we set the content view to be connected to this activity’s layout file.

Go to your strings.xml file and add a new string like so:

    <string name=”title_activity_display_message”>My Message</string>

Whenever we create a new activity we must declare it in the AndroidManifest.xml file.  Ours should now look like this:

<application … >




android:parentActivityName=”com.example.myfirstapp.MainActivity” >



android:value=”com.example.myfirstapp.MainActivity” />



Here we added the DisplayMessageActivity activity and its label as well as its parent.

So with our new activity created and the intent from the calling class, we now need to receive that intent in the new class.  Here is the code:


public void onCreate(Bundle savedInstanceState) {


// Get the message from the intent

Intent intent = getIntent();

String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

// Create the text view

TextView textView = new TextView(this);



// Set the text view as the activity layout



We again create an Intent object and get the message from the MainActivity.  We then get a TextView reference, set its text size and set its text property to that in the gotten message.  Finally we set the content view to what it needs to be now.

Voila!  Build & Run and enjoy your first android app.

Leave a Reply