Issue
I am an PHP developer. Today i tried to learn how to develop android app. I started with Hello World app.
It seems i have done something wrong. Whatever i type in the text box, I am getting only the "Hello World!" as the output in the next view.
views are written in the xml files and activities related to the views are in the java class.
sendMessage()
This function contains
EditText editText = (EditText) findViewById(R.id.edit_message);
1) What is EditText? In xml, I can see it is creating an text box to type. How does it work in the class file? and "R.id.edit_message" what value will be in it? Is it an object?
2) public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
I have no clue about the "com.example.myfirstapp.MESSAGE"; MESSAGE. what is it? where is it coming from?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// 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);
textView.setTextSize(40);
textView.setText(message);
// Show the Up button in the action bar.
setupActionBar();
}
setContentView(R.layout.activity_display_message); -> when should we call the setContentView?
At last, Why is it displaying always "Hello World!" all the time?
I know, It will be frustrating to see questions like this. But I really need to start from the basic to understand the development. Forgive me!
CODES
DisplayMessageActivity.java
package com.example.myfristapp;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class DisplayMessageActivity extends Activity {
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(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);
textView.setTextSize(40);
textView.setText(message);
setContentView(R.layout.activity_display_message);
// Show the Up button in the action bar.
setupActionBar();
}
/**
* Set up the {@link android.app.ActionBar}, if the API is available.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.display_message, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
MainActivity.java
package com.example.myfristapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
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);
}
}
activity_display_message.xml It is created automatically by eclispse. I didn't change it.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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"
tools:context=".DisplayMessageActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">My First App</string>
<string name="edit_message">Enter a message</string>
<string name="button_send">Send</string>
<string name="action_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
<string name="title_activity_display_message">My Message</string>
<string name="hello_world">Hello world!</string>
</resources>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<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"
android:onClick="sendMessage" />
</LinearLayout>
Solution
To answer your questions...
R.id.edit_message
is an integer, generated by Android, that references an id set in a layout file so, assuming your layout XML contains something like
<EditText android:id="@+id/edit_message" ... />
then R.id.edit_message
is a identifier that tells Android, "when I'm using this layout and this id, I mean that particular EditText and no other". The EditText is the box you type your message into. Presumably, its contents are being read via editText.getText().toString() for being passed to the display activity.
com.example.myfirstapp.MESSAGE
is just a name for a key. When starting an activity, you can send it extra data in the form of key/value pair, e.g,
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra(EXTRA_MESSAGE, "Your message")
startActivity(intent);
Declaring the key as a public static field means you can use the key in one activity, and then get the key name from another activity in order to retrieve the key, which is what
intent.getStringExtra(MainActivity.EXTRA_MESSAGE)
is doing.
setContentView
is used to tell the activity which layout file it should use*, and should be called in onCreate before any code that needs to access those views to, e.g, read the contents of your EditText, or add click handlers to buttons or so on.
*Actually, it doesn't have to be a reference to a layout file. You could be creating your own view(s) first, in which case it would need to be called after all your views had been created.
To answer your problem...
In your display activity, you're creating a new TextView object and setting its message, but you don't add that view to the layout, so you never see it. Instead you are seeing the textview that is already there (which has the hello world message in it). To change the message of the existing TextView in activity_display_message.xml
give it an ID, e.g.
<TextView
android:id="@+id/display_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
and then do
setContentView(R.layout.activity_display_message);
TextView textView = (TextView) findViewById(R.id.display_message);
textView.setTextSize(40);
textView.setText(message);
to change its contents. (If you really want to add a new textview, you will need to give the RelativeLayout in activity_display_message.xml an id, and then do something like
// set the layout file:
setContentView(R.layout.activity_display_message);
// get a reference to the layout:
RelativeLayout rl = (RelativeLayout) findViewById(R.id.display_layout);
// create your new textview:
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// set the TextView to display top left:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
// add the view to layout:
rl.addView(textView, params);
Answered By - Ben Williams
Answer Checked By - Cary Denson (JavaFixing Admin)