Difference between revisions of "Android Studio: Cara Membuat Android Apps 5"

From OnnoWiki
Jump to navigation Jump to search
(New page: 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...)
 
Line 9: Line 9:
 
second_layout.xml
 
second_layout.xml
  
01 <?xml version="1.0" encoding="utf-8"?>
+
<?xml version="1.0" encoding="utf-8"?>
02 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03     android:layout_width="wrap_content"
+
    android:layout_width="wrap_content"
04     android:layout_height="wrap_content"
+
    android:layout_height="wrap_content"
05     android:orientation="vertical"
+
    android:orientation="vertical"
06     android:gravity="left"
+
    android:gravity="left"
07     android:padding="20dp">
+
    android:padding="20dp">
08  
+
   
09     <LinearLayout
+
    <LinearLayout
10         android:layout_width="wrap_content"
+
        android:layout_width="wrap_content"
11         android:layout_height="wrap_content"
+
        android:layout_height="wrap_content"
12         android:orientation="horizontal">
+
        android:orientation="horizontal">
13
+
 
14     <TextView
+
    <TextView
15         android:layout_width="wrap_content"
+
        android:layout_width="wrap_content"
16         android:layout_height="wrap_content"
+
        android:layout_height="wrap_content"
17         android:text="@string/enter_name_text_view"/>
+
        android:text="@string/enter_name_text_view"/>
18
+
 
19     <EditText
+
    <EditText
20         android:layout_width="185dp"
+
        android:layout_width="185dp"
21         android:layout_height="wrap_content"
+
        android:layout_height="wrap_content"
22         android:id="@+id/users_name_edit_text"/>
+
        android:id="@+id/users_name_edit_text"/>
23
+
 
24     </LinearLayout>
+
    </LinearLayout>
25
+
 
26     <LinearLayout
+
    <LinearLayout
27         android:layout_width="wrap_content"
+
        android:layout_width="wrap_content"
28         android:layout_height="wrap_content"
+
        android:layout_height="wrap_content"
29         android:orientation="horizontal"
+
        android:orientation="horizontal"
30         android:paddingTop="10dp">
+
        android:paddingTop="10dp">
31
+
 
32         <Button
+
        <Button
33             android:layout_width="wrap_content"
+
            android:layout_width="wrap_content"
34             android:layout_height="wrap_content"
+
            android:layout_height="wrap_content"
35             android:text="Enter"
+
            android:text="Enter"
36             android:onClick="onSendUsersName"/>
+
            android:onClick="onSendUsersName"/>
37
+
 
38         <TextView
+
        <TextView
39             android:id="@+id/calling_activity_info_text_view"
+
            android:id="@+id/calling_activity_info_text_view"
40             android:layout_width="wrap_content"
+
            android:layout_width="wrap_content"
41             android:layout_height="wrap_content"
+
            android:layout_height="wrap_content"
42             android:text="@string/calling_activity_message"/>
+
            android:text="@string/calling_activity_message"/>
43  
+
   
44     </LinearLayout>
+
    </LinearLayout>
45  
+
   
46 </LinearLayout>
+
</LinearLayout>
  
 
activity_main.xml
 
activity_main.xml
  
01 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
02     xmlns:tools="http://schemas.android.com/tools"
+
    xmlns:tools="http://schemas.android.com/tools"
03     android:layout_width="match_parent"
+
    android:layout_width="match_parent"
04     android:layout_height="match_parent"
+
    android:layout_height="match_parent"
05     android:orientation="vertical"
+
    android:orientation="vertical"
06     android:gravity="center|top"
+
    android:gravity="center|top"
07     android:padding="20dp"
+
    android:padding="20dp"
08     tools:context="com.newthinktank.switchingscreens2.app.MainActivity">
+
     tools:context="com.newthinktank.switchingscreens2.app.MainActivity">
09  
+
   
10     <TextView
+
    <TextView
11         android:text="@string/get_the_name_text_view"
+
        android:text="@string/get_the_name_text_view"
12         android:layout_width="wrap_content"
+
        android:layout_width="wrap_content"
13         android:layout_height="wrap_content"
+
        android:layout_height="wrap_content"
14         android:textStyle="bold"
+
        android:textStyle="bold"
15         android:textSize="20sp"/>
+
        android:textSize="20sp"/>
16
+
 
17     <Button
+
    <Button
18         android:layout_width="wrap_content"
+
        android:layout_width="wrap_content"
19         android:layout_height="wrap_content"
+
        android:layout_height="wrap_content"
20         android:text="Go Get It"
+
        android:text="Go Get It"
21         android:layout_marginTop="20dp"
+
        android:layout_marginTop="20dp"
22         android:onClick="onGetNameClick"/>
+
        android:onClick="onGetNameClick"/>
23
+
 
24     <TextView
+
    <TextView
25         android:text="@string/users_name_sent_back"
+
        android:text="@string/users_name_sent_back"
26         android:layout_width="wrap_content"
+
        android:layout_width="wrap_content"
27         android:layout_height="wrap_content"
+
        android:layout_height="wrap_content"
28         android:textStyle="bold"
+
        android:textStyle="bold"
29         android:textSize="20sp"
+
        android:textSize="20sp"
30         android:layout_marginTop="20dp"
+
        android:layout_marginTop="20dp"
31         android:id="@+id/users_name_message"/>
+
        android:id="@+id/users_name_message"/>
32  
+
 
33 </LinearLayout>
+
  </LinearLayout>
  
 
MainActivity.java
 
MainActivity.java
  
01 package com.newthinktank.switchingscreens2.app;
+
package com.newthinktank.switchingscreens2.app;
02  
+
 
03 import android.content.Intent;
+
  import android.content.Intent;
04 import android.support.v7.app.ActionBarActivity;
+
import android.support.v7.app.ActionBarActivity;
05 import android.os.Bundle;
+
import android.os.Bundle;
06 import android.view.Menu;
+
import android.view.Menu;
07 import android.view.MenuItem;
+
import android.view.MenuItem;
08 import android.view.View;
+
import android.view.View;
09 import android.widget.TextView;
+
import android.widget.TextView;
10  
+
   
11  
+
 
12 public class MainActivity extends ActionBarActivity {
+
  public class MainActivity extends ActionBarActivity {
 
13  
 
13  
 
14     @Override
 
14     @Override
Line 262: Line 262:
 
29  
 
29  
 
30 </manifest>
 
30 </manifest>
- See more at: http://www.newthinktank.com/2014/06/make-android-apps-5/#sthash.rMWGEHOx.dpuf
 
  
  
Line 270: Line 269:
  
 
* http://www.newthinktank.com/2014/06/make-android-apps-5/
 
* http://www.newthinktank.com/2014/06/make-android-apps-5/
 +
* http://www.newthinktank.com/2014/06/make-android-apps-5/#sthash.rMWGEHOx.dpuf

Revision as of 19:51, 20 April 2015

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 {

13 14 @Override 15 protected void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.activity_main); 18 } 19 20 21 @Override 22 public boolean onCreateOptionsMenu(Menu menu) { 23 // Inflate the menu; this adds items to the action bar if it is present. 24 getMenuInflater().inflate(R.menu.main, menu); 25 return true; 26 } 27 28 @Override 29 public boolean onOptionsItemSelected(MenuItem item) { 30 // Handle action bar item clicks here. The action bar will 31 // automatically handle clicks on the Home/Up button, so long 32 // as you specify a parent activity in AndroidManifest.xml. 33 int id = item.getItemId(); 34 if (id == R.id.action_settings) { 35 return true; 36 } 37 return super.onOptionsItemSelected(item); 38 } 39 40 public void onGetNameClick(View view) { 41 42 // We have to state that are intention is to open another Activity. We do so 43 // by passing a Context and the Activity that we want to open 44 45 Intent getNameScreenIntent = new Intent(this, SecondScreen.class); 46 47 // We ask for the Activity to start and don't expect a result to be sent back 48 // startActivity(getNameScreenIntent); 49 50 // We use startActivityForResult when we expect a result to be sent back 51 52 final int result = 1; 53 54 // To send data use putExtra with a String name followed by its value 55 56 getNameScreenIntent.putExtra("callingActivity", "MainActivity"); 57 58 startActivityForResult(getNameScreenIntent, result); 59 60 } 61 62 @Override 63 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 64 super.onActivityResult(requestCode, resultCode, data); 65 66 // Create the TextView so I can put the users name on it 67 TextView usersNameMessage = (TextView) findViewById(R.id.users_name_message); 68 69 // Get the users name from the previous Activity 70 String nameSentBack = data.getStringExtra("UsersName"); 71 72 // Add the users name to the end of the textView 73 usersNameMessage.append(" " + nameSentBack); 74 75 } 76 }

SecondScreen.java

01 package com.newthinktank.switchingscreens2.app; 02 03 04 import android.app.Activity; 05 import android.content.Intent; 06 import android.os.Bundle; 07 import android.view.View; 08 import android.widget.EditText; 09 import android.widget.TextView; 10 11 public class SecondScreen extends Activity{ 12 @Override 13 protected void onCreate(Bundle savedInstanceState) { 14 super.onCreate(savedInstanceState); 15 16 // Set the layout for the layout we created 17 setContentView(R.layout.second_layout); 18 19 // Get the Intent that called for this Activity to open 20 21 Intent activityThatCalled = getIntent(); 22 23 // Get the data that was sent 24 25 String previousActivity = activityThatCalled.getExtras().getString("callingActivity"); 26 27 TextView callingActivityMessage = (TextView) 28 findViewById(R.id.calling_activity_info_text_view); 29 30 callingActivityMessage.append(" " + previousActivity); 31 } 32 33 public void onSendUsersName(View view) { 34 35 // Get the users name from the EditText 36 EditText usersNameET = (EditText) findViewById(R.id.users_name_edit_text); 37 38 // Get the name typed into the EditText 39 String usersName = String.valueOf(usersNameET.getText()); 40 41 // Define our intention to go back to ActivityMain 42 Intent goingBack = new Intent(); 43 44 // Define the String name and the value to assign to it 45 goingBack.putExtra("UsersName", usersName); 46 47 // Sends data back to the parent and can use RESULT_CANCELED, RESULT_OK, or any 48 // custom values starting at RESULT_FIRST_USER. RESULT_CANCELED is sent if 49 // this Activity crashes 50 setResult(RESULT_OK, goingBack); 51 52 // Close this Activity 53 finish(); 54 55 } 56 }

AndroidManifest.xml

01 <?xml version="1.0" encoding="utf-8"?> 02 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 03 package="com.newthinktank.switchingscreens2.app" > 04 05 <application 06 android:allowBackup="true" 07 android:icon="@drawable/ic_launcher" 08 android:label="@string/app_name" 09 android:theme="@style/AppTheme" > 10 <activity 11 android:name="com.newthinktank.switchingscreens2.app.MainActivity" 12 android:label="@string/app_name" > 13 <intent-filter> 14 <action android:name="android.intent.action.MAIN" /> 15 16 <category android:name="android.intent.category.LAUNCHER" /> 17 </intent-filter> 18 </activity> 19 20 23 24 <activity android:name=".SecondScreen" 25 android:label="Get Name" 26 android:theme="@style/AppTheme"/> 27 28 </application> 29 30 </manifest>



Referensi