2.1: Membuat dan Memulai Aktivitas
Tugas 1. Membuat proyek TwoActivities
1.1 Membuat proyek TwoActivities
Menggunakan cara yang sama dengan pembuatan proyek sebelumnya
1.2 Definisikan layout untuk aktivitas utama
1.3 Mendefinisikan tindakan tombol
package com.example.safiraaini.twoactivities;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private static final String LOG_TAG =
MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void launchSecondActivity(View view) {
Log.d(LOG_TAG, “Button clicked!”);
}
}
Hasil :
Tugas 2. Membuat dan meluncurkan aktivitas kedua
2.1 Membuat aktivitas kedua
2.2 Memodifikasi manifes Android
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.safiraaini.twoactivities"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".SecondActivity" android:label="@string/activity2_name" android:parentActivityName=".MainActivity"> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.example.safiraaini.twoactivities.MainActivity" /> </activity> </application> </manifest>
2.3 Mendefinisikan layout untuk aktivitas utama
2.4 Menambahkan intent ke aktivitas utama
Pada MainActivity.java:
package com.example.safiraaini.twoactivities; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; public class MainActivity extends AppCompatActivity { private static final String LOG_TAG = MainActivity.class.getSimpleName(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void launchSecondActivity(View view) { Log.d(LOG_TAG, "Button clicked!"); Intent intent = new Intent(this, SecondActivity.class); startActivity(intent); } }
Pada second_activity.xml
<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:app=”http://schemas.android.com/apk/res-auto”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”com.example.safiraaini.twoactivities.SecondActivity”>
<TextView
android:id=”@+id/text_header”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentLeft=”true”
android:layout_alignParentStart=”true”
android:layout_alignParentTop=”true”
android:layout_marginLeft=”@dimen/left_Margin”
android:layout_marginStart=”@dimen/start_Margin”
android:layout_marginTop=”@dimen/top_Margin”
android:text=”@string/text_header”
android:textAppearance=”?android:attr/textAppearanceMedium”
android:textStyle=”bold”
tools:layout_editor_absoluteX=”16dp”
tools:layout_editor_absoluteY=”6dp” />
</RelativeLayout>
Hasil :
Tugas 3. Mengirim data dari aktivitas utama ke aktivitas kedua
3.1 Menambahkan EditText ke layout aktivitas utama
3.2 Menambahkan string ke ekstra intent aktivitas utama
3.3 Menambahkan TextView ke aktivitas kedua untuk pesan
3.4 Memodifikasi aktivitas kedua untuk mendapatkan ekstra dan menampilkan pesan
package com.example.safiraaini.twoactivities; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; public class SecondActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); Intent intent = getIntent(); String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE); TextView textView = (TextView) findViewById(R.id.text_Message); textView.setText(message); } }
Tugas 4. Mengembalikan data ke aktivitas utama
4.1 Menambahkan EditText dan Tombol ke layout aktivitas kedua
4.2 Membuat intent respons dalam aktivitas kedua
4.3 Menambahkan TextViews ke layout aktivitas utama untuk menampilkan balasan
Activity_main.xml :
<?xml version="1.0" encoding="utf-8"?> <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" tools:context="com.example.safiraaini.twoactivities.MainActivity"> <TextView android:id="@+id/text_header_reply" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:text="@string/text_header_reply" 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_alignEnd="@+id/text_header" android:layout_alignRight="@+id/text_header" android:layout_below="@+id/text_header_reply" android:layout_marginStart="20dp" android:layout_marginLeft="45dp" 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:layout_marginBottom="13dp" android:layout_marginEnd="18dp" android:layout_marginRight="18dp" 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_toLeftOf="@+id/button_main" android:layout_toStartOf="@+id/button_main" android:layout_marginBottom="13dp" android:layout_marginLeft="18dp" android:layout_alignParentBottom="true" android:ems="10" android:inputType="textPersonName" android:hint="@string/editText_main" /> </RelativeLayout>
Activity_second.xml
<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:app=”http://schemas.android.com/apk/res-auto”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”com.example.safiraaini.twoactivities.SecondActivity”>
<TextView
android:id=”@+id/text_header”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentLeft=”true”
android:layout_alignParentStart=”true”
android:layout_alignParentTop=”true”
android:layout_marginLeft=”@dimen/left_Margin”
android:layout_marginStart=”@dimen/start_Margin”
android:layout_marginTop=”@dimen/top_Margin”
android:text=”@string/text_header”
android:textStyle=”bold”
tools:layout_editor_absoluteX=”16dp”
tools:layout_editor_absoluteY=”6dp” />
<TextView
android:id=”@+id/text_Message”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignEnd=”@+id/text_header”
android:layout_alignRight=”@+id/text_header”
android:layout_below=”@+id/text_header”
android:layout_marginEnd=”45dp”
android:layout_marginRight=”45dp”
/>
<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:layout_marginBottom=”13dp”
android:layout_marginEnd=”18dp”
android:layout_marginRight=”18dp”
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_toLeftOf=”@+id/button_second”
android:layout_toStartOf=”@+id/button_second”
android:layout_marginBottom=”13dp”
android:layout_marginLeft=”18dp”
android:layout_alignParentBottom=”true”
android:ems=”10″
android:inputType=”textPersonName”
android:hint=”@string/editText_second” />
</RelativeLayout>
MainActivity.java
package com.example.safiraaini.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; public class MainActivity extends AppCompatActivity { private static final String LOG_TAG = MainActivity.class.getSimpleName(); public static final String EXTRA_MESSAGE = "com.example.safiraaini.twoactivities.extra.MESSAGE"; public static final int TEXT_REQUEST = 1; private EditText mMessageEditText; private TextView mReplyHeadTextView; private TextView mReplyTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mMessageEditText = (EditText) findViewById(R.id.editText_main); mReplyHeadTextView = (TextView) findViewById(R.id.text_header_reply); mReplyTextView = (TextView) findViewById(R.id.text_message_reply); Log.d(LOG_TAG, "-------"); Log.d(LOG_TAG, "onCreate"); if (savedInstanceState != null) { boolean isVisible = savedInstanceState.getBoolean("reply_visible"); // Show both the header and the message views. If isVisible is // false or missing from the bundle, use the default layout. if (isVisible) { mReplyHeadTextView.setVisibility(View.VISIBLE); mReplyTextView.setText(savedInstanceState.getString("reply_text")); mReplyTextView.setVisibility(View.VISIBLE); } } } @Override public void onStart(){ super.onStart(); Log.d(LOG_TAG, "onStart"); } @Override public void onRestart() { super.onRestart(); Log.d(LOG_TAG, "onRestart"); } @Override public void onResume() { super.onResume(); Log.d(LOG_TAG, "onResume"); } @Override public void onPause() { super.onPause(); Log.d(LOG_TAG, "onPause"); } @Override public void onStop() { super.onStop(); Log.d(LOG_TAG, "onStop"); } @Override public void onDestroy() { super.onDestroy(); Log.d(LOG_TAG, "onDestroy"); } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); // If the heading is visible, we have a message that needs to be saved. // Otherwise we're still using default layout. if (mReplyHeadTextView.getVisibility() == View.VISIBLE) { outState.putBoolean("reply_visible", true); outState.putString("reply_text", mReplyTextView.getText().toString()); } } 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); } public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == TEXT_REQUEST) { if (resultCode == RESULT_OK) { String reply = data.getStringExtra(SecondActivity.EXTRA_REPLY); mReplyHeadTextView.setVisibility(View.VISIBLE); mReplyTextView.setText(reply); mReplyTextView.setVisibility(View.VISIBLE); } } } }
SecondActivity.java
package com.example.safiraaini.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;
public class SecondActivity extends AppCompatActivity {
public static final String EXTRA_REPLY =
“com.example.safiraaini.twoactivities.extra.REPLY”;
private EditText mReply;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
mReply = (EditText) findViewById(R.id.editText_second);
Intent intent = getIntent();
String message =
intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = (TextView) findViewById(R.id.text_Message);
textView.setText(message);
}
private static final String LOG_TAG =
SecondActivity.class.getSimpleName();
public void returnReply(View view) {
String reply = mReply.getText().toString();
Intent replyIntent = new Intent();
replyIntent.putExtra(EXTRA_REPLY, reply);
setResult(RESULT_OK, replyIntent);
Log.d(LOG_TAG, “End SecondActivity”);
finish();
}
@Override
protected void onStart() {
super.onStart();
Log.d(LOG_TAG, “onStart”);
}
@Override
public void onRestart() {
super.onRestart();
Log.d(LOG_TAG, “onRestart”);
}
@Override
public void onResume() {
super.onResume();
Log.d(LOG_TAG, “onResume”);
}
@Override
public void onPause() {
super.onPause();
Log.d(LOG_TAG, “onPause”);
}
@Override
public void onStop() {
super.onStop();
Log.d(LOG_TAG, “onStop”);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(LOG_TAG, “onDestroy”);
}
}
Hasil :
2.2: Siklus Hidup Aktivitas dan Status Instance
Tugas 1. Menambahkan Callback Siklus Hidup ke TwoActivities
1.2 Mengimplementasikan callback ke MainActivity
package com.example.safiraaini.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; public class MainActivity extends AppCompatActivity { private static final String LOG_TAG = MainActivity.class.getSimpleName(); public static final String EXTRA_MESSAGE = "com.example.safiraaini.twoactivities.extra.MESSAGE"; public static final int TEXT_REQUEST = 1; private EditText mMessageEditText; private TextView mReplyHeadTextView; private TextView mReplyTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mMessageEditText = (EditText) findViewById(R.id.editText_main); mReplyHeadTextView = (TextView) findViewById(R.id.text_header_reply); mReplyTextView = (TextView) findViewById(R.id.text_message_reply); Log.d(LOG_TAG, "-------"); Log.d(LOG_TAG, "onCreate"); } @Override public void onStart(){ super.onStart(); Log.d(LOG_TAG, "onStart"); } @Override public void onRestart() { super.onRestart(); Log.d(LOG_TAG, "onRestart"); } @Override public void onResume() { super.onResume(); Log.d(LOG_TAG, "onResume"); } @Override public void onPause() { super.onPause(); Log.d(LOG_TAG, "onPause"); } @Override public void onStop() { super.onStop(); Log.d(LOG_TAG, "onStop"); } @Override public void onDestroy() { super.onDestroy(); Log.d(LOG_TAG, "onDestroy"); } 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); } public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == TEXT_REQUEST) { if (resultCode == RESULT_OK) { String reply = data.getStringExtra(SecondActivity.EXTRA_REPLY); mReplyHeadTextView.setVisibility(View.VISIBLE); mReplyTextView.setText(reply); mReplyTextView.setVisibility(View.VISIBLE); } } } }
1.3 Mengimplementasikan callback siklus hidup dalam SecondActivity
package com.example.safiraaini.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; public class SecondActivity extends AppCompatActivity { public static final String EXTRA_REPLY = "com.example.safiraaini.twoactivities.extra.REPLY"; private EditText mReply; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); mReply = (EditText) findViewById(R.id.editText_second); Intent intent = getIntent(); String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE); TextView textView = (TextView) findViewById(R.id.text_Message); textView.setText(message); } private static final String LOG_TAG = SecondActivity.class.getSimpleName(); public void returnReply(View view) { String reply = mReply.getText().toString(); Intent replyIntent = new Intent(); replyIntent.putExtra(EXTRA_REPLY, reply); setResult(RESULT_OK, replyIntent); Log.d(LOG_TAG, "End SecondActivity"); finish(); } @Override protected void onStart() { super.onStart(); Log.d(LOG_TAG, "onStart"); } @Override public void onRestart() { super.onRestart(); Log.d(LOG_TAG, "onRestart"); } @Override public void onResume() { super.onResume(); Log.d(LOG_TAG, "onResume"); } @Override public void onPause() { super.onPause(); Log.d(LOG_TAG, "onPause"); } @Override public void onStop() { super.onStop(); Log.d(LOG_TAG, "onStop"); } @Override public void onDestroy() { super.onDestroy(); Log.d(LOG_TAG, "onDestroy"); } }
Tugas 2. Menyimpan dan memulihkan status instance aktivitas.
2.1 Simpan status instance aktivitas dengan onSaveInstanceState()
2.2. Memulihkan status instance aktivitas dalam onCreate()
2.3: Memulai Aktivitas dengan Intent Implisit
Tugas 1. Membuat proyek dan layout baru
1.1 Membuat proyek
Tugas 2. Mengimplementasikan “buka situs web”
2.1 Mendefinisikan metode openWebsite
Tugas 3. Mengimplementasikan “buka lokasi”
3.1 Definisikan metode openLocation
Tugas 4. Mengimplementasikan bagikan teks ini
4.1 Mengimplementasikan metode shareText
Tugas 5. Menerima Intent Implisit
5.1 Membuat Proyek & Layout
5.2 Memodifikasi Manifes Android untuk menambahkan filter intent
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.safiraaini.implicitintents.MainActivity"> <EditText android:id="@+id/website_edittext" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="textPersonName" android:text="@string/edittext_uri" tools:layout_editor_absoluteX="16dp" tools:layout_editor_absoluteY="16dp" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/open_website_button" tools:layout_editor_absoluteX="16dp" tools:layout_editor_absoluteY="62dp" android:layout_marginBottom="24dp" android:onClick="openWebsite"/> <EditText android:id="@+id/location_edittext" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:inputType="textPersonName" android:text="@string/edittext_loc" tools:layout_editor_absoluteX="16dp" tools:layout_editor_absoluteY="123dp" /> <Button android:id="@+id/open_location_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button_loc" tools:layout_editor_absoluteX="16dp" tools:layout_editor_absoluteY="169dp" android:onClick="openLocation"/> <EditText android:id="@+id/share_edittext" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:inputType="textPersonName" android:text="@string/edittext_share" tools:layout_editor_absoluteX="16dp" tools:layout_editor_absoluteY="232dp" /> <Button android:id="@+id/share_text_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button_share" tools:layout_editor_absoluteX="16dp" tools:layout_editor_absoluteY="278dp" android:onClick="shareText"/> </LinearLayout>
MainActivity.java
package com.example.safiraaini.implicitintents; import android.content.Intent; import android.net.Uri; import android.support.v4.app.ShareCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.EditText; public class MainActivity extends AppCompatActivity { private EditText mWebsiteEditText; private EditText mLocationEditText; private EditText mShareTextEditText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mWebsiteEditText = (EditText) findViewById(R.id.website_edittext); mLocationEditText = (EditText) findViewById(R.id.location_edittext); mShareTextEditText = (EditText) findViewById(R.id.share_edittext); } public void openWebsite(View view) { String url = mWebsiteEditText.getText().toString(); // Parse the URI and create the intent. Uri webpage = Uri.parse(url); Intent intent = new Intent(Intent.ACTION_VIEW, webpage); // Find an activity to hand the intent and start that activity. if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } else { Log.d("ImplicitIntents", "Can't handle this intent!"); } } public void openLocation(View view) { String loc = mLocationEditText.getText().toString(); // Parse the location and create the intent. Uri addressUri = Uri.parse("geo:0,0?q=" + loc); Intent intent = new Intent(Intent.ACTION_VIEW, addressUri); // Find an activity to handle the intent, and start that activity. if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } else { Log.d("ImplicitIntents", "Can't handle this intent!"); } } public void shareText(View view) { String txt = mShareTextEditText.getText().toString(); String mimeType = "text/plain"; ShareCompat.IntentBuilder .from(this) .setType(mimeType) .setChooserTitle("Share this text with: ") .setText(txt) .startChooser(); } }
Hasil :
Leave a Reply