Android Studio: Cara Membuat Android Apps 6

From OnnoWiki
Jump to navigation Jump to search

In this tutorial I’ll show you how to fix Android Studio errors that you guys are having trouble with. I’ll also show how to pass objects between screens in Android.

I show how to set up Android Studio 0.8, show the files you need to install in the SDK Manager, and how to set up a working Android Virtual Device. I’ll also show how to fix the UIDs are inconsistent error. You can download Android Studio here. All the code used follows the tutorial below.

If you like videos like this, it helps to submit to Google Plus with a click here

If you want a chance to win a Samsung Galaxy Note 3 and Galaxy Gear Smart Watch check out my new contest.

All of the Code from the Video

This code is in addition to the code covered in part 5 of my How to Make Android Apps tutorial.

build.gradle

apply plugin: 'android'

android {
    compileSdkVersion 19
    buildToolsVersion '19.1.0'

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:19.+'
}

Human.java

package com.newthinktank.switchingscreens.app;

import java.io.Serializable;

public class Human implements Serializable{

    private double height, weight;
    private String name = "";

    public Human(double height, double weight, String name) {
        this.height = height;
        this.weight = weight;
        this.name = name;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

MainActivity.java

package com.newthinktank.switchingscreens.app;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
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) { 

        /*Intent getNameScreenIntent = new Intent(this,
                SecondScreen.class);*/

        final int result = 1;

        // getNameScreenIntent.putExtra("callingActivity", "MainActivity");

        Human bob = new Human(6.25, 185, "Bob");

        Intent sendBob = new Intent(this, SecondScreen.class);

        sendBob.putExtra("humanBob", bob);

        startActivityForResult(sendBob, result);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        TextView usersNameMessage = (TextView)
                findViewById(R.id.users_name_message);

        String nameSentBack = data.getStringExtra("UsersName");

        usersNameMessage.append(" " + nameSentBack);

    }


}

SecondScreen.java

package com.newthinktank.switchingscreens.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);

        setContentView(R.layout.second_layout);

        Intent activityThatCalled = getIntent();

        // String previousActivity = activityThatCalled.getExtras().getString("callingActivity");

        Human bob = (Human) activityThatCalled.getSerializableExtra("humanBob");

        TextView callingActivityMessage = (TextView)
                findViewById(R.id.calling_activity_info_text_view);

        callingActivityMessage.append(bob.getName() + " " +
        bob.getHeight() + " ft " + bob.getWeight() + " lbs");
    }

    public void onSendUsersName(View view) {

        EditText usersNameET = (EditText)
                findViewById(R.id.users_name_edit_text);

        String usersName = String.valueOf(usersNameET.getText());

        Intent goingBack = new Intent(); 

        goingBack.putExtra("UsersName", usersName);

        setResult(RESULT_OK, goingBack);

        finish();
    }
}


Referensi