Sending messages from one activity to another

Previous article: Adding elements to our Android App

Short link: bit.ly/22t9Bg1

GitHub Repository: https://github.com/androidcodingspace/MyFirstApp

Examples for this article.

First we learned how to create an simple Hello World project, then how to add some elements to this project. Now, we will learn how to send an message from one activity (“screen”) to another.

First, at the content_my.xml (app/res/layout) you need to add the attribute android:onClick to the Button. The code of the button should look like this:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_send"
    android:onClick="sendMessage"/>

At the java/space.androidcoding.myfirstapp directory, open the MyActivity.java file. Add the code below:

public void sendMessage(View view) {
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.edit_message);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
}

When some code turn “red”, usually is a missing import. Just type ALT+Enter and Android Studio will solve it for you.

Code explanation:

  • Intent is how Android send information from one activity to another
  • DisplayMessageActivity will show an error. Don’t bother for now. We will create the class soon.
  • We are using the method findViewById to get the EditText element.
  • The message is “stored” at the variable message.
  • The putExtra is uset to “carry” data. EXTRA_MESSAGE is the key and message is the value.
  • The startActivity method will “finish” the intent.

At the top of the MyActivity.java class, you should add this line:

public final static String EXTRA_MESSAGE = "space.androidcoding.myfirstap.MESSAGE";

The package name is used to not get mixed with another Apps messages.

Now you will create the second activity.

Do a right-click at the java directory in Android Studio, then “New”, “Activity”, “Blank Activity”. The type the details of the new activity:

  • Activity Name: DisplayMessageActivity
  • Layout Name: activity_display_message
  • Title: My Message
  • Hierarchical Parent: space.androidcoding.myfirstapp.MyActivity
  • Package name: space.androidcoding.myfirstapp

Go to the content_display_message.xml file (app/res/layout). Add an android:id attribute to the RelativeLayout. You need this attribute to reference the object from your app code. Your code will look like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"      tools:context="space.androidcoding.myfirstapp.DisplayMessageActivity"
    tools:showIn="@layout/activity_display_message"
    android:id="@+id/content">

</RelativeLayout>

Go to the DisplayMessageActivity.java class, at java/space.androidcoding.myfirstapp.

Add this code to the end of the onCreate method:

Intent intent = getIntent();
String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);

RelativeLayout layout = (RelativeLayout) findViewById(R.id.content);
layout.addView(textView);

Don’t forget to import the TextView (ALT+Enter) class at the top of your code.

The complete onCreate() method for DisplayMessageActivity now looks like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_message);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    Intent intent = getIntent();
    String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);
    TextView textView = new TextView(this);
    textView.setTextSize(40);
    textView.setText(message);

    RelativeLayout layout = (RelativeLayout) findViewById(R.id.content);
    layout.addView(textView);

}

Now you can run the app, type an text and hit send. The message will appear at the other activity!

Source: http://developer.android.com/intl/pt-br/training/basics/firstapp/starting-activity.html

Adding elements to our Android App

Previous article: Hello World App!

Short link: bit.ly/1TQ1JoS

GitHub Repository: https://github.com/androidcodingspace/MyFirstApp

Examples for this article.

This article uses an blank project as starting point. If you don’t know nothing about creating Android Apps, you can start from the article where i’ve shown how to create an “Hello World” App.

Let’s go!

Open content_my.xml (app/res/layout) file at the “Text” tab.

You will change the RelativeLayout to LinearLayout, delete the “padding” and tools:context lines. Then, add an android:orientation.

The code was like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="space.androidcoding.myfirstapp.MyActivity"
    tools:showIn="@layout/activity_my">

[...]

</RelativeLayout>

And will be like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_my">

[...]

</LinearLayout>


Now, inside the LinearLayout element, you will delete the TextView element and add an EditText and a Button element. Like this:

EditText

<EditText android:id="@+id/edit_message"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:hint="@string/edit_message"/>

Button

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_send"/>

The android:id is an identifier. You will use it when you need to refer to this specific EditText. The layout_width, layout_weight and layout_height at both elements sets the size of them. The attribute android:hint at EditText is the text shown to the user before he types something, and the android:text at the Button, is the text at the button.

The complete code of context_my.xml should look like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_my">

    <EditText android:id="@+id/edit_message"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="@string/edit_message"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send"/>

</LinearLayout>

You could ask: what means @string/button_send and @string/edit_message ?

I will explain! 😉

There is an file called strings.xml (app/res/values/strings.xml). At this file you can put all “texts” of your app. So if you need to change anything, or translate to another language, it will be soooo much easier. 🙂

Your strings.xml should look like this one:

<resources>
    <string name="app_name">MyFirstApp</string>
    <string name="action_settings">Settings</string>
    <string name="edit_message">Enter a message</string>
    <string name="button_send">Send</string>
</resources>

You can see the edit_message and button_send there?

That’s it for now! At the next article i will show how to add some actions to your elements.

Just click at the “Run” Menu, then “Run App” (or Shift + F10). It will look like this:

Your App with EditText and Button Elements!
Your App with EditText and Button Elements!

Next article: Sending messages from one activity to another

Source: http://developer.android.com/intl/pt-br/training/basics/firstapp/building-ui.html

Hello World App!

Short link: bit.ly/1XnruMd

GitHub Repository: https://github.com/androidcodingspace/MyFirstApp

Examples for this article.

This is the first post from Android Coding Space! Here, i will show how to create an “Hello World” App. I will assume that you have Android Studio installed.

Let’s open the Android Studio. The screen above will appear. You should click at “Start a new Android Studio Project”.

Android Studio - Quick Start
Android Studio – Quick Start

Then you will choose the name of your app and click Next.

Choose App Name
Choose App Name

Choose the Minimum SDK where your App will run. This means that if you choose the API 16 (Android 4.1), your app will not run at older versions (like Android 4.0, for example).

Older versions means more compatibility. Newer versions means better resources.

For our example, we will choose the API 16.

Choose API 16 and click next.

Target Android Devices
Target Android Devices

Add an Activity. There are some models. For now, Black Activity will work for us. An activity is like an “screen” of your App.

Add An Activity
Add An Activity

Here you can define some information for your Activity. Change the Activity Name to MyActivity and click Finish.

Customize the Activity
Customize the Activity

This is the Android Studio Main Screen. Is here that the Magic works 😉

Your Hello World App is ready! Easy, right?

To see the App working, you will need to emulate it. The Android Studio will run your app without the need of having an Android Phone. Just click at the “Run” Menu, then “Run App” (or Shift + F10).

Android Studio
Android Studio

Choose the device that Android Studio will Emulate and click OK.

Device Chooser
Device Chooser

This is your App running!

Your App!
Your App!

This was just an kickstarter post. More tips will come. Keep coming back!

Next article: Adding elements to our Android App

Source: http://developer.android.com/intl/pt-br/training/basics/firstapp/creating-project.html