0% found this document useful (0 votes)
76 views

Mobile Programming

The document provides instructions for creating an Android application project in Eclipse. It includes steps to: 1. Install the JDK and extract the Eclipse and Android SDK files. 2. Open Eclipse and configure it to use the installed Android SDK. 3. Create a new Android project and name it. 4. Add components like buttons and text fields to the project layout.

Uploaded by

Syafrial Hady
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

Mobile Programming

The document provides instructions for creating an Android application project in Eclipse. It includes steps to: 1. Install the JDK and extract the Eclipse and Android SDK files. 2. Open Eclipse and configure it to use the installed Android SDK. 3. Create a new Android project and name it. 4. Add components like buttons and text fields to the project layout.

Uploaded by

Syafrial Hady
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 184

UNIVERSITAS PAMULANG

Teknik Informatika
a. Install JDK terlebih dahulu.
b. Extract file eclipse-jee-juno-win32-Android.zip ke
drive C:\
c. Extract android-sdk.part01.rar yang ada di direktori
SDK ke Drive C:\, sehingga folder android-sdk berada
di drive C:\ (C:\android-sdk).
d. Jalankan program eclipse.
e. Jika muncul permintaan penginstalan android-sdk,
pilih untuk menggunakan android-sdk yang sudah
ada. Browse ke C:\android-sdk
f. Jika ada konfigurasi yang tidak sesuai, pada eclipse -
Silahkan masuk ke menu Windows->Preferences.
Langkah: Tampilan:
a. Klik Toolbars New yang
berada dibawah Tab File
b. Pilih Other
Langkah: Tampilan:
a. Pilih Android, Lalu pilih
b. Android Application
Project
c. Klik Next
Langkah: Tampilan:
a. Ketikkan Nama Aplikasi
b. Pilih Build SDK
c. Pilih juga Minimum
Required SDK
d. Lalu klik Next
Pilih Next

Pilih Next Lagi


Klik Finish

Tampilan Design
• Buat Project Baru
• Next
• Next
• Next
• Finish
• Klik Text “Hello world!”
• Hapus
• Pilih PlainText (abc)
• Button
Langkah: Tampilan
a. Klik kanan pada Project
b. Pilih SRC
c. Pilih com.unpam.widget
d. Double Click
MainActivity.java
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{


EditText inText;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

inText = (EditText) findViewById(R.id.editText);


findViewById(R.id.button1).setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

public void onClick(View v){


switch (v.getId()){
case R.id.button1:
Toast.makeText(this, inText.getText(), Toast.LENGTH_LONG).show();
}
}
}
Langkah: Tampilan:
• Klik kanan Project
• Pilih Run As
• Pilih Android Application
• android:onClick="Masukan"
/>
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {

TextView Masukan, Keluaran;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Masukan = (TextView) findViewById(R.id.masukan);


Keluaran = (TextView) findViewById(R.id.hasilmasukan);

public void Masukan(View V){


String kataKalimat = Masukan.getText().toString();
Keluaran.setText(kataKalimat);
}
}
android:onClick="klikHitungYa" />
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
//Pertama
TextView input1, cetakNilai,
cetakLogika ;
Button buttonLogika;

private double nilaiLogika;


private String cek1;
private String cek2;
//kedua
input1 = (TextView)
findViewById(R.id.NilaiEditText);
cetakNilai = (TextView)
findViewById(R.id.CetakNilaiTextView);
cetakLogika = (TextView)
findViewById(R.id.LogikaTextView);
buttonLogika = (Button)
findViewById(R.id.LogikaButton);
//ketiga
public void klikHitungYa(View V){
double inputA =
Double.parseDouble(input1.getText().
toString());
if(inputA>=80){
cetakNilai.setText(""+inputA);
cetakLogika.setText("A");
}
}
android:onClick="klikHitungYa" />
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
//R.1
TextView input1, input2,
operator, result, notif;
Button btHitung;

//V.1
private int operation = 0;
private double HasilAkhir = 0.0;
private String Cek1 = "";
private String Cek2 = "";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_operasi_dua_angk
a);

//R.2
input1 = (TextView)
findViewById(R.id.angkaPertama);
input2 = (TextView) findViewById(R.id.angkaKedua);
operator = (TextView) findViewById(R.id.operasi);
result = (TextView) findViewById(R.id.hasil);
notif = (TextView) findViewById(R.id.keterangan);

//R.3
btHitung = (Button) findViewById(R.id.button1);
}
//Metod.Hitung
public void klikHitungYa(View V){

double inputA =
Double.parseDouble(input1.getText().toString());
double inputB =
Double.parseDouble(input2.getText().toString());
HasilAkhir = inputA + inputB;
String hasilString = String.valueOf(HasilAkhir);
result.setText(hasilString);
operator.setText("+");
if(HasilAkhir>0){
notif.setText("angka positif");
}else if(HasilAkhir<0){
notif.setText("angka negatif");

}
Cek1 = input1.getText().toString();
if((Cek1.equalsIgnoreCase("")) ||
(Cek2.equalsIgnoreCase(""))){
Cek2 = input2.getText().toString();
notif.setText("Kolom tidak
boleh kosong");
}
a. ID TextField1 = masukan1
b. ID TextField2 = masukan2
c. Button ID = hitung
d. Edit Text Button = hitung
android:onClick="klikHasil" />
a. Button + = tambah a. android:onClick="klikTambah" />
b. Button - = kurang b. android:onClick="klikKurang" />
c. android:onClick="klikKali" />
c. Button x = kali
d. android:onClick="klikBagi" />
d. Button : = bagi
• ID = hasil
• Change text = 0
• ID = operan
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
//R.1
TextView input1, input2,
operator, result, notif;
Button btHitung, btJumlah,
btKurang, btKali, btBagi;

//V.1
private int operation = 0;
private double HasilAkhir = 0.0;
private String Cek1 = "";
private String Cek2 = "";
//M.1
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hitung_angka);

//R.2
input1 = (TextView) findViewById(R.id.masukan1);
input2 = (TextView) findViewById(R.id.masukan2);
operator = (TextView) findViewById(R.id.operan);
result = (TextView) findViewById(R.id.hasil);
notif = (TextView) findViewById(R.id.notifikasi);

//R.3
btHitung = (Button) findViewById(R.id.hitung);
btJumlah = (Button) findViewById(R.id.tambah);
btKurang = (Button) findViewById(R.id.kurang);
btKali = (Button) findViewById(R.id.kali);
btBagi = (Button) findViewById(R.id.bagi);
//M.2
public void klikTambah(View V){
operation = 1;
operator.setText(" + ");
}
//M.3
public void klikKurang(View V){
operation = 2;
operator.setText(" - ");
}

//M.4
public void klikKali(View V){
operation = 3;
operator.setText(" x ");
}

//M.4
public void klikBagi(View V){
operation = 4;
operator.setText(" : ");
}
//M.5 //D.1
switch(operation){
public void klikHasil(View V){ case 1:
HasilAkhir = inputA + inputB;
break;
Cek1 = input1.getText().toString();
Cek2 = input1.getText().toString(); case 2:
HasilAkhir = inputA - inputB;
break;
if((Cek1.equalsIgnoreCase("")) ||
(Cek2.equalsIgnoreCase(""))){ case 3:
notif.setText("Kolom tidak boleh kosong"); HasilAkhir = inputA * inputB;
break;
}
else{ case 4:
double inputA = HasilAkhir = inputA / inputB;
Double.parseDouble(input1.getText().toStrin break;

g());
case 0:
double inputB = notif.setText("Harap pilih operan terlebih dahulu!");
Double.parseDouble(input2.getText().toStrin break;
g());
default:
notif.setText("Undescribeable Error!");
break;
}
//D.2
if(operation < 1){
result.setText("0");
}

else{
String hasilString =
String.valueOf(HasilAkhir);
result.setText(hasilString);
notif.setText("Simple Calculator
Ready!");
}
}

}
a. Masukkan TextView
b. Edit Text TextView1 = Home Ku
c. Masukkan Button
d. Edit ID button = NextButton
android:onClick=“NextKlickYa" /> a. Selanjutnya juga buat
android activity lain yang
kita beri nama Halaman_A.
Caranya Pilih New-
>Android Activity lengkapi
atribut yang dibutuhkan
pada pembuatan Child
Activity.
import android.os.Bundle; //pertama
import android.app.Activity; Button btnPindah;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
//kedua
btnPindah =
(Button)findViewById(R.id.NextButton);
btnPindah.setOnClickListener(new
View.OnClickListener() {

public void onClick(View v) {


Intent i = new Intent(getApplicationContext(),
Halaman_A.class);
startActivity(i);
}
});
}
• Menghapus perintah <action
android:name="android.intent.action.M
AIN" /> Pada AndroidManifest.xml
<activity
a. Klik dan masuk android:name=".Halaman_A"
“AndroidManifest.xml” android:label="@string/title_activity_halaman_" >
<intent-filter>
b. Selanjutnya perhatikan <action
Struktur sintak yang berada android:name="android.intent.action.MAIN" />
disamping ini <category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

Hapus yang bertanda merah


<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" />

<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="bottom" />

</LinearLayout>
</TabHost>
import android.os.Bundle;
import android.app.TabActivity;
import android.content.Intent;
import android.widget.TabHost;
TabActivity
TabHost tabhost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, BerandaActivity.class);//content
pada tab yang akan kita buat
spec =
tabhost.newTabSpec("beranda").setIndicator("Beranda",null).
setContent(intent);//mengeset nama tab dan mengisi content
pada menu tab anda.
tabhost.addTab(spec);//untuk membuat tabbaru disini bisa
diatur sesuai keinginan anda

intent = new Intent().setClass(this, BeritaActivity.class);


spec =
tabhost.newTabSpec("berita").setIndicator("Berita",null).setC
ontent(intent);
tabhost.addTab(spec);

intent = new Intent().setClass(this, TemanActivity.class);


spec =
tabhost.newTabSpec("teman").setIndicator("Teman",null).set
Content(intent);
tabhost.addTab(spec);
<DigitalClock
xmlns:android="http://schemas.android.c
om/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical|center"
android:textSize="50sp" >
</DigitalClock>
<LinearLayout
xmlns:android="http://schemas.androi
d.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<LinearLayout
xmlns:android="http://schemas.androi
d.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
setListAdapter(new
String [] berita ={"Jadwal Piala ArrayAdapter<String>(this,
Dunia 2014", "Capres android.R.layout.simple_list_it
Indonesia", "Debat Capres", em_1, berita));
"Debat Cawapres"};

//ubah
public class Berita extends Activity {
Menjadi :
public class Berita extends
ListActivity {
import android.os.Bundle;
import android.view.Menu;
import android.app.ListActivity;
import android.widget.ArrayAdapter;
import android.app.ListActivity; String [] teman ={"Niki", "Toni",
import android.os.Bundle; "Ari", "Ardy"};
import android.view.Menu;
import android.widget.ArrayAdapter;

//ubah
public class Berita extends Activity {
Menjadi :
public class Berita extends
ListActivity {
setListAdapter(new
ArrayAdapter<String>(this,
android.R.layout.simple_list_item
_1, teman));
<activity android:name="BerandaActivity"></activity>
<activity android:name="BeritaActivity"></activity>
<activity android:name="TemanActivity"></activity>
• Bila terjadi kesalahan pada “MainActivity.java”
• Perbaiki kesalahan/syntak error tersebut
android:background="@drawable/batman"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com
/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com
/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com
/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
/>
</LinearLayout>
import android.os.Bundle;
import android.app.ListActivity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.content.Intent;
import android.view.View;
String[] perangkat = { "Activity
Laptop", "Activity Smartphone"};

//Tambahan
Ubah Activity Menjadi ListActivity
setListAdapter(new ArrayAdapter<String>
(this,android.R.layout.simple_list_item_1,
perangkat));
public void onListItemClick(ListView parent, View v,
int urutan, long id) {
Object detail =
this.getListAdapter().getItem(urutan);
String tampil = detail.toString();
Intent i = null;
if(tampil=="Activity Laptop")
{
i = new
Intent(MainActivity.this, Leptop.class);
startActivity(i);
}
else if(tampil=="Activity Smartphone")
{
i = new
Intent(MainActivity.this, Smartphone.class);
startActivity(i);
}
}
import android.os.Bundle;
import android.app.ListActivity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.content.Intent;
import android.view.View;
String[] perangkat = { "Activity Asus",
"Activity Acer",
"Activity Toshiba",
"Activity Sony",
"Activity Axioo",
"Activity Dell",
"<< Kembali"};

//Tambahan
Ubah Activity menjadi ListActivity
setListAdapter(new ArrayAdapter<String>
(this,android.R.layout.simple_list_item_1,
perangkat));
public void onListItemClick(ListView parent, View v, int
urutan, long id) {
Object detail =
this.getListAdapter().getItem(urutan);
String tampil = detail.toString();
Intent i = null;
if(tampil=="<< Kembali")
{
i = new Intent(Leptop.this,
MainActivity.class);
startActivity(i);
}
else
{
Toast.makeText(this, "Anda
Memilih : " + tampil, Toast.LENGTH_SHORT).show();
}
}
<?xml version="1.0" encoding="utf-8"?>
<WebView
xmlns:android="http://schemas.andr
oid.com/apk/res/android"
android:id="@+id/webViewDroidMu"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
private WebView WebUnpamKu_Nky;
WebUnpamKu_Nky= new WebView(this);

WebUnpamKu_Nky.getSettings().setJavaScri
ptEnabled(true); // enable javascript

final Activity activity = this;

WebUnpamKu_Nky.setWebViewClient
(new WebViewClient());

WebUnpamKu_Nky
.loadUrl("http://my.unpam.ac.id/");
setContentView(WebUnpamKu_Nky );
<uses-permission
android:name="android.
permission.INTERNET"
/>
<WebView
android:id="@+id/mybrowser"
android:layout_width="match_parent"
android:layout_height="match_parent" />
WebView TampilHTML; TampilHTML =
(WebView)findViewById(R.id.mybrowser);

TampilHTML.loadUrl("file:///android_asse
t/index.html");

TampilHTML.getSettings().setJavaScriptEn
abled(true);
TampilHTML.loadUrl("file:///android_asset/de
mo_mobileprogramming/index.html");

You might also like