Browse Source

Implement DisplayMessage activity:

- Define class for DisplayMessage activity
  - Activity creates ad-hoc TextView for message output
  - Create Intent on method call from button click
  - Transfer data to new activity, display TextView
master
jimi 13 years ago
parent
commit
cf6a96345c
  1. 7
      AndroidManifest.xml
  2. 1
      res/values/strings.xml
  3. 25
      src/com/jinfield/sampleapp/DisplayMessageActivity.java
  4. 10
      src/com/jinfield/sampleapp/MainActivity.java

7
AndroidManifest.xml

@ -11,5 +11,12 @@
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </intent-filter>
</activity> </activity>
<activity
android:name=".DisplayMessageActivity"
android:label="@string/title_activity_display_message" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.jinfield.sampleapp.MainActivity" />
</activity>
</application> </application>
</manifest> </manifest>

1
res/values/strings.xml

@ -5,4 +5,5 @@
<string name="button_send">Send</string> <string name="button_send">Send</string>
<string name="menu_settings">Settings</string> <string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string> <string name="title_activity_main">MainActivity</string>
<string name="title_activity_display_message">DisplayMessageActivity</string>
</resources> </resources>

25
src/com/jinfield/sampleapp/DisplayMessageActivity.java

@ -0,0 +1,25 @@
package com.jinfield.sampleapp;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.widget.TextView;
public class DisplayMessageActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Recieve message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create TextView
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set textView as activity layout
setContentView(textView);
}
}

10
src/com/jinfield/sampleapp/MainActivity.java

@ -3,9 +3,13 @@ package com.jinfield.sampleapp;
import android.app.Activity; import android.app.Activity;
import android.os.Bundle; import android.os.Bundle;
import android.view.View; import android.view.View;
import android.content.Intent;
import android.widget.EditText;
public class MainActivity extends Activity public class MainActivity extends Activity
{ {
public final static String EXTRA_MESSAGE = "com.jinfield.sampleapp.MESSAGE";
/** Called when the activity is first created. */ /** Called when the activity is first created. */
@Override @Override
public void onCreate(Bundle savedInstanceState) public void onCreate(Bundle savedInstanceState)
@ -15,6 +19,10 @@ public class MainActivity extends Activity
} }
/** Called whe user clicks the send Button */ /** Called whe user clicks the send Button */
public void sendMessage(View view) { public void sendMessage(View view) {
// TODO
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);
} }
} }