I have List of route in tablelayout.
CheckBox & TextView created dynamically.
I set the checkbox id.
I want to do the selectall & deselectAll . How we can implement?
I created checkBox using coding.then How can call this CheckBox cb = (CheckBox)view.findViewById(R.id.????);
tl = (TableLayout) findViewById(R.id.dailyDRoute);
ArrayList<SalesRoutes> routeList = getSalesRoute();
for (int i = 0; i < routeList.size(); i++) {
TableRow tr = new TableRow(this);
CheckBox ch = new CheckBox(this);
ch.setHeight(1);
ch.setId(i);
TextView tv2 = new TextView(this);
tr.addView(ch);
tv2.setText(routeList.get(i).getDescription());
tv2.setTextColor(Color.BLACK);
tr.addView(tv2);
tl.addView(tr);
}
}
public void deselectAll(View view){
// CheckBox cb = (CheckBox)view.findViewById(R.id.);
}
public void selectAll(View view){
}
Please help me ...
Thanks in advance..
R.id.* are integer constants generated during build process. Actually, you need to pass an integer id to findViewById (same integer as you set us as id in init block).
Like this:
for (int i = 0; i < tl.getChildCount(); i++) {
CheckBox cb = (CheckBox)view.findViewById(i);
cb.setChecked(true);
}
But as for me this is not very reliable because id set in runtime can be not unique and I think this loop is better:
for (int i = 0; i < tl.getChildCount(); i++) {
CheckBox cb = (CheckBox)((TableRow)tl.getChildAt(i)).getChildAt(0);
cb.setChecked(true);
}
Related
I have created the dynamic view. That view contains two edittext and one radio group. when I click the add button the view is added to the layout. Now I got a confusion, how to get values from these type of dynamic views. I tried but it doesn't work. when I add the two or more views, the loop does not find the next views values. I want to add that values to ArrayList. This is code:
private void addDynamicViews() {
EditText name = new EditText(this);
EditText mobile = new EditText(this);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
p.setMargins(10, 10, 5, 5);
name.setLayoutParams(p);
name.setBackgroundResource(R.drawable.edittext_box);
name.setHint("Enter Name");
studentslayout.addView(name);
mobile.setLayoutParams(p);
mobile.setBackgroundResource(R.drawable.edittext_box);
mobile.setHint("Enter Mobile No");
studentslayout.addView(mobile);
/* radioGroup - Radio Group
maleButton,femaleButton - Radio Buttons
studentslayout - Linear Layout */
radioGroup = new RadioGroup(this);
radioGroup.setOrientation(RadioGroup.VERTICAL);
maleButton = new RadioButton(this);
maleButton.setText("Male");
radioGroup.addView(maleButton);
femaleButton = new RadioButton(this);
radioGroup.addView(femaleButton);
femaleButton.setText("Female");
studentslayout.addView(radioGroup);
}
How to take all dynamic edittext and radio group values ?
I tried this code But unfortunately it stopped.
#Override
public void onClick(View v) {
String[] array = new String[studentslayout.getChildCount()];
int count = studentslayout.getChildCount();
for (int i=0; i < studentslayout.getChildCount(); i++){
editText = (EditText)studentslayout.getChildAt(i);
array[i] = editText.getText().toString();
RadioButton radValues = (RadioButton) studentslayout.getChildAt(i);
array[i] = radValues.getText().toString();
}
}
RadioButton radValues = (RadioButton) studentslayout.getChildAt(i);
You have added radioGroup and are expecting radiobutton. Also since you are looping, you should check the type of the view.
You can try something like this:
int childCount = studentslayout.getChildCount();
for (int i = 0; i < childCount; i++) {
View childView = studentslayout.getChildAt(i);
if (childView instanceof EditText) {
EditText editText = (EditText) childView;
String text = editText.getText().toString();
//use text
} else if (childView instanceof RadioGroup) {
RadioGroup radioGroup = (RadioGroup) childView;
int radioCount = radioGroup.getChildCount();
for (int j = 0; j < radioCount; j++) {
RadioButton radioButton = (RadioButton) radioGroup.getChildAt(i);
//use radioButton.
}
}
}
I am trying to implement checkboxes list so after selecte the multiple checkboxes I want to store them in ArrayList and after clicking the button send them to the server. At the momenant I am getting the following error in the is statement
The method getId() in the type View is not applicable for the arguments (int)
as wellas at this line cb = new CheckBox(this);
The final local variable cb may already have been assigned
How can I fix that?
private void createCheckboxList(final ArrayList<Integer> items) {
//final CheckBox cb;
final LinearLayout ll = (LinearLayout) findViewById(R.id.lila);
for (int i = 0; i < items.size(); i++) {
CheckBox cb = new CheckBox(this);
cb.setText(items.get(i).toString());
cb.setId(i);
ll.addView(cb);
}
Button btn = new Button(this);
btn.setLayoutParams(new LinearLayout.LayoutParams(500, 150));
btn.setText("submit");
ll.addView(btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i : items) {
if (cb.getId(i).isChecked()) {
}
}
}
});
}
Edit:
I have deleted the final CheckBox cb; and assign the object in the for loop CheckBox cb = new CheckBox(this); but I am getting in the if statement this error cb cannot be resolved How can I assign it to get ride of the error?
When I assign the Checkbox as class variable I am getting this error The method getId() in the type View is not applicable for the arguments (int) in the if statement.
final LinearLayout ll = (LinearLayout) findViewById(R.id.lila);
for (int i = 0; i < items.size(); i++) {
CheckBox cb = new CheckBox(this);
cb.setText(String.valueOf(items.get(i)));
cb.setId(i);
ll.addView(cb);
}
use this rather
I am trying to access the cb in the if statement but I am getting cb cant be resolved
I have tried declare Checkbox cb as class variable but I am getting The method getId() in the type View is not applicable for the arguments (int).
I tried to declare it as method local variable like final CheckBox cb; but I am getting two errors: The first one The final local variable cb may already have been assigned at this line cb = new CheckBox(this); and the second one The method getId() in the type View is not applicable for the arguments (int)
how can I fix that?
private void createCheckboxList(final ArrayList<Integer> items) {
//final CheckBox cb;
final LinearLayout ll = (LinearLayout) findViewById(R.id.lila);
for (int i = 0; i < items.size(); i++) {
CheckBox cb = new CheckBox(this);
cb.setText(String.valueOf(items.get(i)));
cb.setId(i);
ll.addView(cb);
}
Button btn = new Button(this);
btn.setLayoutParams(new LinearLayout.LayoutParams(500, 150));
btn.setText("submit");
ll.addView(btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i : items) {
if (cb.getId(i).isChecked()) {
}
}
}
});
}
The reference cb wont exist outside the 'for' loop. Since you know its id you can create a new checkbox reference and use findviewById(); to get refer to the same checkbox
cb.getId() returns and integer not a checkbox reference
final LinearLayout ll = (LinearLayout) findViewById(R.id.lila);
for (int i = 0; i < items.size(); i++) {
final CheckBox cb = new CheckBox(this);
cb.setText(String.valueOf(items.get(i)));
cb.setId(i);
ll.addView(cb);
}
Button btn = new Button(this);
btn.setLayoutParams(new LinearLayout.LayoutParams(500, 150));
btn.setText("submit");
ll.addView(btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i < items.size()) { \\
Checkbox ch=(Checkbox) findViewById(i); \\
if (ch.isChecked()) {
}
}
}
});
}
how to create the textview in code not in xml file. It is because number of textviews will be changing in my application according to some integer.
This is the code to create TextView Dynamically
LinearLayout layout = (LinearLayout ) findViewById(R.id.llayout);
for (int i = 0; i < 3; i++) {
TextView dynamicTextView = new TextView(this);
dynamicTextView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
dynamicTextView.setText("NewYork");
layout.addView(tstate);
}
Maybe thats what you need:
LinearLayout lin = (LinearLayout) findViewById(R.id.myLinear);
for (int i = 0; i <= 10 ; i++)
{
TextView myText = new TextView(this);
myText.setText("textview# "+ i);
lin.addView(myText);
}
Something like the following should be what you need:
final int N = 10; // total number of textviews to add
final TextView[] myTextViews = new TextView[N]; // create an empty array;
for (int i = 0; i < N; i++) {
// create a new textview
final TextView rowTextView = new TextView(this);
// set some properties of rowTextView or something
rowTextView.setText("This is TextView #" + i);
// add the textview to the linearlayout
myLinearLayout.addView(rowTextView);
// save a reference to the textview for later
myTextViews[i] = rowTextView;
}
private LinearLayout ll;
private TextView tv;
// in oncreate()
onCreate()
{
int WrapWidth = LinearLayout.LayoutParams.WRAP_CONTENT;
int WrapHeight = LinearLayout.LayoutParams.WRAP_CONTENT;
tv = new TextView(this);
ll.addView(tv,WrapWidth,WrapHeight);
}
Code is here
final int c = 12;
final TextView[] mtext = new TextView[c];
for (int i = 0; i < c; i++) {
TextView rowtxt = new TextView(this);
rowtxt.setText("Hello" + i);
myLinearLayout.addView(rowtxt);
myTextViews[i] = rowtxt;
myTextViews[i].setOnClickListener(onclicklistener);//textview click
}
OnClickListeners code is here
OnClickListener onclicklistener = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v == myTextViews[0]){
//do whatever you want....
}
}
};
Hope it is helpful for you
You use TextView textView = new TextView(CurrentActivity.this);
and then you add setter arguments that come with the TextView class
final TableLayout table = (TableLayout) findViewById(R.id.tableLayout);
TableRow row = new TableRow(this);
TextView t2 = new TextView(this);
t2.setText("test");
row.addView(t2);
Button bu = new Button(this);
bu.setBackgroundResource(R.drawable.del);
bu.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
//I need to delete the tablerow
//how to do?
}
});
row.addView(bu);
table.addView(row, new TableLayout.LayoutParams(WC, WC));
**
i want to delete tablerow in bu.setOnClickListener
how to do removeViewAt() ,i cant find indexId
**
use removeView for removing tablerow as:
table.removeView(row);
NOTE: If they don't have unique id then use:
table.removeView(rowIndex);
and by using removeViewAt
for(int i = 0, j < table.getChildCount(); i < j; i++){
// then, you can remove the the row you want...
// for instance...
TableRow row = getChildAt(i);
if( something you want to check ) {
removeViewAt(i);
// or...
removeView(row);
}
}