Sunday 7 April 2013

Lanjutt lagi kawan..

Pada episode kali ini, kita akan membahas mengenai pembuatan multiple-choice list. Caranya hampir sama dengan membuat single-choice list (radio buttons), bedanya disini kita akan menggunakan method setMultiChoiceItems().

Langsung saja ikuti langkah-langkahnya.
  1. Buka eclipse.
  2. Buka project yang telah kita buat pada postingan sebelumnya.
  3. Buka activity_main.xml dan tambahkan kode berikut.
    <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"
        tools:context=".MainActivity" >
    
        <Button
            android:id="@+id/btnNeutral"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="14dp"
            android:text="Dialog Neutral"
            tools:ignore="HardcodedText" />
    
        <Button
            android:id="@+id/btnList"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/btnNeutral"
            android:text="Dialog Items"
            tools:ignore="HardcodedText" />
    
        <Button
            android:id="@+id/btnRadioList"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/btnList"
            android:text="Dialog Radio List"
            tools:ignore="HardcodedText" />
    
        <Button
            android:id="@+id/btnChekBox"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/btnRadioList"
            android:text="Dialog ChekBox List"
            tools:ignore="HardcodedText" />
    
        <Button
            android:id="@+id/btnKeluar"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/btnCustom"
            android:text="Keluar"
            tools:ignore="HardcodedText" />
    
    </RelativeLayout>
  4. Selanjutnya buka MainActivity.java , deklarasikan atribut dengan tipe button dan buat juga sebuah arraylist untuk menampung inputan yang nantinya akan dipilih melalui dialog.
    Button buttonChekBoxList;
    
    ArrayList<Integer> arrayList=new ArrayList<Integer>();

  5. Deklarasikan pula button tadi pada method onCreate(Bundle savedInstanceState).
    buttonChekBoxList=(Button)findViewById(R.id.btnChekBox);

  6. Buat method untuk menampung dialog yang aakan kita buat.
     public void actionChekBoxList(){
         
         AlertDialog.Builder builder=new AlertDialog.Builder(context);
         builder.setTitle("CheckBox List");
         builder.setMultiChoiceItems(items, null, new DialogInterface.OnMultiChoiceClickListener() {
       
       @Override
       public void onClick(DialogInterface dialog, int which, boolean isChecked) {
        // TODO Auto-generated method stub
        if(isChecked){
         arrayList.add(which);
        }else{
         arrayList.remove(Integer.valueOf(which));
        }
       }
      });
         
         builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
       
       @Override
       public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
        String sequence="";
        
        for(int i=0;i<arrayList.size();i++){
         int x=arrayList.get(i);
         sequence +=items[x] + "\n";
        }
        
        Toast.makeText(context,sequence, Toast.LENGTH_LONG).show();
        
        arrayList.clear();
        
       }
      });
         
         builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
       
       @Override
       public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
        
       }
      });
         
         AlertDialog alertDialog=builder.create();
         alertDialog.show();
        }

  7. Untuk lebih jelasnya berikut kode lengkap MainActivity.java.
    package com.is.dialogtest;
    
    import java.util.ArrayList;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
    
    
     Context context=this;
     Button buttonNeutral;
     Button buttonKeluar;
     Button buttonList;
     Button buttonRadioList;
     Button buttonChekBoxList;
     
     
     ArrayList<Integer> arrayList=new ArrayList<Integer>();
     CharSequence items[]={"Ubuntu","Mandriva","Mint"};
     
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            buttonNeutral=(Button) findViewById(R.id.btnNeutral);
            buttonList=(Button) findViewById(R.id.btnList);
            buttonRadioList=(Button) findViewById(R.id.btnRadioList);
            buttonChekBoxList=(Button)findViewById(R.id.btnChekBox);
            buttonKeluar=(Button) findViewById(R.id.btnKeluar);
            
            buttonNeutral.setOnClickListener(new OnClickListener() {
       
       @Override
       public void onClick(View arg0) {
        // TODO Auto-generated method stub
        actionNeutralButton();
       }
      });
            
            buttonList.setOnClickListener(new OnClickListener() {
       
            @Override
            public void onClick(View arg0) {
             // TODO Auto-generated method stub
             actionDialogList();
            }
           });
            
            buttonRadioList.setOnClickListener(new OnClickListener() {
       
       @Override
       public void onClick(View arg0) {
        // TODO Auto-generated method stub
        actionRadioList();
       }
      });
            
            buttonChekBoxList.setOnClickListener(new OnClickListener() {
       
       @Override
       public void onClick(View arg0) {
        // TODO Auto-generated method stub
        actionChekBoxList();
       }
      });
                   
            buttonKeluar.setOnClickListener(new OnClickListener() {
       
       @Override
       public void onClick(View arg0) {
        // TODO Auto-generated method stub
        actionKeluar();
       }
      });
            
        }
        
        public void actionKeluar(){
         AlertDialog.Builder builder=new AlertDialog.Builder(context);
      builder.setTitle("Konfirmasi");
      builder.setMessage("Apakah Anda Akan Keluar ?");
      builder.setCancelable(false);
      builder.setPositiveButton("Ya", new DialogInterface.OnClickListener() {
       
       @Override
       public void onClick(DialogInterface arg0, int arg1) {
        // TODO Auto-generated method stub
        MainActivity.this.finish();
       }
      });
      
      builder.setNegativeButton("Tidak", new DialogInterface.OnClickListener() {
       
       @Override
       public void onClick(DialogInterface arg0, int arg1) {
        // TODO Auto-generated method stub
        arg0.cancel();
       }
      });
      
      AlertDialog alertDialog=builder.create();
      alertDialog.show();
        }
        
        public void actionNeutralButton(){
         new AlertDialog.Builder(this).setTitle("Ini Title")
          .setMessage("Ini Message")
          .setNeutralButton("Oke", new DialogInterface.OnClickListener() {
       
       @Override
       public void onClick(DialogInterface arg0, int arg1) {
        // TODO Auto-generated method stub
        //Tidak melakukan apapun
       }
      }).show();
        }
        
        public void actionDialogList(){
         AlertDialog.Builder builder=new AlertDialog.Builder(context);
         builder.setTitle("Distro Linux");
         builder.setItems(items, new DialogInterface.OnClickListener() {
       
       @Override
       public void onClick(DialogInterface arg0, int arg1) {
        // TODO Auto-generated method stub
        Toast.makeText(context, items[arg1], Toast.LENGTH_LONG).show();
       }
      });
         
         AlertDialog alertDialog=builder.create();
         alertDialog.show();
        }
    
        public void actionRadioList(){
         AlertDialog.Builder builder=new AlertDialog.Builder(context);
         builder.setTitle("Radio Items");
         builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
       
       @Override
       public void onClick(DialogInterface arg0, int arg1) {
        // TODO Auto-generated method stub
        Toast.makeText(context, items[arg1], Toast.LENGTH_LONG).show();
        arg0.dismiss();
       }
      });
         
         AlertDialog alertDialog=builder.create();
         alertDialog.show();
        }
        
        public void actionChekBoxList(){
         
         AlertDialog.Builder builder=new AlertDialog.Builder(context);
         builder.setTitle("CheckBox List");
         builder.setMultiChoiceItems(items, null, new DialogInterface.OnMultiChoiceClickListener() {
       
       @Override
       public void onClick(DialogInterface dialog, int which, boolean isChecked) {
        // TODO Auto-generated method stub
        if(isChecked){
         arrayList.add(which);
        }else{
         arrayList.remove(Integer.valueOf(which));
        }
       }
      });
         
         builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
       
       @Override
       public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
        String sequence="";
        
        for(int i=0;i<arrayList.size();i++){
         int x=arrayList.get(i);
         sequence +=items[x] + "\n";
        }
        
        Toast.makeText(context,sequence, Toast.LENGTH_LONG).show();
        
        arrayList.clear();
        
       }
      });
         
         builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
       
       @Override
       public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
        
       }
      });
         
         AlertDialog alertDialog=builder.create();
         alertDialog.show();
        }
           
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }
    }

  8. Jalankan aplikasi kawan-kawan
    4-7-2013 7-22-42 AM4-7-2013 7-23-09 AM

Nah, sekarang kita telah berhasil membuat dialog sederhanasingle-choice listsingle-choice list (radio buttons) dan multiple-choice list (checkboxes). Pada episode yang akan datang kita akan membahas bagaimana membuat custom dialog.


Bersambung ...

Posted by Unknown On 01:03 3 comments

3 comments:

  1. Howdy very cool website!! Guy .. Beautiful .. Wonderful .
    . I will bookmark your website and take the
    feeds additionally? I'm satisfied to search out numerous helpful info right here within the publish, we'd like develop more techniques on this regard,
    thank you for sharing. . . . . .

    ReplyDelete
  2. If some one desires to be updated with most up-to-date technologies therefore
    he must be pay a quick visit this web site and be up to date every day.

    ReplyDelete
  3. I read this paragraph completely concerning the
    difference of newest and preceding technologies, it's remarkable article.

    ReplyDelete

  • RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
  • Youtube

Recent Post

Entri Populer

Total Pageviews

Visitor



Flag Counter

    About

    Orang yang berilmu mengetahui orang yang bodoh karena dia pernah bodoh, sedangkan orang yang bodoh tidak mengetahui orang yang berilmu karena dia tidak pernah berilmu.