On Eclipse and start a new Android project. Create a folder called /xml in the /res directory. Add three XML-based resources to the /res/xml folder, two that represent the drawing code for the actual checkbox and a third that is the state selector.
checked.xml
checked.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android=http://schemas.android.com/apk/res/android
android:shape="rectangle">
<gradient android:startColor="#ffff0000" android:endColor="#ff000000" android:angle="270"/>
<stroke android:width="4px" android:color="#ffc0c0c0" />
<size android:height="20dp" android:width="20dp"/>
</shape>
Unchecked.xml
<?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <gradient android:startColor="#ff585858" android:endColor="#ff000000" android:angle="270"/> <stroke android:width="4px" android:color="#ffc0c0c0" /> <size android:height="20dp" android:width="20dp"/> </shape>
custom_checkbox.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true" android:drawable="@xml/checked" /> <item android:state_pressed="true" android:drawable="@xml/checked" /> <item android:drawable="@xml/unchecked" /> </selector>
main.xml<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="20dip" android:gravity="left" android:padding="10dip" android:text="Check It!" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="16dip" android:gravity="left" android:padding="10dip" android:text="Here is a checkbox..." /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="left" android:orientation="horizontal"> <CheckBox android:id="@+id/checkbox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:button="@xml/custom_checkbox"/> <TextView android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="14dip" android:gravity="center" android:padding="10dip" android:text="(unchecked)" /> </LinearLayout> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="16dip" android:gravity="left" android:padding="10dip" android:text="And here is another..." /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="left" android:orientation="horizontal"> <CheckBox android:id="@+id/checkbox2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:button="@xml/custom_checkbox"/> <TextView android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="14dip" android:gravity="center" android:padding="10dip" android:text="(unchecked)" /> </LinearLayout> </LinearLayout>
Main.java
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.CheckBox; import android.widget.TextView; public class Main extends Activity implements OnClickListener { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findViewById(R.id.checkbox1).setOnClickListener(this); findViewById(R.id.checkbox2).setOnClickListener(this); } public void onClick(View v) { TextView tv; if (v.getId()==R.id.checkbox1) { tv = (TextView)findViewById(R.id.textview1); } else { tv = (TextView)findViewById(R.id.textview2); } if (((CheckBox)v).isChecked()) { tv.setText("(checked)"); } else { tv.setText("(unchecked)"); } } }
No comments:
Post a Comment