i have an app that make check box dynamically.
A button and edit text,i insert some thing in the edit text then click button after that a check box with that text appear.
but i have problem with that.
i am completely confuse, i cannot see them!!!! and i do not have any error!!!!????
can some one help me ? :(
...
public int i2 = 0;
...
final EditText et1 = (EditText) findViewById(R.id.editText1);
final Button bnext = (Button) findViewById(R.id.button1);
//
final LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
//
bnext.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//getting text from edit text
String str_et1 = et1.getText().toString();
do {
CheckBox cb = new CheckBox(getApplicationContext());
cb.setId(i2);//set id to each checkbox
cb.setText(str_et1);//set text to each checkbox
ll.addView(cb);
} while (bnext.isDirty());//add when ever i clicked
i2++;//this the counter
}
});
Try this code -
final EditText et1 = (EditText) findViewById(R.id.editText1);
final Button bnext = (Button) findViewById(R.id.button1);
//
final LinearLayout ll = new LinearLayout(this);
LayoutParams params =
new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setLayoutParams(params);
//
bnext.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//getting text from edit text
String str_et1 = et1.getText().toString();
do {
CheckBox cb = new CheckBox(getApplicationContext());
cb .setLayoutParams(params);
cb.setId(i2);//set id to each checkbox
cb.setText(str_et1);//set text to each checkbox
ll.addView(cb);
} while (bnext.isDirty());//add when ever i clicked
i2++;//this the counter
}
});
Hope this code helps you!!
if its not working please let me know i will try to help more.
A check box alone is not a visible component. You will need to have a LayoutParam, then attach the checkbox to it to make it visible.
Related
I am working with android LinearLayout and i am adding a TextView programmicataly and then on TextView click add an EditText in View. Now I want to get data at then end on button click from all added EditTexts in layout. here is my coding through which i am working.
TextView addMoreText = new TextView(this);
addMoreText.setText("Add More Ingredients");
addMoreText.setGravity(Gravity.CENTER);
addMoreText.setPadding(20, 20, 20, 20);
addMoreText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.add, 0);
addMoreText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final EditText editTextItem = new EditText(SearchRecipe.this);
editTextItem.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.cross, 0);
editTextItem.setPadding(20, 20, 20, 20);
editTextItem.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
parentLayout.removeView(editTextItem);
return true;
}
});
parentLayout.addView(editTextItem, 0);
}
});
parentLayout.addView(addMoreText);
searchRecipe.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
i want to get all the data from EditText on searchRecipe click
Assign the id to the EditText:
editTextItem .setId({yourId});
Then inside searchRecipe onClick method, invoke:
EditText editTextItem = (EditText) parentLayout.findViewById({yourId});
And then use it as you want.
Add an arraylist and put the edittexts in it to keep track of them.
ArrayList<EditText> etList = new ArrayList();
In onClick of textview just add to it at the end.
etList.add(editTextItem);
In searchReciepe go through your list.
for(Edittext et : etList){
et.getText();
[...]
}
Edit: Obviously remember to remove it from the List on remove.
private int EDITTEXT_ID = 1;
private List<EditText> editTextList = new ArrayList<EditText>();
// Add Edittext programmatically in your view
private void addView() {
EditText editText = new EditText(this);
editText.setId(EDITTEXT_ID);
EDITTEXT_ID++;
editText.setText("Hello there " + EDITTEXT_ID);
editTextList.add(editText);
lnvEditText.addView(editText); // lnvEdittext is my LinearLayout which is added in XML file
}
// Get values of every Edittext on click of button
for(int i=0; i<editTextList.size(); i++) {
Log.e("All Values=", editTextList.get(i).getText().toString());
}
Here I have created EdiIexts dynamically by clicking the button, how can I take values from these EditTexts? I have seen many examples but I am unable to get values!
final LinearLayout ll=new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
Button add_btn=new Button(this);
add_btn.setText("Click to add TextViiews and EditTexts");
ll.addView(add_btn);
add_btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
EditText et=new EditText(getApplicationContext());
ll.addView(et);
You can get dynamically created edittext's value the same way you would do with edittext of .xml file.
String value;
add_btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
value = et.getText().toString();
}
});
add this code where you want to get the edittext's value
EditText et2=(EditText)ll.getChildAt(l1.getChildCount()); //make sure to create new edittext variable do not use "et"
String s=et2.getText().toString();
Declare EditText et as a Class level Member variables for your Activity,
private EditText et = null;
Now, in Button's onClick
add_btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
et = new EditText(getApplicationContext());
ll.addView(et);
Now, you can get the EditText et values any where in your Activity scope, using
if(et != null)
String value = et.getText().toString();
try this,
LinearLayout linearLayoutForm = (LinearLayout) activity.findViewById(R.id.linearLayoutForm);
EditText edit = (EditText) linearLayoutForm.findViewById(id);
String value = edit.getText().toString();
A table is being created such that:
TableLayout layout = new TableLayout(this);
layout.setLayoutParams(new TableLayout.LayoutParams(4,5));
layout.setPadding(1, 1, 1, 1);
for(int i=0; i<7; i++) {
TableRow tr = new TableRow(this);
for(int j=0; j<6; j++) {
Button b = new Button(this);
b.setText("0");
b.setOnClickListener(buttonListener);
tr.addView(b);
}
layout.addView(tr);
}
super.setContentView(layout);
The OnClickListener buttonListener is:
View.OnClickListener buttonListener = new View.OnClickListener() {
public void onClick(View v) {
Button thisButton = (Button) findViewById(((Button)v).getId());
//thisButton.setText(Integer.toString(Integer.parseInt((String) thisButton.getText()) + 1));
thisButton.getText();
}
};
The call to thisButton.getText() throws a NullPointerException, and I'm not sure why. Can anyone help me out?
TableLayout layout = new TableLayout(this);
layout.setLayoutParams(new TableLayout.LayoutParams(4,5));
layout.setPadding(1, 1, 1, 1);
for(int i=0; i<7; i++) {
TableRow tr = new TableRow(this);
for(int j=0; j<6; j++) {
Button b = new Button(this);
b.setText("0");
b.setOnClickListener(buttonListener);
tr.addView(b);
}
layout.addView(tr);
}
setContentView(layout);
}
View.OnClickListener buttonListener = new View.OnClickListener() {
public void onClick(View v) {
Button btn1 = (Button)v;
//thisButton.setText(Integer.toString(Integer.parseInt((String) thisButton.getText()) + 1));
String name = btn1.getText().toString();
}
};
I have tested this will work.
When you create view dynamically you don't to assgin an id to it.
you work using the object of the view.
I hope it makes sense to all the people who are curious to set id for dynamic buttons.
One problem in your code.
Have not set id of Button
b.setText("0");
b.setOnClickListener(buttonListener);
b.setId(i);
Assumption:
I can't see anything regarding setting id to button
The below line may causing exception:
Button thisButton = (Button) findViewById(((Button)v).getId());
Why are you doing? Instead you are already passing a view in click action: View v
So you can do straight way like:
String strText = ((Button) v).getText();
It mean your thisButton is probably null, because the statement before that was not successful.
Button thisButton = (Button) findViewById(((Button)v).getId());
does not look right. Usually the paramenter inside findViewById() should be a resource id.
such as
Button thisButton = (Button) findViewById(R.id.button);
R.id.button is defined by your layout xml.
You may simply get the text from the passed view, instead of recreating a button.
public void onClick(View v) {
if(v instanceof Button)
String text = ((Button) v).getText();
}
I created buttons dynamically, which is assigned to url address obtained from handlers as settext().
But I am not able to get text of that button,if it is clicked,since arg.gettext() is not working in OnClickListener.
Is there any way to get text of the button which is created dynamically
for ( i = 0; i <itemList.getTitle().size()-1; i++) {
title[i] = new TextView(this);
title[i].setTextColor( -16711936 );
title[i].setTextSize(18);
title[i].setText("Title = "+itemList.getTitle().get(i));
description[i] = new TextView(this);
description[i].setTextColor(-16776961);
description[i].setText("Description = "+itemList.getDescription().get(i)+"......");
more[i]=new Button(this);
more[i].setText(itemList.getLink().get(i));
layout.addView(title[i]);
System.out.println("Title view is set");
layout.addView(description[i]);
//System.out.println("Description view is set");
layout.addView(more[i]);
more[i].setOnClickListener(listener);
}
private OnClickListener listener=new OnClickListener(){
public void onClick(View arg) {
// TODO Auto-generated method stub
String value=(should get the text of the selected button)
}
Any help would be greatly appreciated.
The View in onClick() is your Button.
Just downcast it:
Button btn = (Button) arg;
String btnText = btn.getText();
I have created an array of buttons..now i want to interact with each button..so that when i click a particular button it will show the text of that button on my text view..so please suggest me for that.. i am sending my code where i have created an array of button..
public boolean initDay()
{
LinearLayout layoutVertical = (LinearLayout) findViewById(R.id.liVLayout);
LinearLayout rowLayout=null;
LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT,1);
for (int i = 0; i<6; i++)
{
rowLayout = new LinearLayout(this);
rowLayout.setWeightSum(7);
layoutVertical.addView(rowLayout,param);
for(int j=0;j<7;j++)
{
pBtnDay[i][j]=new Button(this);
rowLayout.addView(pBtnDay[i][j],param);
pBtnDay[i][j].setClickable(true);
}
}
return true;
}
setOnClickListener() for every object in loop:
pBtnDay[i][j].setOnClickListener(new View.OnClickListener{});
Nothing special. Hope this helps.
pBtnDay[i][j].setOnClickListener(new OnClickListener(){
public void onClick(View v){
v.getText();//it contains button level
}
}
you can interact with each button using this code :
for(int j=0;j<7;j++)
{
pBtnDay[i][j]=new Button(this);
rowLayout.addView(pBtnDay[i][j],param);
pBtnDay[i][j].setClickable(true);
//add a listener for each button
pBtnDay[i][j].setOnClickListener(new OnClickListener(){
Toast.makeText(YourActivity.this, "text of button is "+pBtnDay[i][j].getText(),4000).show();
});
}