Tutorial Membuat Two Activities pada Android Studio
Assalamualaikum..
Kembali lagi dengan blog saya guys.. kali ini saya
akan membahas tentang bagaimana caranya membuat aplikasi Two Activities dengan menggunakan
Android Studio. Sebelumnya saya sudah pernah membahas tutorial materi lainya
seperti tutorial membuat Aplikasi Hello Toast, Membuat Aplikasi Scrolling Text
, dan membahas tentang perbedaan Linear Layout, Relative Layout dan Constraint
Layout. Di materi yang saya bahas kali ini tentang Two Activities. Baiklah
project ini ada Dua Aktifitas yang dibangun dengan 3 tahap. Berikut mengenai
pembahasan 3 tahap diantaranya :
1. Tahap Pertama
Buat aplikasi yang aktifitas utamanya
hanya berisi satu tombol (Send). Saat pengguna mengeklik tombol ini, aktifitsas
utama menggunakan intent untuk memulai aplikasi kedua.
2. Tahap Kedua
Pengguna memasukkan pesan dan mengeklik
Send. Aktifitas utama menggunakan intent untuk memulai aktifitas kedua dan
untuk mengirimkan pesan pengguna ke aktifitas tersebut. Aktifitas kedua
menampilkan pesan yang diterimanya.
3. Tahap Ketiga
Tambahkan tampilan EditText dan tombol
Reply ke aktifitas kedua. Sekarang pengguna bisa mengetik pesan balasan dan
mengeklik Reply, dan balasanya ditampilkan pada aktifitas utama. Pada titik
ini, gunakan intent disini untuk meneruskan pesan balasan kembali dari
aktifitas kedua ke aktifitas utama.
Berikut adalah langkah-langkah untuk
membuat TwoActivities :
Buka Aplikasi Android Studio, kemudian buat project
baru dengan nama “TwoActvities”
Ubah domain
menjadi “Android.example.com”
Pilih SDK Minimum API15 :
Android 4.0.3 IceCreamSandwich pilih next
Buka res
kemudian layout pilih mainactivity.java
<?xml version="1.0" encoding="utf-8"?><!-- Copyright 2016 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. -->
<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=".MainActivity">
<TextView
android:id="@+id/text_header_reply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:text="@string/text_header_reply"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold"
android:visibility="invisible" />
<TextView
android:id="@+id/text_message_reply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text_header_reply"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:textAppearance="?android:attr/textAppearanceMedium"
android:visibility="invisible" />
<Button
android:id="@+id/button_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:onClick="launchSecondActivity"
android:text="@string/button_main" />
<EditText
android:id="@+id/editText_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@+id/button_main"
android:layout_toStartOf="@+id/button_main"
android:hint="@string/editText_main" />
</RelativeLayout> |
/* * Copyright (C) 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.example.android.twoactivities; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.EditText; import android.widget.TextView; /** * The TwoActivities app contains two activities and sends messages (intents) between them. */ public class MainActivity extends AppCompatActivity { // Class name for Log tag private static final String LOG_TAG = MainActivity.class.getSimpleName(); // Unique tag required for the intent extra public static final String EXTRA_MESSAGE = "com.example.android.twoactivities.extra.MESSAGE"; // Unique tag for the intent reply public static final int TEXT_REQUEST = 1; // EditText view for the message private EditText mMessageEditText; // TextView for the reply header private TextView mReplyHeadTextView; // TextView for the reply body private TextView mReplyTextView; /** * Initializes the activity. * * @param savedInstanceState The current state data. */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Initialize all the view variables. mMessageEditText = (EditText) findViewById(R.id.editText_main); mReplyHeadTextView = (TextView) findViewById(R.id.text_header_reply); mReplyTextView = (TextView) findViewById(R.id.text_message_reply); } /** * Handle the onClick for the "Send" button. Gets the value of the main EditText, * creates an intent, and launches the second activity with that intent. * * The return intent from the second activity is onActivityResult(). * @param view The view (Button) that was clicked. */ public void launchSecondActivity(View view) { Log.d(LOG_TAG, "Button clicked!"); Intent intent = new Intent(this, SecondActivity.class); String message = mMessageEditText.getText().toString(); intent.putExtra(EXTRA_MESSAGE, message); startActivityForResult(intent, TEXT_REQUEST); } /** * Handle the data in the return intent from SecondActivity. * * @param requestCode Code for the SecondActivity request. * @param resultCode Code that comes back from SecondActivity. * @param data Intent data sent back from SecondActivity. */ @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); // Test for the right intent reply if (requestCode == TEXT_REQUEST) { // Test to make sure the intent reply result was good. if (resultCode == RESULT_OK) { String reply = data.getStringExtra(SecondActivity.EXTRA_REPLY); // Make the reply head visible. mReplyHeadTextView.setVisibility(View.VISIBLE); // Set the reply and make it visible. mReplyTextView.setText(reply); mReplyTextView.setVisibility(View.VISIBLE); } } }
}
|
<?xml version="1.0"
encoding="utf-8"?><!-- Copyright 2016 Google Inc.
Licensed under the Apache License, Version 2.0 (the
"License");
you may not use this file except in compliance with
the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in
writing, software
distributed under the License is distributed on an
"AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express or implied.
See the License for the specific language governing
permissions and
limitations under the License. -->
<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=".SecondActivity">
<TextView
android:id="@+id/text_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:text="@string/text_header"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
<TextView
android:id="@+id/text_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text_header"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/button_second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:onClick="returnReply"
android:text="@string/button_second" />
<EditText
android:id="@+id/editText_second"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@+id/button_second"
android:layout_toStartOf="@id/button_second"
android:hint="@string/editText_second" />
</RelativeLayout>
|
Kemudian
ketikan Script SecondActivity.java dibawah ini :
/*
*
Copyright (C) 2016 Google Inc.
*
*
Licensed under the Apache License, Version 2.0 (the "License");
* you
may not use this file except in compliance with the License.
* You
may obtain a copy of the License at
*
*
http://www.apache.org/licenses/LICENSE-2.0
*
*
Unless required by applicable law or agreed to in writing, software
*
distributed under the License is distributed on an "AS IS" BASIS,
*
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See
the License for the specific language governing permissions and
*
limitations under the License.
*/
package
com.example.android.twoactivities;
import
android.content.Intent;
import
android.os.Bundle;
import
android.support.v7.app.AppCompatActivity;
import
android.view.View;
import
android.widget.EditText;
import
android.widget.TextView;
/**
*
SecondActivity defines the second activity in the app. It is launched from an
intent
* with
a message, and sends an intent back with a second message.
*/
public class
SecondActivity extends AppCompatActivity {
// Unique tag for the intent reply.
public static final String EXTRA_REPLY =
"com.example.android.twoactivities.extra.REPLY";
// EditText for the reply.
private EditText mReply;
/**
* Initializes the activity.
*
* @param savedInstanceState The current state data
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
// Initialize view variables.
mReply = (EditText) findViewById(R.id.editText_second);
// Get the intent that launched this activity, and the
message in
// the intent extra.
Intent intent = getIntent();
String message =
intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Put that message into the text_message TextView
TextView textView = (TextView)
findViewById(R.id.text_message);
if (textView != null) {
textView.setText(message);
}
}
/**
* Handle the onClick for the "Reply" button. Gets the
message from the second EditText,
* creates an intent, and returns that message back to the main
activity.
*
* @param view The view (Button) that was clicked.
*/
public void returnReply(View view) {
// Get the reply message from the edit text.
String reply = mReply.getText().toString();
// Create a new intent for the reply, add the reply message
to it as an extra,
// set the intent result, and close the activity.
Intent replyIntent = new Intent();
replyIntent.putExtra(EXTRA_REPLY, reply);
setResult(RESULT_OK, replyIntent);
finish();
}
}
|
<!-- Copyright 2016 Google Inc.
Licensed under the Apache License, Version 2.0 (the
"License");
you may not use this file except in compliance with the
License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software
distributed under the License is distributed on an "AS
IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied.
See the License for the specific language governing
permissions and
limitations under the License. -->
<resources>
<!-- Title of app -->
<string name="app_name">Two
Activities</string>
<!-- Title of second activity -->
<string
name="activity2_name">Second Activity</string>
<!-- Message header text (in second
activity) [CHAR LIMIT=30]-->
<string
name="text_header">Message Received</string>
<!-- Button label in main activity [CHAR
LIMIT=10]-->
<string
name="button_main">Send</string>
<!-- Hint for message edit text in main
activity [CHAR LIMIT=30]-->
<string
name="editText_main">Enter Your Message Here</string>
<!-- Button label in second activity [CHAR
LIMIT=10]-->
<string name="button_second">Reply</string>
<!-- Hint for reply edit text in second
activity [CHAR LIMIT=30]-->
<string
name="editText_second">Enter Your Reply Here</string>
<!-- Reply header text in main activity
[CHAR LIMIT=30]-->
<string name="text_header_reply">Reply
Received</string>
</resources>
|
Tidak ada komentar:
Posting Komentar