Android Studio: Cara Membuat Android Apps 5

From OnnoWiki
Revision as of 20:08, 20 April 2015 by Onnowpurbo (talk | contribs)
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

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

<?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>



Referensi