This is default featured slide 1 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by Lasantha Bandara - Premiumbloggertemplates.com.

This is default featured slide 2 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by Lasantha Bandara - Premiumbloggertemplates.com.

This is default featured slide 3 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by Lasantha Bandara - Premiumbloggertemplates.com.

This is default featured slide 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by Lasantha Bandara - Premiumbloggertemplates.com.

This is default featured slide 5 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by Lasantha Bandara - Premiumbloggertemplates.com.

Kamis, 15 Juni 2017

Cara Membuat Http Connection pada Android


Cara Membuat Http Connection pada Android


Pada projeck ini kita akan menggunakan 1 file.xml dan 1 file.java


-main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scrollbars="vertical">
    <Button 
android:id="@+id/Button01" 
android:text="@string/button01" 
android:layout_height="wrap_content" 
android:layout_width="wrap_content">
</Button>
<Button android:id="@+id/Button02" 
android:layout_height="wrap_content" 
android:text="@string/button02" 
android:layout_width="wrap_content">
</Button>
<ImageView 
android:id="@+id/imageview01" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:cropToPadding="true" >
</ImageView>
<TextView  
    android:id="@+id/textview01"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" />
</LinearLayout>


-HttpURLConnectionA.java

package com.bogotobogo.httpconnecta;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class HttpURLConnectionA extends Activity {
    
private ProgressDialog progressDialog;
private Bitmap bitmap = null;
private String text = null;

/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button imageBtn = (Button)findViewById(R.id.Button01);
        Button textBtn = (Button)findViewById(R.id.Button02);
        
        imageBtn.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
downloadImage("http://www.bogotobogo.com/images/smalltiger.gif");
}
        });
        
        textBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
downloadText("http://www.bogotobogo.com/android.html");
}
        });
    }
    
private void downloadImage(String urlStr) {
progressDialog = ProgressDialog.show(this, "", 
"Downloading Image from " + urlStr);
final String url = urlStr;

new Thread() {
public void run() {
InputStream in = null;
Message msg = Message.obtain();
msg.what = 1;
try {
    in = openHttpConnection(url);
    bitmap = BitmapFactory.decodeStream(in);
    Bundle b = new Bundle();
    b.putParcelable("bitmap", bitmap);
    msg.setData(b);
    in.close();
} catch (IOException e1) {
    e1.printStackTrace();
}
messageHandler.sendMessage(msg);
}
  }.start();
}

private void downloadText(String urlStr) {
progressDialog = ProgressDialog.show(this, "", 
"Download Text from " + urlStr);
final String url = urlStr;

new Thread () {
public void run() {
int BUFFER_SIZE = 2000;
        InputStream in = null;
        Message msg = Message.obtain();
        msg.what=2;
        try {
         in = openHttpConnection(url);
            
            InputStreamReader isr = new InputStreamReader(in);
            int charRead;
              text = "";
              char[] inputBuffer = new char[BUFFER_SIZE];

                  while ((charRead = isr.read(inputBuffer))>0)
                  {                    
                      String readString = 
                          String.copyValueOf(inputBuffer, 0, charRead);                    
                      text += readString;
                      inputBuffer = new char[BUFFER_SIZE];
                  }
                 Bundle b = new Bundle();
    b.putString("text", text);
    msg.setData(b);
                  in.close();
                  
}catch (IOException e2) {
                e2.printStackTrace();
            }
messageHandler.sendMessage(msg);
}
}.start();    
}

private InputStream openHttpConnection(String urlStr) {
InputStream in = null;
int resCode = -1;

try {
URL url = new URL(urlStr);
URLConnection urlConn = url.openConnection();

if (!(urlConn instanceof HttpURLConnection)) {
throw new IOException ("URL is not an Http URL");
}

HttpURLConnection httpConn = (HttpURLConnection)urlConn;
httpConn.setAllowUserInteraction(false);
            httpConn.setInstanceFollowRedirects(true);
            httpConn.setRequestMethod("GET");
            httpConn.connect(); 

            resCode = httpConn.getResponseCode();                 
            if (resCode == HttpURLConnection.HTTP_OK) {
                in = httpConn.getInputStream();                                 
            }         
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return in;
}

private Handler messageHandler = new Handler() {

public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 1:
ImageView img = (ImageView) findViewById(R.id.imageview01);
img.setImageBitmap((Bitmap)(msg.getData().getParcelable("bitmap")));
break;
case 2:
TextView text = (TextView) findViewById(R.id.textview01);
text.setText(msg.getData().getString("text"));
break;
}
progressDialog.dismiss();
}
};
}

Hasil :



Cara Membuat Kuis pada Android dengan Eclipse

Cara Membuat Kuis Tanya Jawab pada Android dengan Eclipse



Jadi gambaran aplikasi ini yaitu si user atau pengguna aplikasi ini akan di tanyakan sebuah pertanyaan dan pertanyaan itu di jawab dengan benar dengan cara di centang. Jika ingin mengetahui cara menggunakan aplikasi ini dan mengetahui alur programnya ikuti coding dibawah ini yaitu coding untuk main.xml dan Appkuis.java. Ingat jangan lupa buat project baru yaaa… setelah itu tambahkan coding di bawah ini :
main.xml
<RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android&#8221;
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:paddingBottom=“@dimen/activity_vertical_margin”
android:paddingLeft=“@dimen/activity_horizontal_margin”
android:paddingRight=“@dimen/activity_horizontal_margin”
android:paddingTop=“@dimen/activity_vertical_margin”
tools:context=“.Main”
android:orientation=“vertical”

android:background=“@drawable/aa”>
<TextView
android:id=“@+id/textView1”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:textSize=“16sp”
        android:text=“1.Manahkah kota dibawah ini yang merupakan ibu kota provinsi?” />

<CheckBox
android:id=“@+id/cbandung”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_alignLeft=“@+id/textView1”
android:layout_below=“@+id/textView1”
android:layout_marginTop=“40dp”
android:text=“Bandung” />

<CheckBox
android:id=“@+id/cbogor”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_alignLeft=“@+id/cbandung”
android:layout_below=“@+id/cbandung”
android:text=“Bogor” />

<CheckBox
android:id=“@+id/cbanjarmasin”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_alignLeft=“@+id/cbogor”
android:layout_below=“@+id/cbogor”
android:text=“BanjarMasin” />

<CheckBox
android:id=“@+id/cbontang”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_alignLeft=“@+id/cbanjarmasin”
android:layout_below=“@+id/cbanjarmasin”
android:text=“Bontang” />

<Button
android:id=“@+id/proses”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_alignLeft=“@+id/cbontang”
android:layout_alignRight=“@+id/cbanjarmasin”
android:layout_below=“@+id/cbontang”
android:layout_marginTop=“17dp”
android:text=“Lihat Nilai” />

<TextView
android:id=“@+id/tnilai”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_alignLeft=“@+id/proses”
android:layout_below=“@+id/proses”
android:layout_marginTop=“30dp”
android:text=“Nilai Anda adalah :”
android:textAppearance=“?android:attr/textAppearanceLarge” />

</RelativeLayout>


Appkuis.java
package putra.example.kuis;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

public class AppKuis extends Activity implements OnClickListener {
Button proses;
CheckBox cekbandung, cekbogor, cekbanjar, cekbontang;
TextView outputnilai;
String nilai;

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

proses = (Button) findViewById(R.id.proses);

cekbandung = (CheckBox) findViewById(R.id.cbandung);
cekbogor = (CheckBox) findViewById(R.id.cbogor);
cekbanjar = (CheckBox) findViewById(R.id.cbanjarmasin);
cekbontang = (CheckBox) findViewById(R.id.cbontang);
outputnilai = (TextView) findViewById(R.id.tnilai);

proses.setOnClickListener(this);

}

@Override
public void onClick(View klik) {
// TODO Auto-generated method stub

if (cekbandung.isChecked() && cekbanjar.isChecked()
&& cekbogor.isChecked() && cekbontang.isChecked()) {
outputnilai.setText(“10”);

} else if (cekbandung.isChecked() && cekbanjar.isChecked()
&& cekbogor.isChecked()) {
outputnilai.setText(“15”);

} else if (cekbandung.isChecked() && cekbanjar.isChecked()
&& cekbontang.isChecked()) {
outputnilai.setText(“15”);

} else if (cekbanjar.isChecked() && cekbogor.isChecked()
&& cekbontang.isChecked()) {
outputnilai.setText(“0”);

} else if (cekbandung.isChecked() && cekbanjar.isChecked()) {
outputnilai.setText(“20”);

} else if (cekbandung.isChecked() && cekbogor.isChecked()) {
outputnilai.setText(“5”);

} else if (cekbandung.isChecked() && cekbontang.isChecked()) {
outputnilai.setText(“5”);

} else if (cekbogor.isChecked() && cekbanjar.isChecked()) {
outputnilai.setText(“5”);

} else if (cekbontang.isChecked() && cekbanjar.isChecked()) {
outputnilai.setText(“5”);
} else if (cekbontang.isChecked() && cekbogor.isChecked()) {
outputnilai.setText(“-10”);

} else if (cekbandung.isChecked()) {
outputnilai.setText(“10”);

} else if (cekbanjar.isChecked()) {
outputnilai.setText(“10”);

} else if (cekbogor.isChecked()) {
outputnilai.setText(“-5”);

} else if (cekbontang.isChecked()) {
outputnilai.setText(“-5”);

} else {
outputnilai.setText(“0”);
}

cekbandung.setChecked(false);
cekbanjar.setChecked(false);
cekbogor.setChecked(false);
cekbontang.setChecked(false);
}

}

Membuat MP3 Player pada Andoid

  • Siang ini saya akan berbagi tentang cara membuat aplikasi pemutar musik dengan menggunakan Eclipse. Aplikasi yang saya buat merupakan aplikasi sederhana yang saya pelajari melalui internet. Ikuti langkah-langkah dibawah ini. Selamat belajar ðŸ™‚


  • Buatlah sebuah project baru dengan nama “MusicPlayer“.
  • Siapkan 3 buah gambar yang nantinya akan digunakan untuk dijadikan gambar pada button dan satu buah atau beberapa lagu yang akan dijalankan nanti. Berikut gambar gambar yang dibutuhkan.

  • Klik kanan pada folder res > New >  Folder > Beri nama “drawable”. Kemudian buat folder baru lagi dengan nama “raw” yang nantinya akan digunakan untuk menyimpan lagu yang akan kita jalankan.
  • Copy dan paste 3 buah gambar tadi ke dalam folder drawable, dan copy paste file lagu ke dalam folder raw.
  • Bukalah file activity_main.xml dan susunlah komponen android menjadi seperti dibawah ini.


  • DIbawah ini merupakan program dari activity_main.xml.
<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
       android:id="@+id/textView1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentTop="true"
       android:layout_centerHorizontal="true"
       android:layout_marginTop="24dp"
       android:text="@string/judul"
       android:textSize="20sp" />

    <ImageButton
       android:id="@+id/stop"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignTop="@+id/play"
       android:layout_marginLeft="22dp"
       android:layout_toRightOf="@+id/play"
       android:src="@drawable/stop1" />

    <ImageButton
       android:id="@+id/play"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignTop="@+id/pause"
       android:layout_centerHorizontal="true"
       android:src="@drawable/play1" />

    <ImageButton
       android:id="@+id/pause"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_below="@+id/textView1"
       android:layout_marginRight="18dp"
       android:layout_marginTop="112dp"
       android:layout_toLeftOf="@+id/play"
       android:src="@drawable/pause1" />
</RelativeLayout>
  • Kemudian bukalah file MainActivity.java dan salinlah program dibawah ini. Jangan lupa untuk tetap menyesuaikan program dibawah ini dengan program yang anda buat.

package com.irmarismay.musicplayer;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;

public class MainActivity extends Activity implements OnCompletionListener {
    ImageButton pause, play, stop;
    MediaPlayer mp;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    pause = (ImageButton)findViewById(R.id.pause);
    play = (ImageButton)findViewById(R.id.play);
    stop = (ImageButton)findViewById(R.id.stop);

    pause.setOnClickListener(new View.OnClickListener() {
    @Override
        public void onClick(View v) {
           // TODO Auto-generated method stub
           pause();
        }
    });
  
    play.setOnClickListener(new View.OnClickListener() {
    @Override
       public void onClick(View v) {
       // TODO Auto-generated method stub
          play();
       }
    });

    stop.setOnClickListener(new View.OnClickListener() {
    @Override
       public void onClick(View v) {
       // TODO Auto-generated method stub
          stop();
       }
    });

    setup();
}

@Override public void onDestroy() {
   super.onDestroy();
   if (stop.isEnabled()){
      stop();
   }
}

public void onCompletion(MediaPlayer mp) {
   stop();
}

private void play() {
   mp.start();
   play.setEnabled(false);
   pause.setEnabled(true);
   stop.setEnabled(true);
}

private void stop(){
   mp.stop();
   pause.setEnabled(false);
   stop.setEnabled(false);
   try { mp.prepare();
      mp.seekTo(0);
      play.setEnabled(true);
   } catch (Throwable t) {
      goBlooey(t);
   }
}

private void pause() {
   mp.pause();
   play.setEnabled(true);
   pause.setEnabled(false);
   stop.setEnabled(true);
}

private void loadClip() {
   try { mp=MediaPlayer.create(this, R.raw.start);
      mp.setOnCompletionListener(this);
   } catch (Throwable t) {
      goBlooey(t);
   }
}

private void setup() {
   loadClip();
   play.setEnabled(true);
   pause.setEnabled(false);
   stop.setEnabled(false);
}

private void goBlooey(Throwable t) {
   AlertDialog.Builder builder=new AlertDialog.Builder(this);
   builder .setTitle("Exception!") .setMessage(t.toString()) .setPositiveButton("OK", null) .show();
}
  • Setelah selesai, jalankan aplikasi pada emulator maupun pada Smartphone anda.
  • Berikut tampilannya.