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

From OnnoWiki
Jump to navigation Jump to search
(New page: 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...)
 
 
Line 13: Line 13:
 
build.gradle
 
build.gradle
  
apply plugin: 'android'
+
apply plugin: 'android'
 
+
android {
+
android {
    compileSdkVersion 19
+
    compileSdkVersion 19
    buildToolsVersion '19.1.0'
+
    buildToolsVersion '19.1.0'
 
+
    defaultConfig {
+
    defaultConfig {
        minSdkVersion 14
+
        minSdkVersion 14
        targetSdkVersion 19
+
        targetSdkVersion 19
        versionCode 1
+
        versionCode 1
        versionName "1.0"
+
        versionName "1.0"
    }
+
    }
    buildTypes {
+
    buildTypes {
        release {
+
        release {
            runProguard false
+
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
+
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
+
        }
    }
+
    }
}
+
}
 
+
dependencies {
+
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
+
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:19.+'
+
    compile 'com.android.support:appcompat-v7:19.+'
}
+
}
  
 
Human.java
 
Human.java
  
package com.newthinktank.switchingscreens.app;
+
package com.newthinktank.switchingscreens.app;
 
+
import java.io.Serializable;
+
import java.io.Serializable;
 
+
public class Human implements Serializable{
+
public class Human implements Serializable{
 
+
    private double height, weight;
+
    private double height, weight;
    private String name = "";
+
    private String name = "";
 
+
    public Human(double height, double weight, String name) {
+
    public Human(double height, double weight, String name) {
        this.height = height;
+
        this.height = height;
        this.weight = weight;
+
        this.weight = weight;
        this.name = name;
+
        this.name = name;
    }
+
    }
 
+
    public double getHeight() {
+
    public double getHeight() {
        return height;
+
        return height;
    }
+
    }
 
+
    public void setHeight(double height) {
+
    public void setHeight(double height) {
        this.height = height;
+
        this.height = height;
    }
+
    }
 
+
    public double getWeight() {
+
    public double getWeight() {
        return weight;
+
        return weight;
    }
+
    }
 
+
    public void setWeight(double weight) {
+
    public void setWeight(double weight) {
        this.weight = weight;
+
        this.weight = weight;
    }
+
    }
 
+
    public String getName() {
+
    public String getName() {
        return name;
+
        return name;
    }
+
    }
 
+
    public void setName(String name) {
+
    public void setName(String name) {
        this.name = name;
+
        this.name = name;
    }
+
    }
}
+
}
  
 
MainActivity.java
 
MainActivity.java
  
package com.newthinktank.switchingscreens.app;
+
package com.newthinktank.switchingscreens.app;
 
+
import android.content.Intent;
+
import android.content.Intent;
import android.os.Bundle;
+
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
+
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
+
import android.view.Menu;
import android.view.MenuItem;
+
import android.view.MenuItem;
import android.view.View;
+
import android.view.View;
import android.widget.TextView;
+
import android.widget.TextView;
 
+
 
 
+
public class MainActivity extends ActionBarActivity {
+
public class MainActivity extends ActionBarActivity {
 
+
    @Override
+
    @Override
    protected void onCreate(Bundle savedInstanceState) {
+
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
+
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
+
        setContentView(R.layout.activity_main);
    }
+
    }
 
+
 
+
    @Override
+
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
+
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
+
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
+
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
+
        return true;
    }
+
    }
 
+
    @Override
+
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
+
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
+
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
+
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
+
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
+
        int id = item.getItemId();
        if (id == R.id.action_settings) {
+
        if (id == R.id.action_settings) {
            return true;
+
            return true;
        }
+
        }
        return super.onOptionsItemSelected(item);
+
        return super.onOptionsItemSelected(item);
    }
+
    }
 
+
    public void onGetNameClick(View view) {
+
    public void onGetNameClick(View view) {  
 
+
        /*Intent getNameScreenIntent = new Intent(this,
+
        /*Intent getNameScreenIntent = new Intent(this,
                SecondScreen.class);*/
+
                SecondScreen.class);*/
 
+
        final int result = 1;
+
        final int result = 1;
 
+
        // getNameScreenIntent.putExtra("callingActivity", "MainActivity");
+
        // getNameScreenIntent.putExtra("callingActivity", "MainActivity");
 
+
        Human bob = new Human(6.25, 185, "Bob");
+
        Human bob = new Human(6.25, 185, "Bob");
 
+
        Intent sendBob = new Intent(this, SecondScreen.class);
+
        Intent sendBob = new Intent(this, SecondScreen.class);
 
+
        sendBob.putExtra("humanBob", bob);
+
        sendBob.putExtra("humanBob", bob);
 
+
        startActivityForResult(sendBob, result);
+
        startActivityForResult(sendBob, result);
 
+
    }
+
    }
 
+
    @Override
+
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
+
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
+
        super.onActivityResult(requestCode, resultCode, data);
 
+
        TextView usersNameMessage = (TextView)
+
        TextView usersNameMessage = (TextView)
                findViewById(R.id.users_name_message);
+
                findViewById(R.id.users_name_message);
 
+
        String nameSentBack = data.getStringExtra("UsersName");
+
        String nameSentBack = data.getStringExtra("UsersName");
 
+
        usersNameMessage.append(" " + nameSentBack);
+
        usersNameMessage.append(" " + nameSentBack);
 
+
    }
+
    }
 
+
 
+
}
+
}
  
 
SecondScreen.java
 
SecondScreen.java
  
package com.newthinktank.switchingscreens.app;
+
package com.newthinktank.switchingscreens.app;
 
+
import android.app.Activity;
+
import android.app.Activity;
import android.content.Intent;
+
import android.content.Intent;
import android.os.Bundle;
+
import android.os.Bundle;
import android.view.View;
+
import android.view.View;
import android.widget.EditText;
+
import android.widget.EditText;
import android.widget.TextView;
+
import android.widget.TextView;
 
+
 
 
+
public class SecondScreen extends Activity{
+
public class SecondScreen extends Activity{
    @Override
+
    @Override
    protected void onCreate(Bundle savedInstanceState) {
+
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
+
        super.onCreate(savedInstanceState);
 
+
        setContentView(R.layout.second_layout);
+
        setContentView(R.layout.second_layout);
 
+
        Intent activityThatCalled = getIntent();
+
        Intent activityThatCalled = getIntent();
 
+
        // String previousActivity = activityThatCalled.getExtras().getString("callingActivity");
+
        // String previousActivity = activityThatCalled.getExtras().getString("callingActivity");
 
+
        Human bob = (Human) activityThatCalled.getSerializableExtra("humanBob");
+
        Human bob = (Human) activityThatCalled.getSerializableExtra("humanBob");
 
+
        TextView callingActivityMessage = (TextView)
+
        TextView callingActivityMessage = (TextView)
                findViewById(R.id.calling_activity_info_text_view);
+
                findViewById(R.id.calling_activity_info_text_view);
 
+
        callingActivityMessage.append(bob.getName() + " " +
+
        callingActivityMessage.append(bob.getName() + " " +
        bob.getHeight() + " ft " + bob.getWeight() + " lbs");
+
        bob.getHeight() + " ft " + bob.getWeight() + " lbs");
    }
+
    }
 
+
    public void onSendUsersName(View view) {
+
    public void onSendUsersName(View view) {
 
+
        EditText usersNameET = (EditText)
+
        EditText usersNameET = (EditText)
                findViewById(R.id.users_name_edit_text);
+
                findViewById(R.id.users_name_edit_text);
 
+
        String usersName = String.valueOf(usersNameET.getText());
+
        String usersName = String.valueOf(usersNameET.getText());
 
+
        Intent goingBack = new Intent();
+
        Intent goingBack = new Intent();  
 
+
        goingBack.putExtra("UsersName", usersName);
+
        goingBack.putExtra("UsersName", usersName);
 
+
        setResult(RESULT_OK, goingBack);
+
        setResult(RESULT_OK, goingBack);
 
+
        finish();
+
        finish();
    }
+
    }
}
+
}
 
 
- See more at: http://www.newthinktank.com/2014/07/make-android-apps-6/#sthash.Sa8GzQnD.dpuf
 
 
 
 
 
  
  
Line 214: Line 210:
  
 
* http://www.newthinktank.com/2014/07/make-android-apps-6/
 
* http://www.newthinktank.com/2014/07/make-android-apps-6/
 +
* http://www.newthinktank.com/2014/07/make-android-apps-6/#sthash.Sa8GzQnD.dpuf

Latest revision as of 20:39, 20 April 2015

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