1. XML Layout
Buat dua XML layout file di folder “res/layout/” :
res/layout/activity_main.xml
– Activity pertamares/layout/displaymessage.xml
– Activity kedua
File : res/layout/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:id="@+id/sendbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"/>
</LinearLayout>
Kemudian kita tambahkan activity kedua untuk menampung teks dari activity pertama.
File : res/layout/displaymessage.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="113dp"/>
</RelativeLayout>
File : res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Send Message</string>
<string name="edit_message">Write Message here</string>
<string name="button_send">Send</string>
<string name="action_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
</resources>
2. Activity
buat 2 buah activity class
1. MainActivity.java
2. DisplayMessage.java
File : MainActivity.java
package com.example.simpleui;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
protected static final String EXTRA_MESSAGE =
"com.example.simpleui.MESSAGE";
public Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
onClickListener();
}
public void onClickListener(){
final Context context = this;
button = (Button)findViewById(R.id.sendbutton);
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Intent intent =
new Intent(context, DisplayMessage.class);
EditText editText =
(EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
});
}
}
File : DisplayMessage.java
package com.example.simpleui;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class DisplayMessage extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.displaymessage);
onDisplayMessage();
}
public void onDisplayMessage(){
Intent intent = getIntent();
String message =
intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView =
(TextView) findViewById(R.id.message);
textView.setTextSize(24);
textView.setText(message);
}
}
3. AndroidManifest.xml
deklarasikan class activity kedua pada
AndroidManifest.xml
File : AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.simpleui" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".DisplayMessage" android:label="@string/app_name" > </activity> </application> </manifest>
4. Demo
Running Aplikasi
Running Aplikasi
Download Source Code
No comments:
Post a Comment