Code example for CheckBox

Methods: setGravitysetPaddingsetText, setChecked, setLayoutParams, setOnClickListener

0
 
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                    ViewGroup.LayoutParams.FILL_PARENT, 64);
 
            CheckBox checkBox = new CheckBox(Plugins_View.this);
            checkBox.setLayoutParams(lp);
            // Center the text vertically 
            checkBox.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER);
            // Set the text starting position 
            checkBox.setOnClickListener(new OnClickListener(){
            	public void onClick(View v){
                    if (((CheckBox) v).isChecked()) {
                    	SharedPreferences settings = getSharedPreferences("Munin_Node", 0);
                        SharedPreferences.Editor editor = settings.edit();
                        editor.putBoolean(((CheckBox) v).getText().toString(), true);
                        editor.commit();
                    } else { 
                    	SharedPreferences settings = getSharedPreferences("Munin_Node", 0);
                        SharedPreferences.Editor editor = settings.edit();
                        editor.putBoolean(((CheckBox) v).getText().toString(), false);
                        editor.commit();
                    } 
            	} 
            }); 
            checkBox.setPadding(36, 0, 0, 0);
            checkBox.setText(getChild(groupPosition, childPosition).toString());
        	SharedPreferences settings = getSharedPreferences("Munin_Node", 0);
        	checkBox.setChecked(settings.getBoolean(getChild(groupPosition, childPosition).toString(), true));
            return checkBox;
        } 
 
        public Object getGroup(int groupPosition) {
            return groups[groupPosition];