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());
}
Related
Android: I have created dynamic buttons based on my arraylist size,Lets consider 10 buttons. When a button is clicked, the color of the button will change to grey. When another one is clicked, the color of the previous button should be reset to the default color.
boolean iscolor = true;
final LinearLayout linearLayout = view.findViewById(R.id.total_count);
final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
for (int j =1;j<=datalist.size()/2;j++) {
final Button btn = new Button(getContext());
final int id_ = j;
btn.setText("" + j);
btn.setTextColor(Color.WHITE);
btn.setMaxWidth(5);
btn.setId(id_);
btn.setPadding(8, 8, 8, 8);
btn.setBackgroundColor(getContext().getResources().getColor(R.color.DarkBlue));
linearLayout.addView(btn, params);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!iscolor ) {
btn.setBackgroundColor(getResources().getColor(R.color.DarkBlue));
iscolor =true;
}
else
{
btn.setBackgroundColor(getResources().getColor(R.color.gray));
iscolor = false;
}
}});
How to restore the color of the previous clicked Button in Android.
Try this :
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
/* you need to have already stored buttons in a data structure, something like : List<Button> btns; */
for(Button b : btns){
if(b.getId() == v.getId(){ b.setBackgroundColor(getResources().getColor(R.color.gray)); } else{ b.setBackgroundColor(getResources().getColor(R.color.yourdefaultcolor)); } //no need for isColor variable
}});
linearLayout.addView(btn, params);
I have TableLayout with 2 buttons and some textView
I added rows dynamically from sq-lite with these codes and it works:
TableLayout tbl
= (TableLayout) Vfood.findViewById(R.id.frag_food);
///
///somecode
///
dbfood.open();
Cursor cursor = dbfood.getGroupFood(group_Name);
int i = 1;
if (cursor.moveToFirst()) {
do {
TableRow tr = new TableRow(getActivity());
tr.setId(i);
//btnAdd
final Button btnAddDeser = new Button(getActivity());
btnAddDeser.setText("add");
btnAddDeser.setId(i);
TableRow.LayoutParams trParams1
= new TableRow.LayoutParams(65, TableRow.LayoutParams.MATCH_PARENT);
btnAddDeser.setGravity(Gravity.LEFT);
btnAddDeser.setTextSize(10);
btnAddDeser.setPadding(30, 10, 10, 10);
btnAddDeser.setMaxWidth(65);
btnAddDeser.setMinimumWidth(70);
//my problem>
btnAddDeser.setOnClickListener(mListener);
//
tr.addView(btnAddDeser, trParams1);
// Count
final TextView trCount = new TextView(getActivity());
trCount.setText(cursor .getString(3));
trCountDeserc.setId(i);
trCount.setTextColor(Color.BLACK);
trCount.setGravity(Gravity.RIGHT);
trCount.setTextSize(10);
trCount.setPadding(10, 10, 10, 15);
trCount.setClickable(true);
tr.addView(trCount);
///
///other text View
///some code
tbl.addView(tr,
new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
i++;
} while (cdeser.moveToNext());
}
dbDeser.close();
return Vfood;
}
private OnClickListener mListener = new OnClickListener() {
#Override
public void onClick(View v) {
//
//I dont know what shoud i do !
//
}
};
I just want to increase trCount one digit when user click on button.
can anyone help me?
I am new to android.
thanks
You just add this Button click event in your loop
btnAddDeser.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String str=trCount.getText().toString();
Toast.makeText(getActivity(), str, Toast.LENGTH_LONG).show();
}
});
I have created a set of TextViews programmatically using a for loop. This what i have tried.
for(int i=1; i<5; i++){
valueTV = new TextView(AddMyVehicle.this);
linearLayout.addView(valueTV);
vehicleModelReturned = myVehicleData.getString("VehicleModel"+x, "");
valueTV.setText(vehicleModelReturned);
valueTV.setGravity(Gravity.CENTER_HORIZONTAL);
valueTV.setTextSize(TypedValue.COMPLEX_UNIT_SP,22);
valueTV.setTextColor(Color.parseColor("#333333"));
i++;
}
this.valueTV.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
valueTV.setText("Hello");
}
});
I need to change the text of clicked TextView to "Hello". How can i achieve this?
You have to add a listener to all your TextView created.
And be careful, you incremented i twice : one in the first line of the for and another one in the end of the for.
for (int i = 1; i < 5; i++)
{
final TextView valueTV = new TextView(AddMyVehicle.this);
linearLayout.addView(valueTV);
vehicleModelReturned = myVehicleData.getString("VehicleModel" + x, "");
valueTV.setText(vehicleModelReturned);
valueTV.setGravity(Gravity.CENTER_HORIZONTAL);
valueTV.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
valueTV.setTextColor(Color.parseColor("#333333"));
valueTV.setId("test");
valueTV.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
TextView tv = (TextView) v;
tv.setText("Hello");
}
});
}
First, set the OnClickListener to all the TextViews you create, not just the last one. That is, move the setOnClickListener() inside the for loop.
Second, in onClick(), change the text of the clicked view and not again the last one you created. The View v param is the view that was clicked. You can cast it to TextView.
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();
I'm new in android and i have created editText dynamically with the following code while clicking add new button.Is it possible to add a delete button near editText so that each while clicking the delete respective editText will be removed?
btnAddNew.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
LinearLayout rAlign = (LinearLayout)findViewById(R.id.lId);
EditText newPass = new EditText(getApplicationContext());
allEds.add(newPass);
newPass.setHint("Name of Label");
newPass.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
newPass.setWidth(318);
newPass.setTextColor(Color.parseColor("#333333"));
newPass.setId(textb);
rAlign.addView(newPass);
MY_BUTTON ++;
addSpinner();
}
});
Yes, create your remove button at the same time as your EditText and use the removeView() method just like the addView() method, maybe like this:
btnAddNew.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
...
rAlign.addView(newPass);
MY_BUTTON ++;
addSpinner();
Button btnRemoveOld = new Button(this);
btnRemoveOld.setId(32); // arbitrary number
btnRemoveOld.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
LinearLayout rAlign = (LinearLayout)findViewById(R.id.lId);
rAlign.removeView(findViewById(textb));
}
});
// You will need to set parameters to define how the button looks
// and where it is in relation to the EditText here
rAlign.addView(btnRemoveOld);
}
});