Archive for Juni 2018
https://www.petanikode.com/html-canvas/
Langkah Awal select canvas
<!DOCTYPE html>
<html>
<head>
<meta charset goyang dombret="utf-8">
<title>Tutorial HTML 5 Canvas</title>
</head>
<body>
<canvas id="myCanvas" width="640" height="480" style="border:1px solid #000000;">
</canvas>
<script type="text/javascript"> var canvas = document.getElementById("myCanvas"); var ctx = canvas.getContext("2d"); </script>
</body> </html>
Code Code Menggambar pada Canvas
1. ctx.fillRect(x,y,1,1);
menggambar titik 1px
2. ctx.beginPath()
untuk memulai/ membuat garis baru.
3. ctx.moveTo(x,y)
untuk menentukan titik awal
4. ctx.lineTo(x,y)
untuk menentukan titik akhir
5. ctx.stroke()
untuk menggambar garisnya.
6. ctx.strokeRect(x,y,w,h)
persegi dengan garis (outline) saja.
7. ctx.fillRect(x,y,w,h)
persegi dengan polesan warna (default-nya hitam).
8. ctx.clearRect(x,y,w,h)
persegi transparan untuk menghapus.
Seperti biasa, kita harus memberikan titik koordinat x dan y. Sementara nilai w dan h untuk lebar dan tingginya.
9. ctx.closePath()
untuk menggambar garis dari titik terakhir ke titik awal secara otomatis.
10. strokeText("text",x,y)
untuk teks dengan garis luar (outline) saja.
11. fillText("text",x,y)
untuk teks dengan warna (default hitam).
12. ctx.font = "64px Arial";
Jenis dan ukuran font
13. drawImage(img, x, y);
Menambahkan gambar
14. var image= new Image();
image.src="./bird.png";
ctx.drawImage(image,10,10,200,200);
Menambahkan image ke canvas
[WEB] Canvas dan javascript
AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
MainActivity.java
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.*;
import android.widget.*;
public class MainActivity extends Activity {
Button button;
ProgressDialog progressdialog;
public static final int Progress_Dialog_Progress = 0;
String ImageURL ;
URL url;
URLConnection urlconnection ;
int FileSize;
InputStream inputstream;
OutputStream outputstream;
byte dataArray[] = new byte[1024];
long totalSize = 0;
EditText txt, edit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edit = (EditText)findViewById(R.id.edit);
button = (Button)findViewById(R.id.lihat);
txt =(EditText)findViewById(R.id.text);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ImageURL= edit.getText().toString();
new ImageDownloadWithProgressDialog().execute(ImageURL);
}});}
public class ImageDownloadWithProgressDialog extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(Progress_Dialog_Progress);}
@Override
protected String doInBackground(String... aurl) {
int count;
try {
url = new URL(aurl[0]);
urlconnection = url.openConnection();
urlconnection.connect();
FileSize = urlconnection.getContentLength();
inputstream = new BufferedInputStream(url.openStream());
outputstream = new FileOutputStream("/sdcard/downloadurl.txt");
while ((count = inputstream.read(dataArray)) != -1) {
totalSize += count;
publishProgress(""+(int)((totalSize*100)/FileSize));
outputstream.write(dataArray, 0, count);}
outputstream.flush();
outputstream.close();
inputstream.close();
} catch (Exception e) {}
return null;}
protected void onProgressUpdate(String... progress) {
progressdialog.setProgress(Integer.parseInt(progress[0]));}
@Override
protected void onPostExecute(String unused) {
dismissDialog(Progress_Dialog_Progress);
try {
FileReader fr = new FileReader("/sdcard/downloadurl.txt");
BufferedReader br = new BufferedReader(fr);
String text;
while ((text = br.readLine()) != null)
txt.setText(text);}
catch (FileNotFoundException fnfe) {
fnfe.getMessage();}
catch (IOException ioe) {
ioe.getMessage();}
Toast.makeText(MainActivity.this, "Text Downloaded Successfully", Toast.LENGTH_LONG).show();
}} protected Dialog onCreateDialog(int id) {
switch (id) { case Progress_Dialog_Progress:
progressdialog = new ProgressDialog(MainActivity.this);
progressdialog.setMessage("Downloading Text From Server...");
progressdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressdialog.setCancelable(false);
progressdialog.show();
return progressdialog;
default:
return null;}}}
[DROID] Baca Tulis Isi File Via Diwnload File With AsyncTask
public void simpan_data(){
SavePreferences("PrefDemo","save");
}
private void SavePreferences(String key, String value) {
SharedPreferences sharedPreferences = this.getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit(); }
public void lihat_data(){
showPreferences("PrefDemo");
}
private void showPreferences(String key){
SharedPreferences sharedPreferences =
this.getPreferences(Context.MODE_PRIVATE);
String Data = sharedPreferences.getString(key, "");
TexView.setText(Data);
}
[DROID] SavedPreverences
AndroidManifest.xml
MainActivity.java
import android.content.Intent;
import android.app.*;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
// nama kelas harus sama dengan nama file nya
public class MainActivity extends Activity {
Button bKirim;
RadioGroup radio;
EditText tNama;
EditText tHobi;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
inisialisasi();
bKirim.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), second.class);
i.putExtra("variabelnama", tNama.getText().toString());
i.putExtra("variabelumur", tHobi.getText().toString());
int id = radio.getCheckedRadioButtonId();
RadioButton radiobutton = (RadioButton)findViewById(id);
i.putExtra("variabelkelamin", radiobutton.getText().toString());
startActivity(i);
}
});
}
private void inisialisasi() {
bKirim = (Button)findViewById(R.id.kirim);
radio = (RadioGroup)findViewById(R.id.jeniskelamin);
tNama = (EditText)findViewById(R.id.nama);
tHobi= (EditText)findViewById(R.id.hobi);
}
}
second.java
import android.content.Intent;
import android.app.*;
import android.os.Bundle;
import android.widget.TextView;
public class second extends Activity {
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondmain);
Intent i = getIntent();
String nerimanama = i.getStringExtra("variabelnama");
String nerimaumur = i.getStringExtra("variabelumur");
String kelamin = i.getStringExtra("variabelkelamin");
TextView nama = (TextView)findViewById(R.id.namaValue);
TextView umur = (TextView)findViewById(R.id.hobivalue);
TextView jk = (TextView)findViewById(R.id.jeniskelaminvalue);
nama.setText(nerimanama);
umur.setText(nerimaumur);
jk.setText(kelamin);
}
}
[DROID] Parsing Data Antar Activity
Cara Import Library (firebase)
1. bikin project baru kemudian edit file build.gradle pertama dan tambahkan classpath untuk firebase
sperti ini
classpath 'com.google.gms:google-services:3.2.0' // google-services plugin
kemudian tambahkan maven dibawah jcenter() sperti ini
allprojects {
repositories {
jcenter()
maven {
url "https://maven.google.com" // Google's Maven repository
}
}
}
2. setelah itu edit file build.gradle yang kedua (module)
tambahkan compile untuk firebase didalam dependencies
dan letakan aplly plugin di paling bawah
apply plugin: 'com.google.gms.google-services'
3. terahir tambahkan library firebase.jar kedalam folder libs
buat folder baru (libs) di dalam folder yang ada folder src dan java
4.klik dan tahan file firebase.jar nya lalu klik add to projek nanti otomatis akan muncul dialog untuk mendowload maven library
selesai
nb: jika download maven library tidak muncul bisa library di dependencies dan di firebase.jar tidak sama versinya
Kedua
coba tutup IDENYA atau pindah projek kemudian buka kembali projek yang tadi nanti akan muncul maven download library
[DROID] Import Library (firebase)
AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
MainActivity.java
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.*;
public class MainActivity extends Activity {
Button button;
ProgressDialog progressdialog;
public static final int Progress_Dialog_Progress = 0;
String ImageURL = "https://www.android-examples.com/wp-content/uploads/2016/04/demo_download_image.jpg" ;
URL url;
URLConnection urlconnection ;
int FileSize;
InputStream inputstream;
OutputStream outputstream;
byte dataArray[] = new byte[1024];
long totalSize = 0;
ImageView imageview;
String GetPath ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button)findViewById(R.id.button);
imageview = (ImageView)findViewById(R.id.image);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new ImageDownloadWithProgressDialog().execute(ImageURL);
}
});
}
public class ImageDownloadWithProgressDialog extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(Progress_Dialog_Progress);
}
@Override
protected String doInBackground(String... aurl) {
int count;
try {
url = new URL(aurl[0]);
urlconnection = url.openConnection();
urlconnection.connect();
FileSize = urlconnection.getContentLength();
inputstream = new BufferedInputStream(url.openStream());
outputstream = new FileOutputStream("/sdcard/demo_photo1.jpg");
while ((count = inputstream.read(dataArray)) != -1) {
totalSize += count;
publishProgress(""+(int)((totalSize*100)/FileSize));
outputstream.write(dataArray, 0, count);
}
outputstream.flush();
outputstream.close();
inputstream.close();
} catch (Exception e) {}
return null;
}
protected void onProgressUpdate(String... progress) {
progressdialog.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(String unused) {
dismissDialog(Progress_Dialog_Progress);
GetPath = Environment.getExternalStorageDirectory().toString() + "/demo_photo1.jpg";
imageview.setImageDrawable(Drawable.createFromPath(GetPath));
Toast.makeText(MainActivity.this, "Image Downloaded Successfully", Toast.LENGTH_LONG).show();
}
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case Progress_Dialog_Progress:
progressdialog = new ProgressDialog(MainActivity.this);
progressdialog.setMessage("Downloading Image From Server...");
progressdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressdialog.setCancelable(false);
progressdialog.show();
return progressdialog;
default:
return null;
}
}
}
[DROID] AsyncTask Download Image
MainActivity.java
import android.app.*;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
public class MainActivity extends Activity {
GridView grid;
int[] imageId = {
R.drawable.fb1,
R.drawable.item, };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
CustomGrid adapter = new CustomGrid(this,imageId);
grid = (GridView) findViewById(R.id.grid);
grid.setAdapter(adapter);
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, "You select ",
Toast.LENGTH_SHORT).show();
}
});
}
}
CustomGrid.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomGrid extends BaseAdapter {
private Context mContext;
private final int[] Imageid;
public CustomGrid(Context c, int[] Imageid) {
mContext = c;
this.Imageid = Imageid;
}
@Override
public int getCount() {
return Imageid.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View grid;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(mContext);
grid = inflater.inflate(R.layout.grid_single, null);
ImageView imageView = (ImageView) grid.findViewById(R.id.grid_image);
imageView.setImageResource(Imageid[position]);
} else {
grid = (View) convertView;
}
return grid;
}
}
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<GridView
android:id="@+id/grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="15dp"
android:columnWidth="100dp"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" />
</LinearLayout>
grid_single.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:padding="5dp">
<ImageView
android:id="@+id/grid_image"
android:layout_width="100dp"
android:layout_height="150dp"/>
</LinearLayout>
[DROID] Grid view image
https://www.google.com/amp/s/rekayuu.wordpress.com/2014/11/03/tugas-kuliahmembuat-alarm-sederhana-untuk-android/amp/
[DROID] alarm sederhana
package com.mycompany.myapp;
import android.app.*;
import android.content.*;
import android.os.*;
import android.widget.*;
public class MainActivity extends Activity {
@Override
private TextView batteryTxt;
private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context ctxt, Intent intent) {
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
batteryTxt.setText(String.valueOf(level) + "%");
if(level==89){batteryTxt.setText("okokokok");}}};
@Override
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.main);
batteryTxt = (TextView) this.findViewById(R.id.l);
this.registerReceiver(this.mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));}}
[DROID] cek level battery
package com.mycompany.myapp;
import java.io.IOException;
import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.app.WallpaperManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from activity_main.xml
setContentView(R.layout.main);
// Create an actionbar
ActionBar actionBar = getActionBar();
actionBar.show();
// Locate ImageView in activity_main.xml
ImageView mywallpaper = (ImageView) findViewById(R.id.wallpaper);
// Attach image into ImageView
mywallpaper.setImageResource(R.drawable.mery);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Create an actionbar menu
menu.add("Set as Wallpaper")
// Add a new Menu Button
.setOnMenuItemClickListener(this.SetWallpaperClickListener)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
return super.onCreateOptionsMenu(menu);
}
// Capture actionbar menu item click
OnMenuItemClickListener SetWallpaperClickListener = new OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
// Retrieve a WallpaperManager
WallpaperManager myWallpaperManager = WallpaperManager
.getInstance(MainActivity.this);
try {
// Change the current system wallpaper
myWallpaperManager.setResource(R.drawable.mery);
// Show a toast message on successful change
Toast.makeText(MainActivity.this,
"Wallpaper successfully changed", Toast.LENGTH_SHORT)
.show();
} catch (IOException e) {
// TODO Auto-generated catch block
}
return false;
}
};
}
AndroidManifest.xml
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.SET_WALLPAPER" >
</uses-permission>
Main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/wallpaper"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
[DROID] Set Walpaper
/*langkah pertama kita buat form dulu dengan html dalam file php*/
<html>
<form method="post" action="">
<input type="text" name="nama" placeholder="nama"/>
<input type="text" name="umur" placeholder="umur"/>
<input type="submit" name="tombol" value="insert"/>
</form>
/*kemudian buat koneksi kedatabase*/
<?php $con=mysqli_connect("localhost","root","","nama_database");
/*setelah itu ambil nama input dari form yg sudah kita buat*/
if(isset($_POST['tombol'])){
$nama=$_POST['nama'];
$umur=$_POST['umur'];
/*nah langkah yang ini untuk mengirim data kedalam database*/
$masukan="INSERT INTO nama_tabel (nama_kolom1,nama_kolom2) value('$nama','$umur')";
$query=mysqli_query($con,$masukan);
/*buat logika*/
if($query){
echo" data berhasil dimasukan";
}else{ echo" gagal memasukan data";
}}
?>
/*jangan lupa penutup html */
</html>
/* jika ada yang error mungkin ada tanda kutip yang hilang silahkan di tanyakan di komentar apa yang error:) /*
[WEB] cara menyimpan data(text) kedalam database mysql
MainActivity.java
package com.mycompany.myapp;
import android.app.*;
import android.content.*;
import android.os.*;
import android.widget.*;
import android.net.*;
public class MainActivity extends Activity{
EditText tx;
String text;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tx=(EditText)findViewById(R.id.tx);
Intent receivedIntent = getIntent();
String receivedAction = receivedIntent.getAction();
String receivedType = receivedIntent.getType();
if(receivedAction.equals(Intent.ACTION_SEND)){
if(receivedType.startsWith("text/")){
String receivedText = receivedIntent.getStringExtra(Intent.EXTRA_TEXT);
if(receivedText != null) {
tx.setText(receivedText);
}}} finish();}}
main.xml
buat EditText dengan id tx
AndroidManifest.xml
tambahkan intent filter
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycompany.myapp" >
<uses-permission
android:name="android.permission.READ_SMS"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/*" />
</intent-filter>
</activity>
</application>
</manifest>