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

Tampil Nim A. Layout Main - XML 2. Menu Dan Dialog A. Layout Main - XML

This document contains code snippets for several Android layouts and activities: 1. A layout and activity for displaying a name and student ID number. 2. A layout, dialog, and activity for collecting user input via text fields and dropdown, then displaying it. 3. The beginning of code for a tabs layout but it is not completed.

Uploaded by

ieiem community
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)
44 views

Tampil Nim A. Layout Main - XML 2. Menu Dan Dialog A. Layout Main - XML

This document contains code snippets for several Android layouts and activities: 1. A layout and activity for displaying a name and student ID number. 2. A layout, dialog, and activity for collecting user input via text fields and dropdown, then displaying it. 3. The beginning of code for a tabs layout but it is not completed.

Uploaded by

ieiem community
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/ 31

1. TAMPIL NIM 2.

MENU DAN DIALOG


a. Layout a. Layout
main.xml Main.xml
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_height="fill_parent" android:background="@color/white">
> <TextView android:id="@+id/TextView01"
android:layout_width="wrap_content"
<TextView android:text="@+id/TextView01" android:id="@+id/TextView01" android:layout_height="wrap_content" android:text="Nama :"></TextView>
android:layout_width="wrap_content" <EditText android:text="@+id/EditText01"
android:layout_height="wrap_content"></TextView> android:id="@+id/EditText01"
<TextView android:text="@+id/TextView02" android:id="@+id/TextView02" android:layout_width="wrap_content"
android:layout_width="wrap_content" android:layout_height="wrap_content"></EditText>
android:layout_height="wrap_content"></TextView> <TextView android:id="@+id/TextView02"
</LinearLayout> android:layout_width="wrap_content" android:layout_height="wrap_content"
b. Source Java android:text="Agama :"></TextView>
<AutoCompleteTextView android:text="@+id/AutoCompleteTextView01"
MainActivity.java android:id="@+id/AutoCompleteTextView01"
package my.TampilNim; android:layout_width="wrap_content"
import android.app.Activity; android:layout_height="wrap_content"></AutoCompleteTextView>
import android.graphics.Color; </LinearLayout>
import android.os.Bundle;
import android.widget.*;
public class TampilNim extends Activity {
Hal1.xml
/** Called when the activity is first created. */ <?xml version="1.0" encoding="utf-8"?>
private TextView nama; <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
private TextView nim; android:orientation="vertical"
@Override android:layout_width="fill_parent"
public void onCreate(Bundle savedInstanceState) { android:layout_height="fill_parent" android:background="@color/white">
super.onCreate(savedInstanceState); <TextView android:text="@+id/TextView03" android:id="@+id/TextView03"
setContentView(R.layout.main); android:layout_width="wrap_content"
nama=(TextView)findViewById(R.id.TextView01); android:layout_height="wrap_content"></TextView>
nim=(TextView)findViewById(R.id.TextView02); </LinearLayout>
nama.setText("STMIK WIDYA UTAMA");
nim.setText("STIDKA001"); b. Source Java
nama.setTextColor(Color.BLUE); package swu.cobaMenudanDialog;
nama.setBackgroundColor(Color.RED);
nim.setTextColor(Color.BLACK); import android.app.Activity;
nim.setBackgroundColor(Color.WHITE); import android.app.AlertDialog;
import android.content.DialogInterface;
} import android.content.DialogInterface.OnClickListener;
} import android.graphics.Color;
import android.os.Bundle;
import android.widget.*; return false;
import android.view.*; }

public void OK()


public class cobaMenudanDialog extends Activity { {
setContentView(R.layout.hal1);
/** Called when the activity is first created. */ tampil=(TextView) findViewById(R.id.TextView03);
private TextView tampil; tampil.setTextColor(Color.BLACK);
private EditText teks; tampil.setText("\tSelamat Datang\nNama Anda
private AutoCompleteTextView actv; :"+teks.getText()+"\nBeragama:"+actv.getText());
private Menu menu; }
@Override public void Exit()
{
public void onCreate(Bundle savedInstanceState) { AlertDialog.Builder ad=new AlertDialog.Builder(cobaMenudanDialog.this);
super.onCreate(savedInstanceState); ad.setTitle("Konfirmasi");
setContentView(R.layout.main); ad.setMessage("Apakah benar ingin keluar?");
ad.setPositiveButton("OK",new OnClickListener(){
teks=(EditText) findViewById(R.id.EditText01);
actv = (AutoCompleteTextView) this.findViewById(R.id.AutoCompleteTextView01); public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
ArrayAdapter<String> agama = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, keluar();
new String[] {"Islam","Kristen","Katolik","Hindu","Budha" }); }});
actv.setAdapter(agama); ad.setNegativeButton("No",new OnClickListener(){

public void onClick(DialogInterface arg0, int arg1) {


} //not do anything
/* Creates the menu items */ }});
public boolean onCreateOptionsMenu(Menu menu) { ad.show();
menu.add(0, 1, 0, "OK") }
.setIcon(android.R.drawable.star_on); public void keluar()
menu.add(0,2, 0, "Exit") {
.setIcon(android.R.drawable.star_off); this.finish();
return true; }
}
}
/* Handles item selections */
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1:
OK();
return true;
case 2:
Exit();
return true;
}
3. TABS android:id="@+id/teks2">
</TextView>
a. Layout
main.xml <AnalogClock android:id="@+id/AnalogClock01"
<?xml version="1.0" encoding="utf-8"?> android:layout_width="wrap_content"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content"></AnalogClock>
android:orientation="vertical" <DigitalClock android:text="@+id/DigitalClock01"
android:layout_width="fill_parent" android:id="@+id/DigitalClock01"
android:layout_height="fill_parent" android:layout_width="wrap_content"
> android:layout_height="wrap_content"></DigitalClock>
<TextView </LinearLayout>
android:layout_width="fill_parent"
android:layout_height="wrap_content" b. Source Java
android:text="@string/hello"
/>
MainActivity.java
</LinearLayout> package swu.cobaTab1;

import android.app.TabActivity;
tab1.xml import android.content.Intent;
<?xml version="1.0" encoding="utf-8"?> import android.os.Bundle;
<LinearLayout import android.widget.TabHost;
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent" public class cobaTab1 extends TabActivity {
android:layout_height="fill_parent" /** Called when the activity is first created. */
xmlns:android="http://schemas.android.com/apk/res/android" @Override
android:orientation="vertical" public void onCreate(Bundle savedInstanceState) {
android:background="@color/white"> super.onCreate(savedInstanceState);
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" TabHost host = this.getTabHost();
android:id="@+id/teks1">
</TextView> host.addTab(host.newTabSpec("one")
</LinearLayout> .setIndicator("Tab1")
.setContent(new Intent(this, tab1.class)));

tab2.xml host.addTab(host.newTabSpec("two")
<?xml version="1.0" encoding="utf-8"?> .setIndicator("Tab2")
<LinearLayout .setContent(new Intent(this, tab2.class)));
android:id="@+id/LinearLayout01" }
android:layout_width="fill_parent" }
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"> Tab1.java
<TextView package swu.cobaTab1;
android:text="@+id/TextView01"
android:layout_width="wrap_content" import android.app.Activity;
android:layout_height="wrap_content" import android.graphics.Color;
import android.os.Bundle; AndroidManifest.xml
import android.widget.*; <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
public class tab1 extends Activity { package="swu.cobaTab1"
/** Called when the activity is first created. */ android:versionCode="1"
android:versionName="1.0">
private TextView text; <application android:icon="@drawable/icon" android:label="@string/app_name">
@Override <activity android:name=".cobaTab1"
public void onCreate(Bundle savedInstanceState) { android:label="@string/app_name">
super.onCreate(savedInstanceState); <intent-filter>
setContentView(R.layout.tab1); <action android:name="android.intent.action.MAIN" />
text=(TextView) findViewById(R.id.teks1); <category android:name="android.intent.category.LAUNCHER" />
text.setText("Ini di TAB PERTAMA"); </intent-filter>
text.setTextColor(Color.BLACK); </activity>
}
} <activity android:name="tab1"></activity>
<activity android:name="tab2"></activity>
Tab2.java </application>
package swu.cobaTab1; <uses-sdk android:minSdkVersion="8" />

import android.app.Activity; </manifest>


import android.graphics.Color;
import android.os.Bundle; 4. TEKS
import android.widget.*;
a. Layout
public class tab2 extends Activity { main.xml
/** Called when the activity is first created. */ <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
private TextView text; android:orientation="vertical"
@Override android:layout_width="fill_parent"
public void onCreate(Bundle savedInstanceState) { android:layout_height="fill_parent" android:background="@color/white">
super.onCreate(savedInstanceState); <TextView android:id="@+id/TextView01"
setContentView(R.layout.tab2); android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Nama :"></TextView>
text=(TextView) findViewById(R.id.teks2); <EditText android:text="@+id/EditText01"
text.setText("JAM"); android:id="@+id/EditText01"
text.setTextColor(Color.WHITE); android:layout_width="wrap_content"
android:layout_height="wrap_content"></EditText>
} <TextView android:id="@+id/TextView02"
} android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Agama :"></TextView>
<AutoCompleteTextView android:text="@+id/AutoCompleteTextView01"
android:id="@+id/AutoCompleteTextView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></AutoCompleteTextView>
<ImageButton android:id="@+id/ImageButton01" teks=(EditText) findViewById(R.id.EditText01);
android:layout_width="wrap_content" but=(ImageButton) findViewById(R.id.ImageButton01);
android:layout_height="wrap_content" android:onClick="but_Klik"></ImageButton> actv = (AutoCompleteTextView) this.findViewById(R.id.AutoCompleteTextView01);
</LinearLayout>
but.setImageResource(android.R.drawable.btn_star);
hal1.xml
<?xml version="1.0" encoding="utf-8"?> ArrayAdapter<String> agama = new ArrayAdapter<String>(this,
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android.R.layout.simple_dropdown_item_1line,
android:orientation="vertical" new String[] {"Islam","Kristen","Katolik","Hindu","Budha" });
android:layout_width="fill_parent" actv.setAdapter(agama);
android:layout_height="fill_parent" android:background="@color/white">
<TextView android:text="@+id/TextView03" android:id="@+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView> }
<Button android:text="@+id/Button01" android:id="@+id/Button01"
android:layout_width="wrap_content" public void but_Klik(View view)
android:layout_height="wrap_content"></Button> {
</LinearLayout> setContentView(R.layout.hal1);
tampil=(TextView) findViewById(R.id.TextView03);
tampil.setTextColor(Color.BLACK);
b. Source Java tampil.setText("\tSelamat Datang\nNama Anda
MainActivity.xml :"+teks.getText()+"\nBeragama:"+actv.getText());
package swu.cobaTeks; }

import android.app.Activity; }
import android.graphics.Color;
import android.os.Bundle; 5. KONVERSI MATA UANG
import
import
android.widget.*;
android.view.*;
a. Layout
main.xml
<?xml version="1.0" encoding="utf-8"?>
public class cobaTeks extends Activity {
<RelativeLayout android:id="@+id/RelativeLayout01"
/** Called when the activity is first created. */ android:layout_width="fill_parent"
private TextView tampil; android:layout_height="fill_parent"
private EditText teks; xmlns:android="http://schemas.android.com/apk/res/android"
private ImageButton but; android:background="#fff">
private AutoCompleteTextView actv; <TextView android:text="Selamat Datang Pada Halaman Utama"
@Override android:id="@+id/textWelcome1"
android:layout_width="fill_parent"
public void onCreate(Bundle savedInstanceState) { android:layout_height="wrap_content"
super.onCreate(savedInstanceState); android:gravity="center_horizontal"
setContentView(R.layout.main); android:textColor="#000"/>
<TextView android:id="@+id/TeksMasukkan"
android:text="Masukkan nilai :" setting.xml
android:layout_height="wrap_content" <?xml version="1.0" encoding="utf-8"?>
android:layout_above="@+id/Konversi" <TableLayout android:id="@+id/TableLayout01"
android:layout_width="wrap_content" android:layout_width="fill_parent"
android:textColor="#000"> android:layout_height="fill_parent"
</TextView> xmlns:android="http://schemas.android.com/apk/res/android"
<EditText android:layout_centerVertical="true" android:background="#fff">
android:layout_centerHorizontal="true"
android:id="@+id/Konversi" <TextView android:text="Masukkan Nilai Tukar dalam Rupiah"
android:layout_height="wrap_content" android:id="@+id/TextView01"
android:text="Nilai" android:layout_width="wrap_content"
android:layout_width="wrap_content"> android:layout_height="wrap_content"
</EditText> >
<TextView android:id="@+id/TeksHasil" </TextView>
android:text="Hasil :"
android:layout_height="wrap_content" <TableRow android:id="@+id/TableRow01"
android:layout_below="@+id/Konversi" android:layout_width="wrap_content"
android:layout_width="wrap_content" android:layout_height="wrap_content">
android:textColor="#000"> <TextView android:text="Dolar :"
</TextView> android:id="@+id/TextView02"
<RadioGroup android:layout_above="@+id/TeksMasukkan" android:layout_width="wrap_content"
android:id="@+id/RadioGroup01" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
android:layout_height="wrap_content"> <EditText
<RadioButton android:text="@+id/RadioButton01" android:id="@+id/teks_Dolar" android:layout_width="wrap_content"
android:id="@+id/RadioButton01" android:layout_height="wrap_content"></EditText>
android:layout_width="wrap_content"
android:layout_height="wrap_content" </TableRow>
android:textColor="#000"></RadioButton> <TableRow android:id="@+id/TableRow02"
<RadioButton android:text="@+id/RadioButton02" android:layout_width="wrap_content"
android:id="@+id/RadioButton02" android:layout_height="wrap_content">
android:layout_width="wrap_content" <TextView android:text="EURO :"
android:textColor="#000" android:layout_height="wrap_content"></RadioButton> android:id="@+id/TextView03"
</RadioGroup> android:layout_width="wrap_content"
<Button android:layout_below="@+id/TeksHasil" android:layout_height="wrap_content">
android:text="Konversi" android:layout_height="wrap_content" </TextView>
android:id="@+id/Button01" android:layout_width="wrap_content" <EditText
android:layout_centerHorizontal="true" android:onClick="butKon_Klik"> android:id="@+id/teks_Euro"
</Button> android:layout_width="wrap_content"
android:layout_height="wrap_content">
</RelativeLayout> </EditText>

</TableRow>
<TableRow android:id="@+id/TableRow03"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="Pounds :" }
android:id="@+id/TextView04" android:layout_width="wrap_content" public void deklarasi()
android:layout_height="wrap_content"></TextView> {
<EditText hasil=(TextView) findViewById(R.id.TeksHasil);
android:id="@+id/teks_Pounds" android:layout_width="wrap_content" konv=(EditText) findViewById(R.id.Konversi);
android:layout_height="wrap_content"> rb1=(RadioButton) findViewById(R.id.RadioButton01);
</EditText> rb2=(RadioButton) findViewById(R.id.RadioButton02);
</TableRow> rb1.setChecked(true);
<Button android:text="OK" setTextRadioButton();
android:id="@+id/Button01" }
android:layout_width="wrap_content" //mangatur tulisan radioButton
android:layout_height="wrap_content" public void setTextRadioButton()
android:onClick="butOK_Klik"></Button> {
</TableLayout> rb1.setText("Konversi dari "+jenis+" ke Rupiah");
rb2.setText("Konversi dari Rupiah ke "+jenis);
b. Source Java }
public boolean onCreateOptionsMenu(Menu menu) {
MainActivity.java SubMenu subMenu = menu.addSubMenu("Mata Uang");
package swu.KonversiMataUang; subMenu.add(1, 1, 0, "Dolar").setChecked(true);
subMenu.add(1, 2, 0, "Euro");
import android.app.Activity; subMenu.add(1, 3, 0, "Pounds");
import android.os.Bundle; subMenu.setGroupCheckable(1, true, true);
import android.view.Menu; menu.add(0,4,0,"Setting");
import android.view.MenuItem; menu.add(0,5, 0, "Exit")
import android.view.SubMenu; .setIcon(android.R.drawable.btn_minus);
import android.view.View; return true;
import android.widget.EditText; }
import android.widget.RadioButton;
import android.widget.TextView; /* menangani pemilihan menu*/
public boolean onOptionsItemSelected(MenuItem item) {
public class KonversiMataUang extends Activity { switch (item.getItemId()) {
/** Called when the activity is first created. */ case 1:
private TextView hasil; jenis="Dolar";
private EditText konv; setTextRadioButton();
private EditText dolar; item.setChecked(true);
private EditText euro; return true;
private EditText pounds; case 2:
private String jenis="Dolar"; jenis="Euro";
private double kurs,nDolar=9000,nEuro=15000,nPounds=19000; setTextRadioButton();
private RadioButton rb1; item.setChecked(true);
private RadioButton rb2; return true;
@Override case 3:
public void onCreate(Bundle savedInstanceState) { jenis="Pounds";
super.onCreate(savedInstanceState); setTextRadioButton();
setContentView(R.layout.main); item.setChecked(true);
deklarasi(); return true;
case 4: catch(Exception e)
setting(); {
item.setChecked(true); hasil.setText("number only");
return true; }
case 5: }
this.finish();
return true; //dipilih konversi dari rupiah
} else
return false; {
} try{
//membuka halaman setting hasil.setText("Hasil Konversi="
public void setting()
{ +Double.valueOf(konv.getText().toString()).doubleValue()/this.kurs);
this.setContentView(R.layout.setting); }
dolar=(EditText) findViewById(R.id.teks_Dolar); catch(Exception e)
euro=(EditText) findViewById(R.id.teks_Euro); {
pounds=(EditText) findViewById(R.id.teks_Pounds); hasil.setText("number only");
dolar.setText(String.valueOf(nDolar)); }
euro.setText(String.valueOf(nEuro)); }
pounds.setText(String.valueOf(nPounds)); }
}
//proses setting selesai }
public void butOK_Klik(View view)
{ 6. BATTERY INFORMATION
nDolar=Double.valueOf(dolar.getText().toString()).doubleValue();
nEuro=Double.valueOf(euro.getText().toString()).doubleValue(); a. Layout
nPounds=Double.valueOf(pounds.getText().toString()).doubleValue(); main.xml
this.setContentView(R.layout.main); <?xml version="1.0" encoding="utf-8"?>
deklarasi(); <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
} android:layout_width="fill_parent"
public void butKon_Klik(View view) android:layout_height="fill_parent"
{ android:background="#fff"
if(jenis.equalsIgnoreCase("dolar")) >
{kurs=nDolar;}
else if(jenis.equalsIgnoreCase("euro")) <TextView android:text="@+id/TextView01"
{kurs=nEuro;} android:id="@+id/TextView01"
else if(jenis.equalsIgnoreCase("pounds")) android:layout_width="wrap_content"
{kurs=nPounds;} android:layout_height="wrap_content"
//dipilih konversi ke rupiah android:textColor="#000"></TextView>
if(rb1.isChecked()) </LinearLayout>
{
try{
hasil.setText("Hasil Konversi="
+this.kurs*Double.valueOf(konv.getText().toString()).doubleValue());
}
b. Source Java android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
MainActivity.java
package swu.batteryInformation; <TableRow android:layout_width="wrap_content" android:id="@+id/TableRow01"
android:layout_height="wrap_content">
import android.app.Activity; <TextView android:id="@+id/TextView01" android:layout_width="wrap_content"
import android.content.BroadcastReceiver; android:layout_height="wrap_content" android:text="Number :"></TextView>
import android.content.Context;
import android.content.Intent; <EditText android:layout_width="wrap_content"
import android.content.IntentFilter; android:layout_height="wrap_content" android:id="@+id/edNum"
import android.os.Bundle; android:phoneNumber="true"></EditText>
import android.widget.TextView;

</TableRow>

public class batteryInformation extends Activity { <TableRow android:layout_width="wrap_content" android:id="@+id/TableRow02"


private TextView batteryTxt; android:layout_height="wrap_content">
private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){ <TextView android:id="@+id/TextView02" android:layout_width="wrap_content"
@Override android:layout_height="wrap_content" android:text="Password : "></TextView>
public void onReceive(Context arg0, Intent intent) {
// TODO Auto-generated method stub <EditText android:layout_width="wrap_content"
int level = intent.getIntExtra("level", 0); android:layout_height="wrap_content" android:id="@+id/edPass"
batteryTxt.setText(String.valueOf(level) + "%"); android:password="true"></EditText>
} </TableRow>
};
<Button android:layout_height="wrap_content" android:layout_width="wrap_content"
@Override android:text="OK" android:id="@+id/butOK" android:onClick="butOK_Click">
public void onCreate(Bundle icicle) { </Button>
super.onCreate(icicle);
setContentView(R.layout.main); </TableLayout>
batteryTxt = (TextView) this.findViewById(R.id.TextView01);
this.registerReceiver(this.mBatInfoReceiver,
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
tampil.xml
<?xml version="1.0" encoding="utf-8"?>
} <RelativeLayout
} xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
7. PASSWORD <TextView android:layout_height="wrap_content"
a. Layout android:layout_width="wrap_content"
main.xml android:id="@+id/numb" android:text="Number"
android:layout_centerInParent="true">
<?xml version="1.0" encoding="utf-8"?>
</TextView>
<Button android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_below="@+id/numb" android:layout_toRightOf="@+id/numb"
<TableLayout android:id="@+id/TableLayout01" android:layout_width="fill_parent"
android:text="Cancel"
android:id="@+id/butCancel" android:onClick="butCancel_Click"></Button> import android.os.Bundle;
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" import android.util.Log;
android:layout_below="@+id/numb" android:layout_toLeftOf="@+id/numb" import android.view.View;
android:text="OK" android:id="@+id/butYes" import android.widget.TextView;
android:onClick="butYes_Click"></Button>
</RelativeLayout> public class Activity2 extends Activity
{
b. Source Java private TextView tNumber;
private String number;
MainActivity.java @Override
package com.swu.password; public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
import android.app.Activity; setContentView(R.layout.tampil);
import android.content.Intent; tNumber=(TextView) findViewById(R.id.numb);
import android.os.Bundle; Intent i=getIntent();
import android.view.View; number=i.getExtras().getString("Number");
import android.widget.EditText; tNumber.setText( "you want to call"+number);

public class password extends Activity { }


private EditText edNumber,edPass; public void butYes_Click(View v)
/** Called when the activity is first created. */ {
@Override Intent call = new
public void onCreate(Bundle savedInstanceState) { Intent(Intent.ACTION_CALL,Uri.parse("tel:"+number));
super.onCreate(savedInstanceState); try{startActivity(call);}
setContentView(R.layout.main); catch(Exception e)
edNumber=(EditText)findViewById(R.id.edNum); {
edPass=(EditText)findViewById(R.id.edPass); Log.d("Test",e.toString());
} }
public void butOK_Click(View v) }
{ public void butCancel_Click(View v)
if(edPass.getText().toString().equals("1234")) {
{ finish();
Intent i=new Intent(password.this,Activity2.class); }
i.putExtra("Number", edNumber.getText().toString()); }
startActivity(i);
}
} c. AndroidManifest.xml
} <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
Activity2.java package="com.swu.password"
package com.swu.password; android:versionCode="1"
android:versionName="1.0">
import android.app.Activity; <application android:icon="@drawable/icon" android:label="@string/app_name">
import android.content.Intent; <activity android:name=".password"
import android.net.Uri; android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> import android.os.Bundle;
<category android:name="android.intent.category.LAUNCHER" /> import android.telephony.SmsManager;
</intent-filter> import android.util.Log;
</activity> import android.view.View;
import android.widget.Toast;
<activity android:name="Activity2">
</activity> public class sendSMS extends Activity {
</application> /** Called when the activity is first created. */
<uses-sdk android:minSdkVersion="8" /> private BroadcastReceiver br;
<uses-permission android:name="android.permission.CALL_PHONE"></uses- @Override
permission> public void onCreate(Bundle savedInstanceState) {
</manifest> super.onCreate(savedInstanceState);
setContentView(R.layout.main);
8. SEND SMS
br=new BroadcastReceiver(){
a. Layout @Override
main.xml public void onReceive(Context context, Intent intent)
<?xml version="1.0" encoding="utf-8"?> {
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" if(getResultCode()==Activity.RESULT_OK)
android:layout_width="fill_parent" {
android:layout_height="fill_parent" Toast.makeText(sendSMS.this,"SMS SENT",
> Toast.LENGTH_LONG).show();
}
<Button android:text="SEND(INTENTs)" android:id="@+id/butSend" else
android:layout_width="wrap_content" Toast.makeText(sendSMS.this,"SMS NOT SENT",
android:layout_height="wrap_content" Toast.LENGTH_LONG).show();
android:onClick="butSend_Click"></Button> }
<Button android:text="SMSMANAGER" android:id="@+id/butSMSM"
android:layout_width="wrap_content" };
android:layout_height="wrap_content" }
android:onClick="butSMSM_Click"> public void butSMSM_Click(View v)
</Button> {
</LinearLayout> try
{
b. Source Java PendingIntent pi=PendingIntent.getBroadcast(this, 0, new
Intent("SMS_SENT"), 0);
MainActivity.java SmsManager sms = SmsManager.getDefault();
package swu.sendSMS; sms.sendTextMessage("5556", null, "My Message by SMS Manager",
pi, null);
import android.app.Activity; registerReceiver(br, new IntentFilter("SMS_SENT"));
import android.app.PendingIntent; Log.d("msg", "abc");
import android.content.BroadcastReceiver; }
import android.content.Context; catch(Exception e)
import android.content.Intent; {
import android.content.IntentFilter; Log.d("err", "err:"+e);
import android.net.Uri; }
} android:layout_width="wrap_content"
public void butSend_Click(View v) android:layout_height="wrap_content" android:onClick="butSMS_Click">
{ </Button><Button android:text="Call"
try android:id="@+id/but_Call"
{ android:layout_width="wrap_content"
Uri uri = Uri.parse("smsto:5556"); android:layout_height="wrap_content" android:onClick="butCall_Click">
Intent i = new Intent(Intent.ACTION_SENDTO, uri); </Button>
i.putExtra("sms_body", "My Message by Intent");
startActivity(i);
} </LinearLayout>
catch(Exception e)
{ </LinearLayout>
Log.d("err", "err:"+e);
}
} sms.xml
} <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
9. CALL AND SMS android:layout_width="fill_parent"
a. Layout android:layout_height="fill_parent"
android:orientation="vertical"
main.xml android:background="#fff">
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout android:id="@+id/LinearLayout02"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_width="fill_parent" android:layout_height="wrap_content">
android:layout_height="fill_parent" <TextView android:text="Phone:"
android:background="#fff"> android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout android:id="@+id/LinearLayout02" </TextView>
android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:text="phoneNumber"
<TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:id="@+id/txtPhone2"
android:layout_height="wrap_content" android:text="Number : android:layout_width="wrap_content"
"></TextView><EditText android:layout_width="wrap_content" android:layout_height="wrap_content">
android:id="@+id/editTNumber" </TextView>
android:phoneNumber="true" android:layout_height="wrap_content"></EditText> </LinearLayout>
<EditText android:text="Message" android:id="@+id/message"
android:layout_width="fill_parent"
</LinearLayout> android:layout_height="90px">
</EditText>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="wrap_content" <Button android:text="SEND"
android:layout_height="wrap_content"> android:id="@+id/butSend"
<Button android:text="SMS" android:layout_width="fill_parent"
android:id="@+id/but_SMS" android:layout_height="wrap_content"
android:onClick="butSend_Click"> operator="XL";
</Button> else operator="notknown";
</LinearLayout> }
catch(Exception ae)
{
b. Source Java operator="notknown";
MainActivity.java }
package swu.callAndSMS; //mulai pemanggilan
Toast.makeText(this.getApplicationContext(), ""+operator,
import android.app.Activity; Toast.LENGTH_LONG).show();
import android.content.Intent; i.setData(Uri.parse("tel:"+edNumber.getText()));
import android.net.Uri; startActivity(i);
import android.os.Bundle; }
import android.view.View; catch(Exception e)
import android.widget.EditText; {
import android.widget.Toast; edNumber.setText(""+e);
}
public class callAndSMS extends Activity { }
private EditText edNumber; public void butSMS_Click(View v)
/** Called when the activity is first created. */ {
@Override try
public void onCreate(Bundle savedInstanceState) { {
super.onCreate(savedInstanceState); //mulai Activity untuk mengirim SMS
setContentView(R.layout.main); Intent i=new Intent(callAndSMS.this,sendSMS.class);
edNumber=(EditText) findViewById(R.id.editTNumber); //Kirim number ke Activity baru
i.putExtra("number", edNumber.getText().toString());
} startActivity(i);
public void butCall_Click(View v) }
{ catch(Exception e)
Intent i=new Intent(Intent.ACTION_CALL); {
try edNumber.setText(""+e);
{ }
String phoneNumber=edNumber.getText().toString(); }
String operator=""; }
//check operator
try{ SendSMS.java
if(phoneNumber.subSequence(0, 4).equals("0813") || package swu.callAndSMS;
phoneNumber.subSequence(0, 4).equals("0852")||
phoneNumber.subSequence(0, 4).equals("0812")) import android.app.Activity;
operator="TELKOMSEL"; import android.app.AlertDialog;
else if(phoneNumber.subSequence(0, 4).equals("0857")|| import android.app.PendingIntent;
phoneNumber.subSequence(0, 4).equals("0856")|| import android.content.BroadcastReceiver;
phoneNumber.subSequence(0, 4).equals("0815")) import android.content.Context;
operator="INDOSAT"; import android.content.Intent;
else if(phoneNumber.subSequence(0, 4).equals("0817")) import android.content.IntentFilter;
import android.os.Bundle; sms.sendTextMessage(number, null, edName.getText().toString(),
import android.telephony.SmsManager; pi, null);
import android.view.View; registerReceiver(br, new IntentFilter("SMS_SENT"));
import android.widget.EditText;
import android.widget.TextView; }
catch(Exception e)
public class sendSMS extends Activity { {
private EditText edName; tNumber.setText(e.toString());
private TextView tNumber; }
private String number; }
private BroadcastReceiver br;
/** Called when the activity is first created. */ }
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); 10. USING SQL HELPER
setContentView(R.layout.sms); a. Layout
edName=(EditText) findViewById(R.id.message);
Main.xml
tNumber=(TextView) findViewById(R.id.txtPhone2);
<?xml version="1.0" encoding="utf-8"?>
Intent i=getIntent();
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
number=i.getExtras().getString("number");
android:orientation="vertical"
tNumber.setText(number);
android:layout_width="fill_parent"
br=new BroadcastReceiver(){
android:layout_height="fill_parent"
@Override
>
public void onReceive(Context context, Intent intent) {
<LinearLayout android:id="@+id/LinearLayout02" android:layout_width="wrap_content"
if(getResultCode()==Activity.RESULT_OK)
android:layout_height="wrap_content">
{
<TextView android:id="@+id/TextView01" android:layout_width="wrap_content"
new AlertDialog.Builder(context)
android:layout_height="wrap_content" android:text="number "></TextView>
.setMessage("SMS_SENT").setNeutralButton("OK",null)
<EditText android:id="@+id/editText_num" android:layout_width="wrap_content"
.setTitle("MESSAGE").show();
android:layout_height="wrap_content" android:phoneNumber="true"></EditText>
}
</LinearLayout>
else
<LinearLayout android:id="@+id/LinearLayout03" android:layout_width="wrap_content"
new AlertDialog.Builder(context)
android:layout_height="wrap_content">
.setMessage("SMS_SENT_ERROR").setNeutralButton("OK",null)
<TextView android:id="@+id/TextView02"
.setTitle("MESSAGE").show();
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="name ">
unregisterReceiver(br);
</TextView>
}
<EditText android:id="@+id/editText_name" android:layout_width="wrap_content"
};
android:layout_height="wrap_content"></EditText>
}
</LinearLayout>
public void butSend_Click(View v)
{
<LinearLayout android:id="@+id/LinearLayout01" android:layout_width="wrap_content"
try
android:layout_height="wrap_content">
{
<Button android:text="Add" android:id="@+id/but_in"
PendingIntent pi=PendingIntent.getBroadcast(this, 0, new
android:layout_width="wrap_content"
Intent("SMS_SENT"), 0);
android:layout_height="wrap_content"
SmsManager sms = SmsManager.getDefault();
android:onClick="but_inClick"></Button> import android.widget.SimpleCursorAdapter;
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" import android.widget.AdapterView.OnItemClickListener;
android:id="@+id/but_Edit" android:text="Edit"
android:onClick="but_EditClick"></Button> public class usingSQLHelper extends Activity {
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" mySqlHelper dbHelper;
android:id="@+id/but_delete" android:text="Delete" private EditText ed_num;
android:onClick="but_DelClick"></Button> private EditText ed_name;
</LinearLayout> protected Cursor cursor;
<TextView android:id="@+id/TextView03" android:layout_width="wrap_content" private int id=-1;
android:layout_height="wrap_content" android:text="DATA"> protected ListAdapter adapter;
</TextView><ListView android:id="@+id/ListView01" protected ListView numberList;
android:layout_width="wrap_content"
android:layout_height="wrap_content"> /** Called when the activity is first created. */
</ListView> @Override
</LinearLayout> public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View2.xml setContentView(R.layout.main);
<?xml version="1.0" encoding="utf-8"?> this.ed_num = (EditText) this.findViewById(R.id.editText_num);
this.ed_name = (EditText) this.findViewById(R.id.editText_name);
<TableLayout android:id="@+id/TableLayout01" this.numberList = (ListView) this.findViewById(R.id.ListView01);
android:layout_width="fill_parent" android:layout_height="fill_parent" dbHelper = new mySqlHelper(this);
xmlns:android="http://schemas.android.com/apk/res/android"> numberList.setSelected(true);
<TextView android:layout_width="wrap_content" numberList.setOnItemClickListener(new OnItemClickListener() {
android:layout_height="wrap_content" public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
android:text="myName" android:id="@+id/name"></TextView> long arg3) {
<TextView android:layout_width="wrap_content" SQLiteDatabase db = dbHelper.getReadableDatabase();
android:layout_height="wrap_content" cursor = db.rawQuery("SELECT * FROM data",null);
android:text="myNumber" android:id="@+id/number"></TextView> cursor.moveToPosition(arg2);
</TableLayout> ed_num.setText(cursor.getString(1));
ed_name.setText(cursor.getString(2));
id=cursor.getInt(0);
b. Source Java }});
MainActivity.java view();
package com.swu.usingSQLHelper; }
private void addData(String num,String name) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
import android.app.Activity; try{
import android.database.Cursor; db.execSQL("insert into "+ mySqlHelper.TABLE +"
import android.database.sqlite.SQLiteDatabase; values(null,'"+num+"','"+name+"')");
import android.os.Bundle; }
import android.view.View; catch(Exception e)
import android.widget.AdapterView; {
import android.widget.EditText; ed_num.setText(e.toString());
import android.widget.ListAdapter; }
import android.widget.ListView; }
public void but_inClick(View v)
{ new String[] {"number","name"},
addData(ed_num.getText().toString(),ed_name.getText().toString()); new int[] {R.id.number,R.id.name});
view();
} numberList.setAdapter(adapter);}
public void but_DelClick(View v) catch(Exception e)
{ {
SQLiteDatabase db = dbHelper.getWritableDatabase(); ed_num.setText(e.toString());
try{ }
if(id!=-1) }
{ }
db.execSQL("delete from "+ mySqlHelper.TABLE +" where
number='" +ed_num.getText().toString()+ "'"); mySqlHelper.java
view(); package com.swu.usingSQLHelper;
}
} import android.content.Context;
catch(Exception e) import android.database.sqlite.SQLiteDatabase;
{ import android.database.sqlite.SQLiteOpenHelper;
ed_num.setText(e.toString()); import android.util.Log;
}
} public class mySqlHelper extends SQLiteOpenHelper{
public void but_EditClick(View v)
{ private static final String DATABASE_NAME = "mydatabase.db";
SQLiteDatabase db = dbHelper.getWritableDatabase(); private static final int DATABASE_VERSION = 1;
try{
if(id!=-1) // Table name
{ public static final String TABLE = "data";
db.execSQL("update data set
number='"+ed_num.getText().toString()+"',name='"+ed_name.getText().toString()+"' // Columns
where _id="+id); public static final String number = "number";
view(); public static final String name = "name";
}
} public mySqlHelper(Context context) {
catch(Exception e) super(context, DATABASE_NAME, null, DATABASE_VERSION);
{ // TODO Auto-generated constructor stub
ed_num.setText(e.toString()); }
} @Override
} public void onCreate(SQLiteDatabase db) {
private void view() String sql = "create table " + TABLE + "( _id"
{ + " integer primary key autoincrement, " + number + " text not null, "
SQLiteDatabase db = dbHelper.getReadableDatabase(); + name + " text not null);";
try{ Log.d("Data", "onCreate: " + sql);
cursor = db.rawQuery("SELECT * FROM data",null); db.execSQL(sql);
adapter = new SimpleCursorAdapter(
this, }
R.layout.view2,
cursor,
@Override b. Source Java
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub MainActivity.java
package com.swu.Book;

} import android.app.Activity;
import android.database.Cursor;
} import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
11. BOOK import
import
android.view.View;
android.widget.EditText;
a. Layout import android.widget.Button;
main.xml import android.widget.ListAdapter;
<?xml version="1.0" encoding="utf-8"?> import android.widget.ListView;
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" import android.widget.SimpleCursorAdapter;
android:orientation="vertical" import android.widget.TextView;
android:layout_width="fill_parent"
android:layout_height="fill_parent" public class Book extends Activity {
> mySqlHelper dbHelper;
<TextView private TextView Jumlah;
android:layout_width="fill_parent" private EditText ed_Search;
android:layout_height="wrap_content" private TextView mytext;
android:text="@string/hello" private Button but_Search;
android:id="@+id/mytext"/> protected Cursor cursor;
<LinearLayout android:id="@+id/LinearLayout01" protected ListAdapter adapter;
android:layout_width="wrap_content" android:layout_height="wrap_content"> protected ListView List;
<EditText android:id="@+id/Ed_search" /** Called when the activity is first created. */
android:layout_width="wrap_content" android:layout_height="wrap_content"> @Override
</EditText> public void onCreate(Bundle savedInstanceState) {
<Button android:text="SEARCH" android:id="@+id/But_Search" super.onCreate(savedInstanceState);
android:layout_width="wrap_content" android:layout_height="wrap_content" setContentView(R.layout.main);
android:onClick="but_SearchClick"> dbHelper = new mySqlHelper(this);
</Button> mytext=(TextView) findViewById(R.id.mytext);
</LinearLayout><TextView android:layout_width="wrap_content" but_Search=(Button) findViewById(R.id.But_Search);
android:layout_height="wrap_content" android:id="@+id/text_Jumlah" ed_Search=(EditText) findViewById(R.id.Ed_search);
android:text="Jumlah :"> List=(ListView) findViewById(R.id.ListView01);
</TextView> Jumlah=(TextView) findViewById(R.id.text_Jumlah);
<ListView android:id="@+id/ListView01" android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView> }
</LinearLayout> public void but_SearchClick(View v)
{
try{
SQLiteDatabase db = dbHelper.getReadableDatabase();
cursor = db.rawQuery("SELECT _id,title,type FROM book where title
LIKE '%"
+ed_Search.getText().toString()+"%'" }
,null); @Override
adapter = new SimpleCursorAdapter( public void onCreate(SQLiteDatabase db) {
this, String sql = "create table " + TABLE + "( _id"
android.R.layout.simple_list_item_2, + " integer primary key autoincrement, " + title + " text not null, "
cursor, + type + " text not null);";
new String[] {"title","type"}, Log.d("Data", "onCreate: " + sql);
new int[] {android.R.id.text1,android.R.id.text2}); db.execSQL(sql);
List.setAdapter(adapter);
cursor = db.rawQuery("select count(type) from book where title LIKE ContentValues newValues = new ContentValues();
'%" newValues.put("title", "Mengenal android");
+ed_Search.getText().toString()+"%'",null); newValues.put("type", "technology");
cursor.moveToFirst(); db.insert(TABLE, null, newValues);
Jumlah.setText("Jumlah : "+cursor.getInt(0));
} newValues = new ContentValues();
catch(Exception e) newValues.put("title", "Memasak dengan mudah");
{ newValues.put("type", "hoby");
mytext.setText(e.toString()); db.insert(TABLE, null, newValues);
}
newValues = new ContentValues();
} newValues.put("title", "cerita anak sma");
} newValues.put("type", "fiction");
db.insert(TABLE, null, newValues);
mySqlHelper.java
package com.swu.Book; newValues = new ContentValues();
newValues.put("title", "Memancing di danau");
import android.content.ContentValues; newValues.put("type", "hoby");
import android.content.Context; db.insert(TABLE, null, newValues);
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper; newValues = new ContentValues();
import android.util.Log; newValues.put("title", "cinta membawa hati");
newValues.put("type", "fiction");
public class mySqlHelper extends SQLiteOpenHelper{ db.insert(TABLE, null, newValues);

private static final String DATABASE_NAME = "database.db"; newValues = new ContentValues();


private static final int DATABASE_VERSION = 1; newValues.put("title", "belajar menari dengan kaset");
newValues.put("type", "hoby");
// Table name db.insert(TABLE, null, newValues);
public static final String TABLE = "book";
// Columns }
public static final String title = "title";
public static final String type = "type"; @Override
public mySqlHelper(Context context) { public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
super(context, DATABASE_NAME, null, DATABASE_VERSION); // TODO Auto-generated method stub
// TODO Auto-generated constructor stub db.execSQL("DROP TABLE IF EXISTS " + TABLE);
// Create a new one.
onCreate(db); android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
} android:background="#fff">
<ImageView android:layout_width="wrap_content"
} android:layout_height="wrap_content"
android:id="@+id/splashView"
12. KALORI METER android:src="@drawable/mylogo"
android:layout_centerInParent="true"></ImageView>
a. Layout
main.xml </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" view2.xml
android:orientation="vertical" <?xml version="1.0" encoding="utf-8"?>
android:layout_width="fill_parent"
android:layout_height="fill_parent" <TableLayout android:id="@+id/TableLayout01" android:layout_width="fill_parent"
> android:layout_height="fill_parent"
<TextView xmlns:android="http://schemas.android.com/apk/res/android">
android:layout_width="fill_parent" <TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textSize="20px" android:layout_height="wrap_content"
android:gravity="center" android:text="FoodName" android:id="@+id/Name_view"></TextView>
android:text="Kalorimeter"/> <TableRow android:id="@+id/TableRow01" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout android:id="@+id/LinearLayout01" <TextView android:layout_width="wrap_content"
android:layout_width="wrap_content" android:layout_height="wrap_content"> android:layout_height="wrap_content"
<EditText android:id="@+id/edT_Search" android:text="myweight" android:id="@+id/weight_view"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:width="100dp"></TextView>
android:width="250dp" android:singleLine="true"> <TextView android:id="@+id/TextView01" android:layout_width="wrap_content"
</EditText> android:layout_height="wrap_content" android:text="(gr)"
<ImageButton android:id="@+id/but_Search" android:width="50sp"></TextView>
android:layout_width="wrap_content" android:layout_height="wrap_content" <TextView android:text="@+id/kalori" android:id="@+id/kal_view"
android:adjustViewBounds="true" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:maxHeight="50dp" android:maxWidth="50dp" android:width="100sp">
android:onClick="but_SearchClick"> </TextView>
</ImageButton> <TextView android:id="@+id/TextView01" android:layout_width="wrap_content"
</LinearLayout> android:layout_height="wrap_content" android:width="30sp"
android:text="(kal)"></TextView>
<ListView android:id="@+id/listV_food"
android:layout_width="wrap_content" </TableRow>
android:layout_height="wrap_content"></ListView> </TableLayout>
</LinearLayout>
detail.xml
splash.xml <?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:id="@+id/RelativeLayout01"
android:layout_width="fill_parent"
<RelativeLayout android:id="@+id/RelativeLayout01"
android:layout_height="fill_parent" android:layout_toRightOf="@+id/ed_Weight" android:onClick="but_AddListClick"
xmlns:android="http://schemas.android.com/apk/res/android"> android:text="Add">
<TextView android:id="@+id/TextView01" android:layout_width="wrap_content" </Button>
android:layout_height="wrap_content" android:text="Details" <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_centerHorizontal="true"> android:layout_toRightOf="@+id/but_AddList"
</TextView> android:layout_below="@+id/TextView05"
<TextView android:id="@+id/TextView02" android:layout_below="@id/TextView01" android:text="Edit" android:id="@+id/but_Edit" android:onClick="but_EditListClick">
android:layout_width="wrap_content" android:layout_height="wrap_content" </Button>
android:text="Name :" <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:height="20sp" android:textSize="15sp"></TextView> android:layout_toRightOf="@+id/but_Edit" android:layout_below="@+id/TextView05"
<TextView android:id="@+id/TextView03" android:id="@+id/But_delete" android:text="Delete"
android:layout_height="wrap_content" android:layout_below="@+id/TextView02" android:onClick="but_DeleteListClick"></Button>
android:layout_width="wrap_content" android:text="Kalori : " android:height="20sp" <TextView android:layout_toRightOf="@+id/TextView03"
android:textSize="15sp"></TextView><TextView android:id="@+id/TextView04" android:layout_height="wrap_content"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/TextView02" android:layout_width="wrap_content"
android:layout_below="@+id/TextView03"></TextView><TextView android:text="FoodKalori" android:id="@+id/text_foodKalori" android:height="20sp"
android:id="@+id/TextView05" android:textSize="15sp"></TextView>
android:layout_width="wrap_content" android:layout_height="wrap_content" <TextView android:layout_toRightOf="@+id/text_foodKalori"
android:text="Food :" android:layout_below="@+id/text_foodKalori"></TextView> android:layout_height="wrap_content"
<TextView android:layout_width="wrap_content" android:layout_below="@+id/TextView02" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="/foodWeight" android:id="@+id/text_FoodWeight" android:height="20sp"
android:layout_toRightOf="@+id/TextView05" android:id="@+id/text_Yourfood" android:textSize="15sp"></TextView><TextView
android:text="YourFood" android:layout_toRightOf="@+id/TextView02"
android:layout_below="@+id/text_foodKalori"></TextView> android:layout_height="wrap_content" android:layout_below="@id/TextView01"
<TextView android:id="@+id/TextView06" android:layout_width="wrap_content" android:layout_width="wrap_content" android:text="FoodName"
android:layout_height="wrap_content" android:layout_below="@+id/TextView05" android:id="@+id/text_foodName" android:height="20sp"
android:text="weight(gr) :" android:height="40dp" android:textSize="15sp"></TextView>
android:gravity="center"></TextView> </RelativeLayout>
<EditText android:layout_width="wrap_content"
android:layout_height="wrap_content" b. Source Java
android:layout_toRightOf="@+id/TextView06"
android:layout_below="@+id/TextView05" Kalorimeter.java
android:numeric="integer" android:width="70dp" android:id="@+id/ed_Weight"> package com.swu.KaloriMeter;
</EditText>
<TextView android:layout_width="wrap_content" import android.app.Activity;
android:layout_height="wrap_content" import android.content.Context;
android:text="@+id/Total" android:id="@+id/text_Total" import android.content.Intent;
android:layout_centerHorizontal="true" import android.database.Cursor;
android:textColor="#f00" android:layout_below="@+id/ed_Weight" import android.database.sqlite.SQLiteDatabase;
android:textSize="15sp"></TextView> import android.os.Bundle;
<ListView android:layout_width="wrap_content" android:layout_height="wrap_content" import android.view.View;
android:id="@+id/lvFood_det" android:layout_centerHorizontal="true" import android.widget.AdapterView;
android:layout_below="@+id/text_Total"> import android.widget.EditText;
</ListView> import android.widget.ImageButton;
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" import android.widget.ListAdapter;
android:layout_below="@+id/TextView05" android:id="@+id/but_AddList" import android.widget.ListView;
import android.widget.SimpleCursorAdapter; public void but_SearchClick(View v)
import android.widget.AdapterView.OnItemClickListener; {
//LAkukan query sesuai masukan
public class KaloriMeter extends Activity { SQLiteDatabase db = dbHelper.getReadableDatabase();
try{
mySqlHelper dbHelper; cursor = db.rawQuery("select * from data where name LIKE
private ImageButton but_Search; '%"+ed_Search.getText().toString()+"%'",null);
private EditText ed_Search; adapter = new SimpleCursorAdapter(
private Cursor cursor; this, android.R.layout.simple_list_item_2,
private ListAdapter adapter; cursor,
private ListView foodList; new String[] {"name"}, new int[]{android.R.id.text1});
private Context con; foodList.setAdapter(adapter);}
/** Called when the activity is first created. */ catch(Exception e)
@Override {
public void onCreate(Bundle savedInstanceState) { ed_Search.setText(e.toString());
super.onCreate(savedInstanceState); }
setContentView(R.layout.main); }
but_Search=(ImageButton) findViewById(R.id.but_Search); }
ed_Search=(EditText) findViewById(R.id.edT_Search); MySQLHelper.java
but_Search.setImageResource(android.R.drawable.ic_menu_search); package com.swu.KaloriMeter;
this.foodList = (ListView) this.findViewById(R.id.listV_food);
con=this; import java.io.FileOutputStream;
foodList.setSelected(true); import java.io.IOException;
foodList.setOnItemClickListener(new OnItemClickListener() { import java.io.InputStream;
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, import java.io.OutputStream;
long arg3) {
SQLiteDatabase db = dbHelper.getReadableDatabase(); import android.content.Context;
cursor = db.rawQuery("select * from data where name LIKE '%" import android.database.sqlite.SQLiteDatabase;
+ed_Search.getText().toString()+"%'",null); import android.database.sqlite.SQLiteException;
cursor.moveToPosition(arg2); import android.database.sqlite.SQLiteOpenHelper;
int id=cursor.getInt(cursor.getColumnIndex("_id"));
Detail(id); public class mySqlHelper extends SQLiteOpenHelper{
}});
dbHelper = new mySqlHelper(this); private static String DB_PATH = "/data/data/com.swu.KaloriMeter/databases/";
try {
dbHelper.createDataBase(); private static final String DATABASE_NAME = "kalori.db";
} private static final int DATABASE_VERSION = 1;
catch (Exception e) {ed_Search.setText(e.toString());;}
} // Table name
//mulai activity detail dengan mengirim id public static final String TABLE = "data";
public void Detail(int id)
{ private Context myContext;
Intent detail=new Intent(con, Detail.class);
detail.putExtra("id", id); public mySqlHelper(Context context) {
this.startActivity(detail); super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
myContext=context;
} //buka db kosong sebagai output stream
public void createDataBase() throws IOException{ OutputStream myOutput = new FileOutputStream(outFileName);
if(DataBaseisExist()){
//tidk melakukan apapun, db telah ada //transfer bytes dari inputfile ke outputfile
} byte[] buffer = new byte[1024];
else{ int length;
//dengan memanggil method ini, db kosong akan dibuat sehingga while ((length = myInput.read(buffer))>0){
dapat ditulisi oleh myOutput.write(buffer, 0, length);
//database aplikasi }
this.getReadableDatabase(); //Close the streams
myOutput.flush();
try { myOutput.close();
copyDataBase(); myInput.close();
} catch (IOException e) { }
throw new Error("Error copying database");
} @Override
} public void onCreate(SQLiteDatabase db) {

} }
private boolean DataBaseisExist(){
@Override
SQLiteDatabase checkDB = null; public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
try{ // TODO Auto-generated method stub
String myPath = DB_PATH + DATABASE_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY); }

}catch(SQLiteException e){
}
//database tidak ada Splash.java
package com.swu.KaloriMeter;
}
if(checkDB != null){ import android.app.Activity;
checkDB.close(); import android.content.Context;
} import android.content.Intent;
import android.os.Bundle;
if(checkDB != null )return true ;else return false; import android.widget.ImageView;
}
private void copyDataBase() throws IOException{ public class splash extends Activity {
private ImageView image;
//buka lokal database sebagai input stream private Thread mThread;
InputStream myInput = myContext.getAssets().open(DATABASE_NAME); /** Called when the activity is first created. */
@Override
// Path untuk pembuatan db public void onCreate(Bundle savedInstanceState) {
String outFileName = DB_PATH + DATABASE_NAME;
super.onCreate(savedInstanceState); import android.widget.SimpleCursorAdapter;
setContentView(R.layout.splash); import android.widget.TextView;
image=(ImageView) findViewById(R.id.splashView); import android.widget.AdapterView.OnItemClickListener;
final Context myContext=this;
image.setImageResource(R.drawable.mylogo); public class Detail extends Activity {
mySqlHelper dbHelper;
mThread = new Thread(){ private TextView name;
@Override private TextView kal;
public void run(){ private TextView weight;
try { private TextView yourFood;
synchronized(this){ private TextView total;
// Wait given period of time or exit on touch private EditText myweight;
wait(5000);
} private Cursor cursor;
} private ListAdapter adapter;
catch(InterruptedException ex){ private ListView lvFood;
} private int food_id;
private int food_idKlik=-1;
finish(); /** Called when the activity is first created. */
@Override
// Run next activity public void onCreate(Bundle savedInstanceState) {
Intent intent = new Intent(); super.onCreate(savedInstanceState);
intent.setClass(myContext, KaloriMeter.class); setContentView(R.layout.detail);
startActivity(intent); Intent main=getIntent();
stop(); food_id=main.getExtras().getInt("id");
} name=(TextView) findViewById(R.id.text_foodName);
}; kal=(TextView) findViewById(R.id.text_foodKalori);
mThread.start(); weight=(TextView) findViewById(R.id.text_FoodWeight);
} total=(TextView) findViewById(R.id.text_Total);
yourFood=(TextView) findViewById(R.id.text_Yourfood);
} myweight=(EditText) findViewById(R.id.ed_Weight);
Detail.java lvFood=(ListView) findViewById(R.id.lvFood_det);
package com.swu.KaloriMeter; dbHelper = new mySqlHelper(this);
//tampilkan data sesuai id yang dikirim sebelumnya
import android.app.Activity; try {
import android.content.Intent; SQLiteDatabase db = dbHelper.getReadableDatabase();
import android.database.Cursor; Cursor selectedFood=db.rawQuery("select * from data where
import android.database.sqlite.SQLiteDatabase; _id="+food_id,null);
import android.os.Bundle; selectedFood.moveToFirst();
import android.util.Log; name.setText(selectedFood.getString(selectedFood.getColumnIndex("name")));
import android.view.View;
import android.widget.AdapterView; yourFood.setText(selectedFood.getString(selectedFood.getColumnIndex("name")));
import android.widget.EditText;
import android.widget.ListAdapter; kal.setText(String.valueOf(selectedFood.getDouble(selectedFood.getColumnIndex("Kalori"))));
import android.widget.ListView; weight.setText(" /
"+String.valueOf(selectedFood.getDouble(selectedFood.getColumnIndex("weight")))+" gr");
} {
catch (Exception ioe) {Log.e("err",ioe.toString());} try{
lvFood.setSelected(true); db.execSQL("update mydata set weight="+myweight.getText()
lvFood.setOnItemClickListener(new OnItemClickListener() { +" where food_id="+food_idKlik);
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, }
long arg3) { catch(Exception e) {yourFood.setText("nothing click");}
try{ }
SQLiteDatabase db = dbHelper.getReadableDatabase(); view();
cursor = db.rawQuery("select * from mydata",null); }
cursor.moveToPosition(arg2); //untuk menghapus daftar
food_idKlik =cursor.getInt(cursor.getColumnIndex("food_id")); public void but_DeleteListClick(View v)
Cursor selectedFood=db.rawQuery("select * from data where {
_id="+food_idKlik,null); SQLiteDatabase db = dbHelper.getWritableDatabase();
selectedFood.moveToFirst(); if(myweight.getText().length()!=0)
{
yourFood.setText(selectedFood.getString(selectedFood.getColumnIndex("name"))); try{
Cursor selected=db.rawQuery("select * from mydata where db.execSQL("delete from mydata where food_id="+food_idKlik);}
food_id="+food_idKlik,null);
selected.moveToFirst(); catch(Exception e) {yourFood.setText("nothing click");}
}
myweight.setText(selected.getString(selected.getColumnIndex("weight")));} view();
catch(Exception e) {Log.e("err",e.toString());} }
}}); private void view()
view(); {
} SQLiteDatabase db = dbHelper.getReadableDatabase();
//untuk menambahkan ke daftar try{
public void but_AddListClick(View v) cursor = db.rawQuery("SELECT A._id as _id,A.name as
{ name,B.weight as weight,(B.weight/A.weight)*A.Kalori as total FROM data A,mydata B where
SQLiteDatabase db = dbHelper.getWritableDatabase(); A.[_id]=B.food_id",null);
if(myweight.getText().length()!=0)
{ adapter = new SimpleCursorAdapter(
if(db.rawQuery("select * from mydata where this,
food_id="+food_id,null).getCount()==0) R.layout.view2,
db.execSQL("insert into mydata values cursor,
(null,"+food_id+","+myweight.getText()+")"); new String[] {"name","weight","total"},
else new int[]
db.execSQL("update mydata set {R.id.Name_view,R.id.weight_view,R.id.kal_view});
weight=weight+"+myweight.getText().toString()+" where food_id="+food_id);
} lvFood.setAdapter(adapter);}
view(); catch(Exception e)
} {
//untuk mengubah daftar Log.e("err",e.toString());
public void but_EditListClick(View v) }
{ Cursor totalC=db.rawQuery("select round(sum((B.weight/A.weight)*A.Kalori),3)
SQLiteDatabase db = dbHelper.getWritableDatabase(); as total FROM data A,mydata B where A.[_id]=B.food_id",null);
if(myweight.getText().length()!=0) totalC.moveToFirst();
total.setText("TOTAL :"+String.valueOf(totalC.getDouble(0))+"(Kal)"); import com.google.android.gms.maps.GoogleMap;
} import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.GoogleMap.OnMyLocationChangeListener;
} import com.google.android.gms.maps.model.LatLng;

import android.app.Activity;
AndroidManifest import android.app.Dialog;
<?xml version="1.0" encoding="utf-8"?> import android.location.Location;
<manifest xmlns:android="http://schemas.android.com/apk/res/android" import android.os.Bundle;
package="com.swu.KaloriMeter" import android.support.v4.app.FragmentActivity;
android:versionCode="1" import android.widget.TextView;
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name"> public class LokasiKu extends FragmentActivity implements
<activity android:name=".KaloriMeter" OnMyLocationChangeListener {
android:label="@string/app_name">
<intent-filter> GoogleMap googleMap;
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter> @Override
</activity> protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
<activity android:name="splash" setContentView(R.layout.lokasisaya);
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"><intent-filter><action
android:name="android.intent.action.MAIN"></action> //googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
<category android:name="android.intent.category.LAUNCHER"></category>
</intent-filter> // Getting Google Play availability status
</activity> int status =
<activity android:name="Detail"></activity> GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());

</application> // Showing status


<uses-sdk android:minSdkVersion="7" /> if(status!=ConnectionResult.SUCCESS){ // Google Play Services are not available

</manifest> int requestCode = 10;


Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this,
requestCode);
13. MY LOCATION dialog.show();
a. Layout
b. Source Java }else { // Google Play Services are available

Lokasiku // Getting reference to the SupportMapFragment of activity_main.xml


package com.android.lokasiku; SupportMapFragment fm = (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.map1);

import com.google.android.gms.common.ConnectionResult; // Getting GoogleMap object from the fragment


import com.google.android.gms.common.GooglePlayServicesUtil; googleMap = fm.getMap();
import com.google.android.gms.maps.CameraUpdateFactory;
// Enabling MyLocation Layer of Google Map c. Layout
googleMap.setMyLocationEnabled(true);
Lokasisaya.xml
// Setting event handler for location change <?xml version="1.0" encoding="utf-8"?>
googleMap.setOnMyLocationChangeListener(this); <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
} android:layout_width="match_parent"
android:layout_height="match_parent" >
}
<TextView
android:id="@+id/tv_location1"
//@Override android:layout_width="wrap_content"
//public boolean onCreateOptionsMenu(Menu menu) { android:layout_height="wrap_content"
// Inflate the menu; this adds items to the action bar if it is present. />
//getMenuInflater().inflate(R.menu.lokasiku, menu);
//return true; <fragment
//} android:id="@+id/map1"
android:layout_width="match_parent"
android:layout_height="match_parent"
@Override android:layout_below="@id/tv_location1"
public void onMyLocationChange(Location location) { class="com.google.android.gms.maps.SupportMapFragment"
TextView tvLocation = (TextView) findViewById(R.id.tv_location1); />

// Getting latitude of the current location </RelativeLayout>


double latitude = location.getLatitude();
AndroidManifest.xml
// Getting longitude of the current location <?xml version="1.0" encoding="utf-8"?>
double longitude = location.getLongitude(); <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.lokasisaya"
// Creating a LatLng object for the current location android:versionCode="1"
LatLng latLng = new LatLng(latitude, longitude); android:versionName="1.0" >

// Showing the current location in Google Map <uses-sdk


googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); android:minSdkVersion="8"
android:targetSdkVersion="17" />
// Zoom in the Google Map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15)); <permission
android:name="com.android.hotel.permission.MAPS_RECEIVE"
// Setting latitude and longitude in the TextView tv_location android:protectionLevel="signature" />
tvLocation.setText("Posisi Anda saat ini, Latitude:" + latitude + ",
Longitude:"+ longitude );
<uses-permission
} android:name="turtle.map.googlemaptest.permission.MAPS_RECEIVE" />
} <uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:orientation="vertical"
android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> android:layout_width="fill_parent"
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" android:layout_height="fill_parent"
/> android:background="#ffff">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <ImageView android:id="@+id/imgSplash" android:layout_width="wrap_content"
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> android:layout_height="wrap_content"
<uses-permission android:name="android.permission.INTERNET" /> android:layout_centerInParent="true"></ImageView>
<uses-permission </RelativeLayout>
android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> b. Source Java
<uses-permission
android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.CALL_PHONE"></uses- Splash.java
permission> package com.SAS.Pingsut;
<uses-feature android:glEsVersion="0x00020000"
android:required="true" /> import android.app.Activity;
import android.content.Context;
<application import android.content.Intent;
android:allowBackup="true" import android.graphics.drawable.AnimationDrawable;
android:icon="@drawable/ic_launcher" import android.os.Bundle;
android:label="@string/app_name" import android.view.animation.Animation;
android:theme="@style/AppTheme" > import android.view.animation.AnimationUtils;
import android.widget.ImageView;
<meta-data android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyAxidJfcPtJPCswvGg_724MWdXC_QB2-Zs" /> public class splash extends Activity {
private ImageView image;
<activity private Thread mThread;
android:name="com.android.wisatawonosobo.MainActivity" private AnimationDrawable animasi;
android:label="@string/app_name" > private Animation myAnim;
<intent-filter> /** Called when the activity is first created. */
<action android:name="android.intent.action.MAIN" /> @Override
public void onCreate(Bundle savedInstanceState) {
<category android:name="android.intent.category.LAUNCHER" /> super.onCreate(savedInstanceState);
</intent-filter> setContentView(R.layout.main);
</activity> image=(ImageView) findViewById(R.id.imgSplash);
final Context myContext=this;
</application> image.setImageResource(R.drawable.mylogo);
animasi = (AnimationDrawable) image.getBackground();
</manifest> myAnim = AnimationUtils.loadAnimation(this, R.anim.myanim);
image.startAnimation(myAnim);
14. GAME PINGSUT mThread = new Thread(){
@Override
a. Layout public void run(){
Mainxml try {
<?xml version="1.0" encoding="utf-8"?> synchronized(this){
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" // Wait given period of time or exit on touch
wait(3000); import android.media.MediaPlayer;
} import android.util.AttributeSet;
} import android.view.KeyEvent;
catch(InterruptedException ex){ import android.view.MotionEvent;
} import android.view.View;

finish();
public class myChoice extends View {
// Run next activity private Context context;
Intent intent = new Intent(); private float sizeImg=75,x,y,posX,posY,mPosImgY;
intent.setClass(myContext, Pingsut.class); private float imgFightX1=10,imgFightX2;
startActivity(intent); private float[] mPosImgX=new float[3];
stop(); private int[] optionFinger={R.drawable.jempol, R.drawable.telunjuk,R.drawable.jentik};
} private int[] selectedFinger={R.drawable.jempol2,
}; R.drawable.telunjuk2,R.drawable.jentik2};
mThread.start(); private int[] imgFight={R.drawable.gajah,R.drawable.orang,R.drawable.semut};
} private int choice=-1,compChoice=-1,alpha=0;
private boolean running=true;
} private volatile boolean fight=false,viewMsg=false;
Pingsut.java private View myView;
package com.SAS.Pingsut; private String message=new String();

import android.app.Activity; public myChoice(Context context) {


import android.os.Bundle; super(context);
this.context=context;
public class Pingsut extends Activity { myView=this;
/** Called when the activity is first created. */ this.setFocusable(true);
@Override this.setFocusableInTouchMode(true);
public void onCreate(Bundle savedInstanceState) { this.setBackgroundResource(R.drawable.back);
super.onCreate(savedInstanceState); (new Thread(new AnimationLoop())).start();
setContentView(new myChoice(this)); }
} public myChoice(Context context,AttributeSet attrs) {
} super(context,attrs);
}
@Override
myChoice.java protected void onDraw(Canvas canvas) {
package com.SAS.Pingsut; mPosImgX[0]=10;
mPosImgX[1]=(this.getWidth()-sizeImg)/2;
import java.util.Random; mPosImgX[2]=(this.getWidth()-sizeImg);
mPosImgY=this.getHeight()-sizeImg;
import android.content.Context; x=(this.getWidth()-sizeImg)/2;
import android.graphics.Bitmap; y=10;
import android.graphics.BitmapFactory; if(!fight)
import android.graphics.Canvas; imgFightX2=this.getWidth()-sizeImg;
import android.graphics.Color; Bitmap mFinger;
import android.graphics.Paint; mFinger= BitmapFactory
.decodeResource(getResources(), else
optionFinger[compChoice]); {
canvas.drawBitmap(mFinger, x,y, null); message="Draw";
MediaPlayer mp = MediaPlayer.create(context,
for(int i=0;i<mPosImgX.length;i++) R.raw.sad);
{ mp.start();
if(choice==i) }
{ mFinger= BitmapFactory imgFightX1=10;
.decodeResource(getResources(), selectedFinger[i]); imgFightX2=this.getWidth()-sizeImg;
} }
else }
mFinger= BitmapFactory Paint paint=new Paint();
.decodeResource(getResources(), optionFinger[i]); paint.setColor(Color.RED);
canvas.drawBitmap(mFinger, mPosImgX[i],mPosImgY, null); paint.setTextSize(75);
} paint.setAlpha(alpha);
if(fight) canvas.save();
{ canvas.rotate(45f);
mFinger= BitmapFactory canvas.drawText(message, 40, 30, paint);
.decodeResource(getResources(), imgFight[choice]); canvas.restore();
canvas.drawBitmap(mFinger, imgFightX1,(this.getHeight()- }
sizeImg)/2, null);
mFinger= BitmapFactory @Override
.decodeResource(getResources(), imgFight[compChoice]); public boolean onTouchEvent(MotionEvent event) {
canvas.drawBitmap(mFinger, imgFightX2,(this.getHeight()- if(!fight)
sizeImg)/2, null); {
choice=-1;
if(imgFightX1>imgFightX2) posX=event.getX();
{ posY=event.getY();
fight=false;
viewMsg=true; for(int i=0;i<mPosImgX.length;i++)
int hasil=compChoice-choice;
if(hasil>0&&hasil!=2||hasil==-2) if(posX>mPosImgX[i]&&posX<mPosImgX[i]+sizeImg&&posY>mPosImgY&&posY<mPos
ImgY+sizeImg)
{ {
message="You Win"; choice=i;
MediaPlayer mp = this.invalidate();
MediaPlayer.create(context, R.raw.happy);
mp.start(); }
} if(!running&&!viewMsg)
else if(hasil<0||hasil==2) {
{ message="";
message="You Lose"; running=true;
MediaPlayer mp = MediaPlayer.create(context, }
R.raw.sad); if(viewMsg)
mp.start(); {
} alpha=0;
viewMsg=false; myView.postInvalidate();
} }
} catch(Exception ex)
return true; {
}
@Override }
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_DPAD_CENTER) }
{ if(fight)
if(running&&!fight&&choice!=-1) {
{ try
fight=true; {
running=false; Thread.sleep(100);
} imgFightX1+=10;
if(!running&&!fight&&!viewMsg) imgFightX2-=10;
{ myView.postInvalidate();
message=""; }
running=true; catch(Exception ex)
} {
if(viewMsg) }
{ }
alpha=0; if(viewMsg)
viewMsg=false; {
} try
} {
else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN || keyCode Thread.sleep(100);
==KeyEvent.KEYCODE_BACK) alpha+=50;
{ if(alpha==200) {
System.exit(0); alpha=0;
} }
myView.postInvalidate();
return true; }
} catch(Exception ex)
class AnimationLoop implements Runnable {
{ }
public void run() }
{ }
while(true) }
{ }
if(running) }
{
try AndroidManifest
{ <?xml version="1.0" encoding="utf-8"?>
Thread.sleep(50); <manifest xmlns:android="http://schemas.android.com/apk/res/android"
compChoice=new package="com.SAS.Pingsut"
Random().nextInt(3);
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Pingsut"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
//muncul lebih dulu
<activity android:name="splash">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
//sebagai launcher
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

</activity>
</application>
<uses-sdk android:minSdkVersion="7" />

</manifest>

You might also like