Ready......Let'sGo Study...!?
Baiklah sebelum langkah langkah dimulai, lebih ke syarat dulu buat Sheet google...
- Buat akun www.google sheet.com
- Buat www.Scripe Google.com
- Aplikasi Postman www.download Postman.com
Buat Project Start New Project. Lanjut
Langkah selanjutnya CreateName Project
Lanjut Pilih API Device ,pilih device sesuai kebutuhan teman teman, kalau saya disi lebih recomended API 14 Karna melihat device hp yang saya pakai lumayan agak rendah jadi saya pilih API 14, jadi bagi teman teman pilih sesuai kebutuhan teman teman :D
Lanjut Pilih Add Layout EmptyActivity_Next
Lanjut Finish
Ready Coding.......
Baiklah teman teman siap siap coding di androidnya, kalau teman teman gak mau ribet saya disini mau berbagi full tutorial teman teman bisa copy source codenya dan bisa di oprek nanti ok....:D
Buat Layout Baru Resource dengan nama add_item; Untuk nambah layout barua clik kanan di layout_New_LayouResourceFile_add_item.xml
add_item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text=" Name"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="16dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="18dp"
android:layout_marginStart="16dp"
android:text="Alamt"
app:layout_constraintBottom_toTopOf="@+id/et_brand"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="16dp" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="18dp"
android:layout_marginStart="16dp"
android:text="No.Hp"
app:layout_constraintBottom_toTopOf="@+id/no"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="16dp" />
<EditText
android:id="@+id/et_item_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="54dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="@+id/textView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/et_brand"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="168dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/no"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="250dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_add_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:text="Save date"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/et_brand" />
</android.support.constraint.ConstraintLayout>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text=" Name"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="16dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="18dp"
android:layout_marginStart="16dp"
android:text="Alamt"
app:layout_constraintBottom_toTopOf="@+id/et_brand"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="16dp" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="18dp"
android:layout_marginStart="16dp"
android:text="No.Hp"
app:layout_constraintBottom_toTopOf="@+id/no"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="16dp" />
<EditText
android:id="@+id/et_item_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="54dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="@+id/textView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/et_brand"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="168dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/no"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="250dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_add_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:text="Save date"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/et_brand" />
</android.support.constraint.ConstraintLayout>
Tambahkan Volly Library Grandle app
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0-alpha1'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
testImplementation 'junit:junit:4.12'
implementation 'com.android.volley:volley:1.1.0'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0-alpha1'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
testImplementation 'junit:junit:4.12'
implementation 'com.android.volley:volley:1.1.0'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
Buata AddItem.java Dengan klik kanan di file java New_JavaClas_AddItem.java
AddItem.java
package g.danytux.sheet;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.RetryPolicy;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import java.util.HashMap;
import java.util.Map;
public class AddItem extends AppCompatActivity implements View.OnClickListener {
EditText editTextItemName,editTextBrand,editTextno;
Button buttonAddItem;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_item);
editTextItemName = (EditText)findViewById(R.id.et_item_name);
editTextBrand = (EditText)findViewById(R.id.et_brand);
editTextno = (EditText)findViewById(R.id.no);
buttonAddItem = (Button)findViewById(R.id.btn_add_item);
buttonAddItem.setOnClickListener(this);
}
//This is the part where data is transafeered from Your Android phone to Sheet by using HTTP Rest API calls
private void addItemToSheet() {
final ProgressDialog loading = ProgressDialog.show(this,"Adding Item","Please wait");
final String name = editTextItemName.getText().toString().trim();
final String brand = editTextBrand.getText().toString().trim();
final String no = editTextno.getText().toString().trim();
StringRequest stringRequest = new StringRequest(Request.Method.POST, "https://script.google.com/macros/s/AKfycbwwU80HBqnsVj_dBrEuukLin248chAcIhYNHogtnRJyjxp2zD02/exec",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
loading.dismiss();
Toast.makeText(AddItem.this,response,Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}
) {
@Override
protected Map<String, String> getParams() {
Map<String, String> parmas = new HashMap<>();
//here we pass params
parmas.put("action","addItem");
parmas.put("itemName",name);
parmas.put("brand",brand);
parmas.put("no",no);
return parmas;
}
};
int socketTimeOut = 50000;// u can change this .. here it is 50 seconds
RetryPolicy retryPolicy = new DefaultRetryPolicy(socketTimeOut, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
stringRequest.setRetryPolicy(retryPolicy);
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(stringRequest);
}
@Override
public void onClick(View v) {
if(v==buttonAddItem){
addItemToSheet();
//Define what to do when button is clicked
}
}
}
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.RetryPolicy;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import java.util.HashMap;
import java.util.Map;
public class AddItem extends AppCompatActivity implements View.OnClickListener {
EditText editTextItemName,editTextBrand,editTextno;
Button buttonAddItem;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_item);
editTextItemName = (EditText)findViewById(R.id.et_item_name);
editTextBrand = (EditText)findViewById(R.id.et_brand);
editTextno = (EditText)findViewById(R.id.no);
buttonAddItem = (Button)findViewById(R.id.btn_add_item);
buttonAddItem.setOnClickListener(this);
}
//This is the part where data is transafeered from Your Android phone to Sheet by using HTTP Rest API calls
private void addItemToSheet() {
final ProgressDialog loading = ProgressDialog.show(this,"Adding Item","Please wait");
final String name = editTextItemName.getText().toString().trim();
final String brand = editTextBrand.getText().toString().trim();
final String no = editTextno.getText().toString().trim();
StringRequest stringRequest = new StringRequest(Request.Method.POST, "https://script.google.com/macros/s/AKfycbwwU80HBqnsVj_dBrEuukLin248chAcIhYNHogtnRJyjxp2zD02/exec",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
loading.dismiss();
Toast.makeText(AddItem.this,response,Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}
) {
@Override
protected Map<String, String> getParams() {
Map<String, String> parmas = new HashMap<>();
//here we pass params
parmas.put("action","addItem");
parmas.put("itemName",name);
parmas.put("brand",brand);
parmas.put("no",no);
return parmas;
}
};
int socketTimeOut = 50000;// u can change this .. here it is 50 seconds
RetryPolicy retryPolicy = new DefaultRetryPolicy(socketTimeOut, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
stringRequest.setRetryPolicy(retryPolicy);
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(stringRequest);
}
@Override
public void onClick(View v) {
if(v==buttonAddItem){
addItemToSheet();
//Define what to do when button is clicked
}
}
}
Add Butoon MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_add_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="130dp"
android:layout_marginTop="194dp"
android:text="Add Item"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginRight="130dp" />
</android.support.constraint.ConstraintLayout>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_add_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="130dp"
android:layout_marginTop="194dp"
android:text="Add Item"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginRight="130dp" />
</android.support.constraint.ConstraintLayout>
ActivityMain.java
//package g.danytux.sheet;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button buttonAddItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonAddItem = (Button)findViewById(R.id.btn_add_item);
buttonAddItem.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v==buttonAddItem){
Intent intent = new Intent(getApplicationContext(),AddItem.class);
startActivity(intent);
}
}
}
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button buttonAddItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonAddItem = (Button)findViewById(R.id.btn_add_item);
buttonAddItem.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v==buttonAddItem){
Intent intent = new Intent(getApplicationContext(),AddItem.class);
startActivity(intent);
}
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="g.danytux.sheet">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AddItem"/>
</application>
</manifest>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="g.danytux.sheet">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AddItem"/>
</application>
</manifest>
Demo App sheetgoogle
Buat app scrip baru dengan kode di bawah ini, teman teman copy saja code yang ada di bawah,dan detail codingannya yah biar gak error,
Ubah URL Spread Sheet ke URL Spread Sheet Anda yang Anda buat pada langkah 8
Pastikan parameter Anda sesuai dengan apa yang pernah Anda kirim dari kode Android.
Contoh e.parameter.brand harus sesuai dengan params.put ("merek", merek)
var ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1NPuQ9JeWqGAfe21rMmdVf08ggm1CRJ-KAUe6ItAFiAY/edit#gid=0");
var sheet = ss.getSheetByName('Items'); // be very careful ... it is the sheet name .. so it should match
function doPost(e){
var action = e.parameter.action;
if(action == 'addItem'){
return addItem(e);
}
}
function addItem(e){
var date = new Date();
var id = "Item"+sheet.getLastRow(); // Item1
var itemName = e.parameter.itemName;
var brand = e.parameter.brand;
var no = e.parameter.no;
sheet.appendRow([date,id,itemName,brand,no]);
return ContentService.createTextOutput("Success").setMimeType(ContentService.MimeType.TEXT);
}
var sheet = ss.getSheetByName('Items'); // be very careful ... it is the sheet name .. so it should match
function doPost(e){
var action = e.parameter.action;
if(action == 'addItem'){
return addItem(e);
}
}
function addItem(e){
var date = new Date();
var id = "Item"+sheet.getLastRow(); // Item1
var itemName = e.parameter.itemName;
var brand = e.parameter.brand;
var no = e.parameter.no;
sheet.appendRow([date,id,itemName,brand,no]);
return ContentService.createTextOutput("Success").setMimeType(ContentService.MimeType.TEXT);
}
Langkah selanjutnya teman teman uji di postman
Langkah selanjutnya test RUN Aplikasi
Demo
Hanya itu yang saya sampaikan semoga bermanfaat kalau ada yang proplem error bisa langsung koment dibawah .....wassalam
Post a Comment