Android Studio: Cara Membuat Android Apps 5
Jump to navigation
Jump to search
In this tutorial I’ll show you how to create multiple Android Activities and then how you’d go about passing data between Android Activities. I also cover Intents.
I’ll specifically cover creating multiple Activity layouts, setting up the manifest file, sending data to an Activity when you open it, retrieving data when an Activity closes and what Intents are. All of the code can be found below and it is heavily commented to help you learn.
If you like videos like this, it helps others find it if you share on Google Plus with a click here
Code from the Video
second_layout.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="left" android:padding="20dp"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/enter_name_text_view"/> <EditText android:layout_width="185dp" android:layout_height="wrap_content" android:id="@+id/users_name_edit_text"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingTop="10dp"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Enter" android:onClick="onSendUsersName"/> <TextView android:id="@+id/calling_activity_info_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/calling_activity_message"/> </LinearLayout> </LinearLayout>
activity_main.xml
<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="vertical" android:gravity="center|top" android:padding="20dp" tools:context="com.newthinktank.switchingscreens2.app.MainActivity"> <TextView android:text="@string/get_the_name_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textStyle="bold" android:textSize="20sp"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Go Get It" android:layout_marginTop="20dp" android:onClick="onGetNameClick"/> <TextView android:text="@string/users_name_sent_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textStyle="bold" android:textSize="20sp" android:layout_marginTop="20dp" android:id="@+id/users_name_message"/> </LinearLayout>
MainActivity.java
package com.newthinktank.switchingscreens2.app; import android.content.Intent; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.TextView; public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } public void onGetNameClick(View view) { // We have to state that are intention is to open another Activity. We do so // by passing a Context and the Activity that we want to open Intent getNameScreenIntent = new Intent(this, SecondScreen.class); // We ask for the Activity to start and don't expect a result to be sent back // startActivity(getNameScreenIntent); // We use startActivityForResult when we expect a result to be sent back final int result = 1; // To send data use putExtra with a String name followed by its value getNameScreenIntent.putExtra("callingActivity", "MainActivity"); startActivityForResult(getNameScreenIntent, result); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); // Create the TextView so I can put the users name on it TextView usersNameMessage = (TextView) findViewById(R.id.users_name_message); // Get the users name from the previous Activity String nameSentBack = data.getStringExtra("UsersName"); // Add the users name to the end of the textView usersNameMessage.append(" " + nameSentBack); } }
SecondScreen.java
package com.newthinktank.switchingscreens2.app; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.TextView; public class SecondScreen extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Set the layout for the layout we created setContentView(R.layout.second_layout); // Get the Intent that called for this Activity to open Intent activityThatCalled = getIntent(); // Get the data that was sent String previousActivity = activityThatCalled.getExtras().getString("callingActivity"); TextView callingActivityMessage = (TextView) findViewById(R.id.calling_activity_info_text_view); callingActivityMessage.append(" " + previousActivity); } public void onSendUsersName(View view) { // Get the users name from the EditText EditText usersNameET = (EditText) findViewById(R.id.users_name_edit_text); // Get the name typed into the EditText String usersName = String.valueOf(usersNameET.getText()); // Define our intention to go back to ActivityMain Intent goingBack = new Intent(); // Define the String name and the value to assign to it goingBack.putExtra("UsersName", usersName); // Sends data back to the parent and can use RESULT_CANCELED, RESULT_OK, or any // custom values starting at RESULT_FIRST_USER. RESULT_CANCELED is sent if // this Activity crashes setResult(RESULT_OK, goingBack); // Close this Activity finish(); } }
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.newthinktank.switchingscreens2.app" > <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.newthinktank.switchingscreens2.app.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=".SecondScreen" android:label="Get Name" android:theme="@style/AppTheme"/> </application> </manifest>