Today I developed a sample code to explain how to create, use and control checkboxes created in Android via programming (and not using XML layouts)
I decided to write this down because it has nothing to do with Android programming, it has to do with programming in general.
The final result will be like the image below:
In your layout you need to have a LinearLayout where to put all the new elements:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layoutBase"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
Next, write the following code in your Activity (change as you need for your specific project):
package com.example.test;
import java.util.Vector;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity
{
Context context;
/**
* Vector for storing checkboxes and use later
*/
Vector<CheckBoxCreatedModel> vectorWithCheckboxes = new Vector<CheckBoxCreatedModel>();
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getApplicationContext();
// Vector with all the info about the checkbox to be created
Vector<CheckboxModel> vectorWithInfo = new Vector<CheckboxModel>();
CheckboxModel model = new CheckboxModel();
model.id = "1";
model.name = "Uno";
model.enabled = true;
model.checked = true;
vectorWithInfo.add( model );
model = new CheckboxModel();
model.id = "2";
model.name = "Dos";
model.enabled = true;
model.checked = true;
vectorWithInfo.add( model );
model = new CheckboxModel();
model.id = "3";
model.name = "Tres";
model.enabled = false;
model.checked = true;
vectorWithInfo.add( model );
LinearLayout layoutBase = (LinearLayout)findViewById(R.id.layoutBase);
addCheckbox(vectorWithInfo, layoutBase);
}
private void addCheckbox(Vector<CheckboxModel> vectorWithInfo, LinearLayout addToView)
{
for ( int i=0; i < vectorWithInfo.size(); i++ )
{
// Model with data
CheckboxModel model = vectorWithInfo.elementAt(i);
// Create a new LinearLayout for adding the elements
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);
addToView.addView(ll);
// Create a checkbox
CheckBox check = new CheckBox(this);
ll.addView(check);
check.setEnabled( model.enabled );
check.setChecked( model.checked);
// Create a TextView as a label for the checkbox
TextView tv = new TextView(this);
tv.setText(model.name);
ll.addView(tv);
// Create a model for adding it to our Vector
CheckBoxCreatedModel cbcm = new CheckBoxCreatedModel();
cbcm.model = model;
cbcm.ll = ll;
cbcm.check = check;
cbcm.tv = tv;
// Add this model to Vector so I can access it anytime from anywhere
vectorWithCheckboxes.addElement(cbcm);
}
}
class CheckBoxCreatedModel
{
public CheckboxModel model;
public LinearLayout ll;
public CheckBox check;
public TextView tv;
}
/**
* Model class for creating a checkbox.
* (you should use all the variables as you please)
*/
class CheckboxModel
{
public String name;
public String id;
public boolean enabled = false;
public boolean checked = false;
}
}
Hope you understand and lern something about it.