Pemrograman

Implementasi Broadcast Receiver Android

Posted on

  1. Buat splash_screen.xml sebagai tampilan awal seperti dibawah ini:

<RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android&#8221;

xmlns:tools=http://schemas.android.com/tools&#8221;

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:animateLayoutChanges=“true” >

<ImageView

android:id=“@+id/logo”

android:layout_width=“120dp”

android:layout_height=“120dp”

android:layout_centerHorizontal=“true”

android:layout_centerVertical=“true” />

<RelativeLayout

android:layout_width=“135dp”

android:layout_height=“wrap_content”

android:layout_below=“@+id/logo”

android:gravity=“center”

android:layout_centerHorizontal=“true”

android:animateLayoutChanges=“true” >

<TextView

android:id=“@+id/t1”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content” />

<TextView

android:id=“@+id/t2”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_toRightOf=“@+id/t1” />

<TextView

android:id=“@+id/t3”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_toRightOf=“@+id/t2” />

<TextView

android:id=“@+id/t4”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_toRightOf=“@+id/t3” />

<TextView

android:id=“@+id/t5”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_toRightOf=“@+id/t4” />

<TextView

android:id=“@+id/t6”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_toRightOf=“@+id/t5” />

<TextView

android:id=“@+id/t7”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_toRightOf=“@+id/t6” />

<TextView

android:id=“@+id/t8”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_toRightOf=“@+id/t7” />

<TextView

android:id=“@+id/t9”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_toRightOf=“@+id/t8” />

<TextView

android:id=“@+id/t10”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_toRightOf=“@+id/t9” />

<TextView

android:id=“@+id/t11”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_toRightOf=“@+id/t10” />

<TextView

android:id=“@+id/t12”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_toRightOf=“@+id/t11” />

<TextView

android:id=“@+id/t13”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_toRightOf=“@+id/t12” />

<TextView

android:id=“@+id/t14”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_toRightOf=“@+id/t13” />

<TextView

android:id=“@+id/t15”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_toRightOf=“@+id/t14” />

<TextView

android:id=“@+id/t16”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_toRightOf=“@+id/t15” />

<TextView

android:id=“@+id/t17”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_toRightOf=“@+id/t16” />

<TextView

android:id=“@+id/t18”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_toRightOf=“@+id/t17” />

<TextView

android:id=“@+id/t19”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_toRightOf=“@+id/t18” />

<TextView

android:id=“@+id/t20”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_toRightOf=“@+id/t19” />

</RelativeLayout>

</RelativeLayout>

  1. Buat activity SplashScreen.java seperti dibawah ini:

import android.app.Activity;

import android.content.Intent;

import android.content.SharedPreferences;

import android.os.Bundle;

import android.os.Handler;

import android.preference.PreferenceManager;

import android.view.Window;

import android.view.animation.Animation;

import android.view.animation.AnimationUtils;

import android.widget.ImageView;

import android.widget.TextView;

public class SplashScreen extends Activity {

private ImageView logo;

private Animation anim1;

private SharedPreferences pref;

private Handler handlerTimer = new Handler();

private TextView t1, t2, t3, t4, t5, t6, t7, t8, t9, t10,

t11, t12, t13, t14, t15, t16, t17, t18, t19, t20;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);

setContentView(R.layout.splash_screen);

init();

initAnimation();

}

private void init() {

logo = (ImageView) findViewById(R.id.logo);

t1 = (TextView) findViewById(R.id.t1);

t2 = (TextView) findViewById(R.id.t2);

t3 = (TextView) findViewById(R.id.t3);

t4 = (TextView) findViewById(R.id.t4);

t5 = (TextView) findViewById(R.id.t5);

t6 = (TextView) findViewById(R.id.t6);

t7 = (TextView) findViewById(R.id.t7);

t8 = (TextView) findViewById(R.id.t8);

t9 = (TextView) findViewById(R.id.t9);

t10 = (TextView) findViewById(R.id.t10);

t11 = (TextView) findViewById(R.id.t11);

t12 = (TextView) findViewById(R.id.t12);

t13 = (TextView) findViewById(R.id.t13);

t14 = (TextView) findViewById(R.id.t14);

t15 = (TextView) findViewById(R.id.t15);

t16 = (TextView) findViewById(R.id.t16);

t17 = (TextView) findViewById(R.id.t17);

t18 = (TextView) findViewById(R.id.t18);

t19 = (TextView) findViewById(R.id.t19);

t20 = (TextView) findViewById(R.id.t20);

}

private void initAnimation() {

anim1 = AnimationUtils.loadAnimation(this, R.anim.logo);

Thread animated = new Thread() {

public void run() {

logo.setImageResource(R.drawable.logo);

logo.startAnimation(anim1);

handlerTimer.postDelayed(new Runnable() {

public void run() {

t1.setText(“I”);

t1.setAnimation(AnimationUtils.loadAnimation(SplashScreen.this, android.R.anim.slide_in_left));

}

}, 1000);

handlerTimer.postDelayed(new Runnable() {

public void run() {

t2.setText(“n”);

t2.setAnimation(AnimationUtils.loadAnimation(SplashScreen.this, android.R.anim.slide_in_left));

}

}, 1050);

handlerTimer.postDelayed(new Runnable() {

public void run() {

t3.setText(“t”);

t3.setAnimation(AnimationUtils.loadAnimation(SplashScreen.this, android.R.anim.slide_in_left));

}

}, 1100);

handlerTimer.postDelayed(new Runnable() {

public void run() {

t4.setText(“e”);

t4.setAnimation(AnimationUtils.loadAnimation(SplashScreen.this, android.R.anim.slide_in_left));

}

}, 1150);

handlerTimer.postDelayed(new Runnable() {

public void run() {

t5.setText(“r”);

t5.setAnimation(AnimationUtils.loadAnimation(SplashScreen.this, android.R.anim.slide_in_left));

}

}, 1200);

handlerTimer.postDelayed(new Runnable() {

public void run() {

t6.setText(“a”);

t6.setAnimation(AnimationUtils.loadAnimation(SplashScreen.this, android.R.anim.slide_in_left));

}

}, 1250);

handlerTimer.postDelayed(new Runnable() {

public void run() {

t7.setText(“c”);

t7.setAnimation(AnimationUtils.loadAnimation(SplashScreen.this, android.R.anim.slide_in_left));

}

}, 1300);

handlerTimer.postDelayed(new Runnable() {

public void run() {

t8.setText(“t”);

t8.setAnimation(AnimationUtils.loadAnimation(SplashScreen.this, android.R.anim.slide_in_left));

}

}, 1350);

handlerTimer.postDelayed(new Runnable() {

public void run() {

t9.setText(“i”);

t9.setAnimation(AnimationUtils.loadAnimation(SplashScreen.this, android.R.anim.slide_in_left));

}

}, 1400);

handlerTimer.postDelayed(new Runnable() {

public void run() {

t10.setText(“v”);

t10.setAnimation(AnimationUtils.loadAnimation(SplashScreen.this, android.R.anim.slide_in_left));

}

}, 1450);

handlerTimer.postDelayed(new Runnable() {

public void run() {

t11.setText(“e “);

t11.setAnimation(AnimationUtils.loadAnimation(SplashScreen.this, android.R.anim.slide_in_left));

}

}, 1500);

handlerTimer.postDelayed(new Runnable() {

public void run() {

t12.setText(“D”);

t12.setAnimation(AnimationUtils.loadAnimation(SplashScreen.this, android.R.anim.slide_in_left));

}

}, 1550);

handlerTimer.postDelayed(new Runnable() {

public void run() {

t13.setText(“e”);

t13.setAnimation(AnimationUtils.loadAnimation(SplashScreen.this, android.R.anim.slide_in_left));

}

}, 1600);

handlerTimer.postDelayed(new Runnable() {

public void run() {

t14.setText(“v”);

t14.setAnimation(AnimationUtils.loadAnimation(SplashScreen.this, android.R.anim.slide_in_left));

}

}, 1650);

handlerTimer.postDelayed(new Runnable() {

public void run() {

t15.setText(“e”);

t15.setAnimation(AnimationUtils.loadAnimation(SplashScreen.this, android.R.anim.slide_in_left));

}

}, 1700);

handlerTimer.postDelayed(new Runnable() {

public void run() {

t16.setText(“l”);

t16.setAnimation(AnimationUtils.loadAnimation(SplashScreen.this, android.R.anim.slide_in_left));

}

}, 1750);

handlerTimer.postDelayed(new Runnable() {

public void run() {

t17.setText(“o”);

t17.setAnimation(AnimationUtils.loadAnimation(SplashScreen.this, android.R.anim.slide_in_left));

}

}, 1800);

handlerTimer.postDelayed(new Runnable() {

public void run() {

t18.setText(“p”);

t18.setAnimation(AnimationUtils.loadAnimation(SplashScreen.this, android.R.anim.slide_in_left));

}

}, 1850);

handlerTimer.postDelayed(new Runnable() {

public void run() {

t19.setText(“e”);

t19.setAnimation(AnimationUtils.loadAnimation(SplashScreen.this, android.R.anim.slide_in_left));

}

}, 1900);

handlerTimer.postDelayed(new Runnable() {

public void run() {

t20.setText(“r”);

t20.setAnimation(AnimationUtils.loadAnimation(SplashScreen.this, android.R.anim.slide_in_left));

}

}, 1950);

handlerTimer.postDelayed(new Runnable() {

public void run() {

}

}, 2000);

handlerTimer.postDelayed(new Runnable() {

public void run() {

pref = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

boolean passEnabled = pref.getBoolean(“passEnabled”, true);

if (passEnabled) {

Intent change = new Intent(SplashScreen.this, Auth.class);

startActivity(change);

} else if (!passEnabled) {

Intent change = new Intent(SplashScreen.this, Main.class);

startActivity(change);

}

finish();

}

}, 4000);

}

};

animated.start();

}

}

  1. Buat tampilan auth.xml seperti dibawah ini:

<?xml version=“1.0” encoding=“utf-8”?>

<RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android&#8221;

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:animateLayoutChanges=“true” >

<RelativeLayout

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:layout_centerHorizontal=“true”

android:animateLayoutChanges=“true”

android:padding=“10dp” >

<EditText

android:id=“@+id/password1”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:layout_below=“@+id/text”

android:drawableLeft=“@drawable/key”

android:ems=“10”

android:inputType=“textPassword”

android:layout_marginBottom=“5dp” >

</EditText>

<TextView

android:id=“@+id/text”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“Input your password” />

<ImageView

android:id=“@+id/ok”

android:layout_width=“wrap_content”

android:layout_height=“25dp”

android:layout_centerHorizontal=“true”

android:layout_below=“@+id/password1”

android:src=“@drawable/ok_b” />

</RelativeLayout>

</RelativeLayout>

  1. Buat activity Auth.java seperti dibawah ini:

package com.interactive.i_sms;

import android.app.Activity;

import android.content.Intent;

import android.database.Cursor;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuInflater;

import android.view.MenuItem;

import android.view.View;

import android.widget.EditText;

import android.widget.ImageView;

import android.widget.Toast;

public class Auth extends Activity {

private EditText pass;

private ImageView ok;

private DatabaseHandler db = new DatabaseHandler(this);

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.auth);

ok = (ImageView) findViewById(R.id.ok);

pass = (EditText) findViewById(R.id.password1);

ok.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Cursor get = db.getAll();

get.moveToFirst();

if (get.getCount() == 0) {

db.insert(pass.getText().toString());

Intent change = new Intent(Auth.this, Main.class);

startActivity(change);

finish();

} else {

if (pass.getText().toString().equals(db.getPassword(get))) {

Intent change = new Intent(Auth.this, Main.class);

startActivity(change);

finish();

} else {

Toast.makeText(Auth.this, “The password is incorrect !”,

Toast.LENGTH_LONG).show();

}

}

}

});

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

MenuInflater menuItem = getMenuInflater();

menuItem.inflate(R.menu.options_auth, menu);

return true;

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {

case R.id.exit:

finish();

break;

}

return false;

}

}

  1. Buat service ServiceRecv.java seperti dibawah ini:

package com.interactive.i_sms;

import android.app.Service;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.content.IntentFilter;

import android.os.Bundle;

import android.os.IBinder;

import android.telephony.SmsMessage;

import android.widget.Toast;

public class ServiceRecv extends Service {

private class SMSReceiver extends BroadcastReceiver {

@Override

public void onReceive(Context context, Intent intent) {

Bundle extras = intent.getExtras();

String strMessage = “”;

if (extras != null) {

Object[] smsextras = (Object[]) extras.get(“pdus”);

for (int i = 0; i < smsextras.length; i++) {

SmsMessage smsmsg = SmsMessage

.createFromPdu((byte[]) smsextras[i]);

String strMsgBody = smsmsg.getMessageBody().toString();

String strMsgSrc = smsmsg.getOriginatingAddress();

strMessage += “SMS from ” + strMsgSrc + ” : ” + strMsgBody;

}

Bundle send = new Bundle();

send.putString(“message”, strMessage);

Intent change = new Intent(ServiceRecv.this, Alert.class);

change.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

change.putExtras(send);

startActivity(change);

}

}

}

private SMSReceiver smsRecv;

private IntentFilter intentFilter;

@Override

public void onCreate() {

super.onCreate();

smsRecv = new SMSReceiver();

intentFilter = new IntentFilter();

intentFilter.addAction(“android.provider.Telephony.SMS_RECEIVED”);

registerReceiver(smsRecv, intentFilter);

}

@Override

public int onStartCommand(Intent intent, int flags, int startId) {

// TODO Auto-generated method stub

return Service.START_STICKY;

}

@Override

public void onDestroy() {

super.onDestroy();

unregisterReceiver(smsRecv);

}

@Override

public IBinder onBind(Intent intent) {

// TODO Auto-generated method stub

return null;

}

}

  1. Buat kelas DatabaseHandler.java untuk menghandle database seperti dibawah ini:

package com.interactive.i_sms;

import android.content.ContentValues;

import android.content.Context;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHandler extends SQLiteOpenHelper {

private static final String DATABASE_NAME = “0001.db”;

private static final int SCHEMA_VERSION = 6;

public DatabaseHandler(Context context) {

super(context, DATABASE_NAME, null, SCHEMA_VERSION);

}

@Override

public void onCreate(SQLiteDatabase db) {

db.execSQL(“CREATE TABLE PASSWORD (ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, PASSWORD VARCHAR NOT NULL)”);

}

@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

// TODO Auto-generated method stub

}

public void insert(String password) {

ContentValues carry = new ContentValues();

carry.put(“PASSWORD”, password);

getWritableDatabase().insert(“PASSWORD”, null, carry);

}

public void update(String password) {

ContentValues carry = new ContentValues();

carry.put(“PASSWORD”, password);

getWritableDatabase().update(“PASSWORD”, carry, null, null);

}

public Cursor getAll() {

return (getReadableDatabase().rawQuery(“SELECT * FROM PASSWORD”, null));

}

public String getPassword(Cursor c) {

return (c.getString(1));

}

}

  1. Buat tampilan halaman pengaturan prefs.xml seperti dibawah ini:

<?xml version=“1.0” encoding=“utf-8”?>

<PreferenceScreen

xmlns:android=http://schemas.android.com/apk/res/android&#8221; >

<CheckBoxPreference

android:title=“Password Authentication”

android:summaryOn=“Password is enabled”

android:summaryOff=“Password is disabled”

android:defaultValue=“true”

android:key=“passEnabled” />

<EditTextPreference

android:title=“Setup new password”

android:summary=“input your new password”

android:key=“newPass” />

</PreferenceScreen>

  1. Buat preference activity Prefs.java seperti dibawah ini:

package com.interactive.i_sms;

import android.content.SharedPreferences;

import android.database.Cursor;

import android.os.Bundle;

import android.preference.PreferenceActivity;

import android.preference.PreferenceManager;

public class Prefs extends PreferenceActivity{

private SharedPreferences pref;

DatabaseHandler db = new DatabaseHandler(this);

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

addPreferencesFromResource(R.xml.prefs);

pref = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

}

@Override

protected void onDestroy() {

super.onDestroy();

String newPass = pref.getString(“newPass”, “”);

Cursor get = db.getAll();

get.moveToFirst();

if (!db.getPassword(get).equals(newPass)) {

if (!newPass.equals(“”)) {

db.update(newPass);

}

}

}

}

  1. Buat tampilan main.xml seperti dibawah ini:

<?xml version=“1.0” encoding=“utf-8”?>

<RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android&#8221;

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:layout_margin=“10dp” >

<TextView

android:id=“@+id/passT”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_alignParentLeft=“true”

android:layout_alignParentTop=“true”

android:text=“Password : “ />

<TextView

android:id=“@+id/tes”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_toRightOf=“@+id/passT” />

<TextView

android:id=“@+id/tes1”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_alignParentLeft=“true”

android:layout_below=“@+id/passT”

android:text=“You will get alert when receive an sms message…” />

<TextView

android:id=“@+id/t1es”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_alignParentLeft=“true”

android:layout_below=“@+id/tes1”

android:text=“You may close this application” />

<ImageView

android:id=“@+id/end”

android:layout_width=“wrap_content”

android:layout_height=“25dp”

android:layout_centerHorizontal=“true”

android:layout_below=“@+id/t1es”

android:layout_marginTop=“5dp”

android:src=“@drawable/end_b” />

</RelativeLayout>

  1. Buat activity Main.java seperti dbawah ini:

package com.interactive.i_sms;

import android.app.Activity;

import android.content.Intent;

import android.content.SharedPreferences;

import android.os.Bundle;

import android.preference.PreferenceManager;

import android.view.Menu;

import android.view.MenuInflater;

import android.view.MenuItem;

import android.view.View;

import android.widget.ImageView;

import android.widget.TextView;

import android.widget.Toast;

public class Main extends Activity {

private SharedPreferences pref;

private TextView tes;

private ImageView end;

private Intent smsReceiver;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

smsReceiver = new Intent(this, ServiceRecv.class);

startService(smsReceiver);

pref = PreferenceManager.getDefaultSharedPreferences(getBaseContext());;

tes = (TextView) findViewById(R.id.tes);

end = (ImageView) findViewById(R.id.end);

end.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

stopService(smsReceiver);

Toast.makeText(Main.this, “SMS popup service has been stopped”, Toast.LENGTH_LONG).show();

}

});

}

@Override

protected void onResume() {

super.onResume();

if (pref.getBoolean(“passEnabled”, true)) {

tes.setText(“Enabled”);

} else if (!pref.getBoolean(“passEnabled”, true)) {

tes.setText(“Disabled”);

}

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

MenuInflater menuItem = getMenuInflater();

menuItem.inflate(R.menu.options_main, menu);

return true;

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

switch(item.getItemId()) {

case R.id.preferences :

Intent prefs = new Intent(Main.this, Prefs.class);

startActivity(prefs);

break;

case R.id.exit :

finish();

break;

}

return false;

}

}

Screenshot

Screenshot_2014-11-03-22-19-46 Screenshot_2014-11-03-21-32-03 Screenshot_2014-11-03-21-35-40

Screenshot_2014-11-03-21-35-48 Screenshot_2014-11-03-21-37-41

Membuat Aplikasi Android Sederhana Menggunakan Button, TextView, ImageView dan Animation

Posted on Updated on

  1. Buat splash_screen.xml untuk tampilan awal aplikasi seperti dibawah ini:

<?xml version=“1.0” encoding=“utf-8”?>

<RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android&#8221;

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:background=“#0844d5” >

<ImageView

android:id=“@+id/logoId”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_alignParentBottom=“true”

android:layout_alignParentLeft=“true”

android:layout_marginBottom=“5dp”

android:layout_marginLeft=“10dp”

android:src=“@raw/logoid” />

<ImageView

android:id=“@+id/logoSt”

android:layout_width=“170dp”

android:layout_height=“170dp”

android:layout_centerHorizontal=“true”

android:layout_centerVertical=“true”

android:src=“@raw/logo” />

</RelativeLayout>

  1. Buat SplashScreen.java seperti dibawah ini:

import android.support.v7.app.ActionBarActivity;

import android.content.Intent;

import android.os.Bundle;

import android.os.Handler;

import android.view.Menu;

import android.view.MenuItem;

import android.view.Window;

import android.view.animation.Animation;

import android.view.animation.AnimationUtils;

import android.widget.ImageView;

public class SplashScreen extends ActionBarActivity {

private Animation anim1, anim2;

private ImageView logoSt, logoId;

private Handler handlerTimer = new Handler();

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);

setContentView(R.layout.splash_screen);

anim1 = AnimationUtils.loadAnimation(this, R.anim.logo);

anim2 = AnimationUtils.loadAnimation(this, R.anim.logo);

Thread splash = new Thread() {

public void run() {

logoSt = (ImageView) findViewById(R.id.logoSt);

logoSt.setImageResource(R.drawable.ic_launcher);

logoSt.startAnimation(anim1);

handlerTimer.postDelayed(new Runnable() {

@Override

public void run() {

logoId = (ImageView) findViewById(R.id.logoId);

logoId.setImageResource(R.raw.logoid);

logoId.startAnimation(anim2);

}

}, 500);

try {

sleep(2000);

} catch (InterruptedException e) {

e.printStackTrace();

}

Intent menu = new Intent(“com.interactivedeveloper.simpletext.MAIN”);

startActivity(menu);

finish();

}

};

splash.start();

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.splash_screen, menu);

return true;

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

int id = item.getItemId();

if (id == R.id.action_settings) {

return true;

}

return super.onOptionsItemSelected(item);

}

}

 

  1. Buat tampilan main.xml seperti dibawah ini:

<?xml version=“1.0” encoding=“utf-8”?>

<RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android&#8221;

android:layout_width=“match_parent”

android:layout_height=“match_parent” >

<RelativeLayout

android:id=“@+id/relativeLayout1”

android:layout_width=“fill_parent”

android:layout_height=“wrap_content”

android:layout_below=“@+id/hijau”

android:layout_centerHorizontal=“true”

        android:gravity=“center” >

<ImageView

android:id=“@+id/show”

android:layout_width=“100dp”

android:layout_height=“30dp”

android:src=“@raw/show” />

<ImageView

android:id=“@+id/clear”

android:layout_width=“100dp”

android:layout_height=“30dp”

android:layout_toRightOf=“@+id/show”

android:src=“@raw/clear” />

</RelativeLayout>

<ImageView

android:id=“@+id/ungu”

android:layout_width=“50dp”

android:layout_height=“50dp”

android:layout_alignTop=“@+id/hijau”

android:layout_toLeftOf=“@+id/merah”

android:src=“@raw/ungu” />

<ImageView

android:id=“@+id/merah”

android:layout_width=“50dp”

android:layout_height=“50dp”

android:layout_alignTop=“@+id/hijau”

android:layout_marginLeft=“5dp”

android:layout_toLeftOf=“@+id/hijau”

android:src=“@raw/merah” />

<ImageView

android:id=“@+id/biru”

android:layout_width=“50dp”

android:layout_height=“50dp”

android:layout_alignTop=“@+id/hijau”

android:layout_marginRight=“5dp”

android:layout_toRightOf=“@+id/hijau”

android:src=“@raw/biru” />

<ImageView

android:id=“@+id/kuning”

android:layout_width=“50dp”

android:layout_height=“50dp”

android:layout_alignTop=“@+id/hijau”

android:layout_toRightOf=“@+id/biru”

android:src=“@raw/kuning” />

<ImageView

android:id=“@+id/hijau”

android:layout_width=“50dp”

android:layout_height=“50dp”

android:layout_centerHorizontal=“true”

android:layout_centerVertical=“true”

android:layout_margin=“5dp”

android:src=“@raw/hijau” />

<TextView

android:id=“@+id/text”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_above=“@+id/hijau”

android:layout_centerHorizontal=“true”

android:layout_margin=“10dp”

android:gravity=“center”

android:textSize=“18sp” />

<TextView

android:id=“@+id/hint”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_alignParentBottom=“true”

android:layout_centerHorizontal=“true”

android:layout_margin=“2dp”

android:gravity=“center”

android:text=“Tekan tomboh Show! untuk memasukkan text” />

</RelativeLayout>

  1. Buat Main.java seperti dibawah ini:

import android.app.AlertDialog;

import android.content.DialogInterface;

import android.graphics.Color;

import android.os.Bundle;

import android.support.v7.app.ActionBarActivity;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.widget.EditText;

import android.widget.ImageView;

import android.widget.TextView;

import android.widget.Toast;

public class Main extends ActionBarActivity {

private TextView text;

private EditText tinput;

private ImageView ungu, merah, hijau, biru, kuning, show, clear;

private boolean flag = false;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

init();

show.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

AlertDialog.Builder alert = new AlertDialog.Builder(Main.this);

alert.setTitle(“Input”);

alert.setMessage(“Masukkan text!”);

tinput = new EditText(Main.this);

alert.setView(tinput);

alert.setPositiveButton(“OK”, new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

text.setText(tinput.getText());

Toast.makeText(Main.this, “Text ditambahkan!”, Toast.LENGTH_LONG).show();

flag = true;

}

});

alert.setNegativeButton(“CANCEL”, new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog,int which) {

}

});

alert.show();

}

});

clear.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

text.setText(“”);

flag = false;

}

});

ungu.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

if (flag) {

text.setTextColor(Color.parseColor(“#d62395”));

} else {

Toast.makeText(Main.this, “Masukkan text terlebih dahulu!”, Toast.LENGTH_LONG).show();

}

}

});

merah.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

if (flag) {

text.setTextColor(Color.parseColor(“#d62323”));

} else {

Toast.makeText(Main.this, “Masukkan text terlebih dahulu!”, Toast.LENGTH_LONG).show();

}

}

});

hijau.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

if (flag) {

text.setTextColor(Color.parseColor(“#23d62c”));

} else {

Toast.makeText(Main.this, “Masukkan text terlebih dahulu!”, Toast.LENGTH_LONG).show();

}

}

});

biru.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

if (flag) {

text.setTextColor(Color.parseColor(“#2338d6”));

} else {

Toast.makeText(Main.this, “Masukkan text terlebih dahulu!”, Toast.LENGTH_LONG).show();

}

}

});

kuning.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

if (flag) {

text.setTextColor(Color.parseColor(“#faf826”));

} else {

Toast.makeText(Main.this, “Masukkan text terlebih dahulu!”, Toast.LENGTH_LONG).show();

}

}

});

}

private void init() {

text = (TextView) findViewById(R.id.text);

ungu = (ImageView) findViewById(R.id.ungu);

merah = (ImageView) findViewById(R.id.merah);

hijau = (ImageView) findViewById(R.id.hijau);

biru = (ImageView) findViewById(R.id.biru);

kuning = (ImageView) findViewById(R.id.kuning);

show = (ImageView) findViewById(R.id.show);

clear = (ImageView) findViewById(R.id.clear);

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.splash_screen, menu);

return true;

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

int id = item.getItemId();

if (id == R.id.action_settings) {

return true;

}

return super.onOptionsItemSelected(item);

}

}

 

  1. Berikut adalah isi file animasi logo.xml

<?xml version=“1.0” encoding=“utf-8”?>

<set xmlns:android=http://schemas.android.com/apk/res/android&#8221;

android:shareInterpolator=“false”

android:ordering=“together” >

<scale

android:fromXScale=“0.5”

android:toXScale=“1.0”

android:fromYScale=“0.5”

android:toYScale=“1.0”

android:pivotX=“50%”

android:pivotY=“50%”

android:fillAfter=“false”

android:duration=“1000”

android:interpolator=“@android:anim/decelerate_interpolator” />

<alpha

android:fromAlpha=“0.7”

android:toAlpha=“1.0”

android:duration=“1000”

android:interpolator=“@android:anim/decelerate_interpolator” />

</set>

 

  1. Berikut adalah isi file AndroidManifest.xml:

<?xml version=“1.0” encoding=“utf-8”?>

<manifest xmlns:android=http://schemas.android.com/apk/res/android&#8221;

package=“com.interactivedeveloper.simpletext”

android:versionCode=“1”

android:versionName=“1.0” >

<uses-sdk

android:minSdkVersion=“8”

android:targetSdkVersion=“19” />

<application

android:allowBackup=“true”

android:icon=“@drawable/ic_launcher”

android:label=“@string/app_name”

android:theme=“@style/AppTheme” >

<activity

android:name=“.SplashScreen”

android:label=“@string/app_name” >

<intent-filter>

<action android:name=“android.intent.action.MAIN” />

<category android:name=“android.intent.category.LAUNCHER” />

</intent-filter>

</activity>

<activity

android:name=“.Main”

android:label=“@string/app_name” >

<intent-filter>

<action android:name=“com.interactivedeveloper.simpletext.MAIN” />

<category android:name=“android.intent.category.DEFAULT” />

</intent-filter>

</activity>

</application>

</manifest>

 

Cara Mengubah String ke Interger Pemrograman Java

Posted on Updated on

Cara Mengubah String ke Interger Pemrograman Java – Kali ini admin akan share cara mengubah tipe data string ke interger dengan berbagai metode sebagai berikut.

Kita dapat mengubah tipe data String, misalnya hasil input dari konsol atau dari hasil bacaan file, ke tipe data bilangan untuk pengolahan lebih lanjut.

Sebagai contoh, kita akan membuat program untuk menghitung nilai investasi setelah n tahun. Input yang diminta dari user adalah :

  • Nama
  • Investasi awal (Rp)
  • Bunga (%)
  • Periode (tahun)

Kita bisa mengambil input tersebut menggunakan kelas BufferedReader seperti berikut.

nama = br.readLine();
strawal = br.readLine();
strbunga = br.readLine();
strperiode = br.readLine();

Tetapi karena keluaran dari readLine() bertipe String, kita harus mengubahnya menjadi bentuk bilangan agar dapat diproses lebih lanjut.

Untuk mengubah String menjadi int, kita dapat menggunakan kelas Java Integer yang di dalamnya memiliki fungsi parseInt(str). Fungsi ini dapat dipanggil dengan:

periode = Integer.parseInt(strperiode);

Sedangkan untuk mengubah String menjadi double, kita menggunakan kelas Java Double yang di dalamnya memiliki fungsi parseDouble(str). Fungsi ini dapat dipanggil dengan:

awal = Double.parseDouble(strawal);
bunga = Double.parseDouble(strbunga);

Setelah semua variabel didapat dan diubah, kita baru bisa untuk memulai perhitungan. Untuk menghitung bunga bank setelah n tahun, kita bisa menggunakan rumus berikut :

akhir = awal * (1 + bunga)periode

Dalam Java, rumus tersebut bisa dituliskan dengan ekspresi berikut

akhir = awal * Math.pow(1 + bunga,periode);
Sekian dari admin, semoga dapat membantu :)