Pages

Mempercantik List Android

Jika membuat List dalam android dengan menggunakan ArrayList dan ArrayAdaptor, dapat dipercantik dengan memberi warna, hal ini tentu untuk membuat applikasi yang kita buat semakin menarik.

Pada contoh kasus ini yang perlu diedit hanya class java saja, untuk main.xml dan androidmanifest.xml, tidak perlu dilakukan perubahanUntuk membuat list yang cantik dalam android, dapat mengikuti langkah-langkah berikut :



  1. Buka Eclipse
  2. Pada Eclipse File - New - Project - Android - Android Project - Next
  3. Kemudian pada jendela New Android Project isikan field berikut :
    1. Project Name : Arraylist Adapter
    2. Build Target : Android 2.2
    3. Aplication name : Arraylist Adapter
    4. Package Name : jhohannes.purba
    5. Create Activity : ArraylistAdapterActivity
    6. Kemudian klik Finish  
  4. Buka ArraylistAdapterActivity.java kemudian ketikkan source code di bawah ini :
    1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    package jhohannes.purba;
    
    import java.util.ArrayList;
    
    import android.app.ListActivity;
    import android.graphics.Color;
    import android.os.Bundle;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class ArraylistAdapterActivity extends ListActivity {
     @Override
     public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            
    //        setContentView(R.layout.main);
            
         ArrayList<String> codingSederhana = new ArrayList<String>();        
            codingSederhana.add("Java");
            codingSederhana.add("Android");
            codingSederhana.add("PHP");
            codingSederhana.add("C++");
            codingSederhana.add("MySql");
            codingSederhana.add("PostgreSql");
            codingSederhana.add("Netbeans");
            codingSederhana.add("Eclipse");
            codingSederhana.add("Ubuntu");
            
            
            ArrayAdapter<String> adapterCodingSederhana= new ArrayAdapter<String>(ArraylistAdapterActivity.this, android.R.layout.simple_list_item_1, codingSederhana){
             @Override
             public View getView(int position, View convertView, ViewGroup parent) {
              View view = super.getView(position, convertView, parent);
               if (position % 2 == 0){
          view.setBackgroundColor(0xff00DD00); // warna background
          ((TextView) view).setTextColor(Color.WHITE); // warna teks     
          ((TextView) view).setTextSize(16.0f);
               } else {
          view.setBackgroundColor(Color.LTGRAY); // warna background
          ((TextView) view).setTextColor(Color.BLUE); // warna teks
          ((TextView) view).setTextSize(16.0f);
         }
              
              return view;
             }         
            };
            
            this.setListAdapter(adapterCodingSederhana);
        }
     
     @Override
     protected void onListItemClick(ListView l, View v, int position, long id) {
      // TODO Auto-generated method stub
      super.onListItemClick(l, v, position, id);
      
      Object pilihan= this.getListAdapter().getItem(position);
      Toast.makeText(ArraylistAdapterActivity.this, "Anda Memilih : " + pilihan, Toast.LENGTH_LONG).show();
     }
    }
Perhatikan pada baris 15, yang digunakan adalahListActivity bukan Activity. kemudian pada baris 20, setContentView(R.layout.main); 

Jika dijalankan dan di klik salah satu, maka akan kelihatan hasilnya seperti gambar di bawah ini :

Jhohannes H Purba Coding Sederhana September 29, 2012

6 comments:

  1. Amazing blog and very interesting stuff you got here! I definitely learned a lot from reading through some of your earlier posts as well and decided to drop a comment on this one!

    ReplyDelete
  2. Hi, Really great effort. Everyone must read this article. Thanks for sharing.

    ReplyDelete
  3. Hey keep posting such good and meaningful articles.

    ReplyDelete
  4. Amazing blog and very interesting stuff you got here! I definitely learned a lot from reading through some of your earlier posts as well and decided to drop a comment on this one!

    ReplyDelete
  5. Very informative, keep posting such good articles, it really helps to know about things.

    ReplyDelete
  6. Hey keep posting such good and meaningful articles.

    ReplyDelete