Change CheckBox Background Color
Agar tampilan checkbox tidak monoton dengan warna default hitam, kita bisa mengubah sendiri warna backgroundnya seperti gambar dibawah ini 
Untuk
membuat seperti di atas, pertama kita harus membuat resource warna
untuk backgroundnya.
Caranya dengan buat file xml di res /
values dengan nama colors.xml
|
<resources> <color name="backgroundColor">#FF227A81</color> </resources> |
Untuk
desaign di file xml nya seperti ini
|
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent" android:orientation="vertical" android:layout_width="fill_parent"> <CheckBox android:id="@+id/A.N.JELL" android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="A.N.JELL"
android:layout_marginBottom="2dp"
android:layout_marginTop="2dp"/>
<CheckBox android:id="@+id/Jang" android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="Jang
Geun Suk" android:layout_marginBottom="2dp"
android:layout_marginTop="2dp"/>
<CheckBox android:id="@+id/CN" android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="CN
Blue" android:layout_marginBottom="2dp"
android:layout_marginTop="2dp"/>
<CheckBox android:id="@+id/FT" android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="FT
Island" android:layout_marginBottom="2dp"
android:layout_marginTop="2dp"/>
<CheckBox android:id="@+id/Simple" android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="Simple
Plan" android:layout_marginBottom="2dp"
android:layout_marginTop="2dp"/>
<CheckBox android:id="@+id/Secondhand" android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="Secondhand
Serenade" android:layout_marginBottom="2dp"
android:layout_marginTop="2dp"/>
</LinearLayout> |
Masuk
ke kelas Activity yang disini saya beri nama CheckboxMain.java
untuk mendeklarasikan
OnCheckedChangeListener.
|
private OnCheckedChangeListener m_BasicCheckListener; |
Kemudian
membuat method OnCheckChangeListener() untuk mengubah background
warna biru ketika checkbox di centang, dan kembali ke warna defaullt
jika kotak tidak di centang.
|
m_BasicCheckListener = new OnCheckedChangeListener(){ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked){ buttonView.setBackgroundResource(R.color.backgroundColor); } else { buttonView.setBackgroundColor(android.R.drawable.screen_background_dark); } } }; |
Untuk
membuat checkbox group maka, anda dapat menggunakan Array seperti
source code berikut :
|
CheckBox[] setOfCheckBoxes = new CheckBox[] {(CheckBox)findViewById(R.id.A_N_JELL), (CheckBox)findViewById(R.id.CN), (CheckBox)findViewById(R.id.FT), (CheckBox)findViewById(R.id.Jang), (CheckBox)findViewById(R.id.Simple), (CheckBox)findViewById(R.id.Secondhand)};
for(int i = 0; i < setOfCheckBoxes.length; i++){ setOfCheckBoxes[i].setOnCheckedChangeListener(m_BasicCheckListener); |
Code lengkapnya di CheckboxMain.java
|
package com.android.changeBackgroundCheckbox; import android.app.Activity; import android.os.Bundle; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener;
public class CheckboxMain extends Activity { /** Called when the activity is first created. */
private OnCheckedChangeListener m_BasicCheckListener;
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.check_list);
CheckBox[] setOfCheckBoxes = new CheckBox[] {(CheckBox)findViewById(R.id.A_N_JELL), (CheckBox)findViewById(R.id.CN), (CheckBox)findViewById(R.id.FT), (CheckBox)findViewById(R.id.Jang), (CheckBox)findViewById(R.id.Simple), (CheckBox)findViewById(R.id.Secondhand)};
m_BasicCheckListener = new OnCheckedChangeListener(){ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked){ buttonView.setBackgroundResource(R.color.backgroundColor); } else { buttonView.setBackgroundColor(android.R.drawable.screen_background_dark); } } };
for(int i = 0; i < setOfCheckBoxes.length; i++){ setOfCheckBoxes[i].setOnCheckedChangeListener(m_BasicCheckListener); } } } |
Posted at 08:50AM May 13, 2010 by nety ozora in General | Comments[1]
nice
Posted by Latief on May 26, 2011 at 12:39 AM WIT #