Hello World Android App Source Code [Eclipse]

After a lot of efforts, I finally got success in running my first android app “Hello World”. I followed the tutorial given at https://developer.android.com/training/index.html which was sufficient enough to run the application. However I faced few problems while trying to run the application. 

 

Problems that you may face during your try to run the hello world app are : 

  1. Send button not working.
  2. What ever you type in message field, It always shows Hello World on the next screen.
  3. After clicking the send button, the next screen is blank. 

Below is the source code from my app which i executed in eclipse. If you getting any error then you must look at the code below to get the proper output. 

 

MainActivity.Java

package com.tech.welcome;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity
{
public final static String EXTRA_MESSAGE = “com.tech.welcome.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;
}
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);
}
}

 

DisplayMessageActivity.Java

package com.tech.welcome;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;

public class DisplayMessageActivity extends Activity {

@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);

// Set the text view as the activity layout
setContentView(textView);

// 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);
}

}

Strings.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<resources>

<string name=”app_name”>Welcome</string>
<string name=”action_settings”>Settings</string>
<string name=”edit_message”>Enter a Message</string>
<string name=”button_send”>Send</string>
<string name=”title_activity_main”>MainActivity</string>
<string name=”title_activity_display_message”>MyMessage</string>
<string name=”hello_world”>Hello world!</string>

</resources>

Activity_Main.xml

<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:hint=”@string/button_send”
android:onClick=”sendMessage” />

</LinearLayout>

Most of these already comes when you make the android project. You need only certain piece of code. However main thing is that you must understand how everything is connecting to each other. If you have any doubts, comment or send us an email at : palii89@gmail.com

Also visit our programming website : Programming in JAVA, C, C++, C#