'Why checkbox of android is so slow?
Today, I used an Android machine with poor performance. I found the checkbox loading is very slow. For example:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
long t1 = System.currentTimeMillis();
setContentView(R.layout.activity_main);
System.out.println("main cost: " + (System.currentTimeMillis() - t1));
}
}
There is only one Checkbox in the page, this will cost 200ms. If there are 3 or 5 checkbox in activity_main.xml, will cost 1s. What happend?
I compared Switch to Checkbox. Obviously Switch is very very better than Checkbox. If i want to keep to use Checkbox, what should i do?
Solution 1:[1]
Why does it happen ?
This is happening because the Checkbox class is very complicated(too much code ?).
What is the solution ?
- You can create a custom layout.
- You can use a library.
How to implement ?
1.Custom layout
- First create a
LinearLayoutwithhorizontalorientation. - Add an
ImageViewand aTextViewin the xml. - Code reference
<LinearLayout
android:id="@+id/checkbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="7dp">
<ImageView
android:id="@+id/checkImageView"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/checked"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is the text for checkbox."
android:layout_gravity="center_vertical"
android:layout_marginStart="15dp"/>
</LinearLayout>
- Now the code for it to work.
In your class add this
boolean isChecked = true
Now it will function like this
LinearLayout checkBox = findViewById( R.id.checkbox );
ImageView checkBoxImage = findViewById( R.id.checkImageView );
checkBox.setOnClickListener( v -> {
final Bitmap bmap = ((BitmapDrawable)checkBoxImage.getDrawable()).getBitmap();
Drawable myDrawable = getResources().getDrawable(R.drawable.checked);
final Bitmap myLogo = ((BitmapDrawable) myDrawable).getBitmap();
if (bmap.sameAs(myLogo)){
//it is checked
isChecked = false;
checkBoxImage.setImageResource( R.drawable.unchecked );
}else {
isChecked = true;
checkBoxImage.setImageResource( R.drawable.checked );
}
} );
2.Using a library
- Use this library.

Sample usage
setChecked(boolean checked); //by default, it's wthout animation
setChecked(boolean checked, boolean animate); //pass true to animate
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample);
final CustomCheckBox scb = (CustomCheckBox) findViewById(R.id.scb);
scb.setOnCheckedChangeListener(new CustomCheckBox.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CustomCheckBox checkBox, boolean isChecked) {
Log.d("CustomCheckBox", String.valueOf(isChecked));
}
});
}
Attributes
| Attr | Type | Description |
|---|---|---|
| duration | integer | Animation Duration |
| stroke_width | dimension | The border width when unchecked |
| color_tick | color | Tick color (visible only when checked) |
| color_checked | color | Fill color when selected |
| color_unchecked | color | Fill color when unchecked |
| color_unchecked_stroke | color | Border color when unchecked |
References
Edit 1 (27/1/22)
Because of the comment received by Decent Dabbler , I now remember what it forgot. I actually forgot to mention that the file that takes too long to load is the xml file.This can be proved as we do not always use the class right? Just the view in the xml. If you open the xml file of the checkbox, you find it too big.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
