Basic Android: My First App
Create a android project named MyFirstApp.
Change the
following files:
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<EditText
android:id="@+id/edit_message"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/edit_message" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage" />
</LinearLayout>
|
Strings.xml
<?xml version="1.0"
encoding="utf-8"?>
<resources>
<string name="hello">This Application is
written by Kent Lau </string>
<string name="app_name">MyFirstApp</string>
<string name="edit_message">Please edit something
here.</string>
<string name="button_send">Hantar Sekarang</string>
</resources>
|
MyFirstAppActivity.java
package com.ugrowit;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MyFirstAppActivity extends Activity {
public final static String
EXTRA_MESSAGE = "com.example.myapp.MESSAGE";
/** Called
when the activity is first created. */
@Override
public
void onCreate(Bundle savedInstanceState) {
Intent
intent = getIntent();
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
/** Called
when the user selects 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);
}
}
|
DisplayMessageActivity.java
package com.ugrowit;
import
android.app.Activity;
import
android.content.Intent;
import
android.os.Bundle;
import
android.widget.TextView;
public class
DisplayMessageActivity extends Activity{
@Override
public void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String
message = intent.getStringExtra(MyFirstAppActivity.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);
}
}
|
No comments:
Post a Comment