I have a table layout where I display the values as below:
Now I am able to achieve on row click, but I have three buttons in each row.
On click on Start, Completed and Delivered I need to update a column (Status) in mysql table.
If it was just a table row we could have done it like this:
tr.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View pos) {
// TODO Auto-generated method stub
System.out.println("Row clicked: " + pos.getId());
}
});
Below is my code for table layout:
/*******getting values from mysql db and assigning it to teach row***********/
try{
allarray = new JSONArray(result);
System.out.println("array is " +allarray.length());
JSONObject json_data=null;
for(int j=0;j<allarray.length();j++){
json_data = allarray.getJSONObject(j);
json_data.getString("PreparationLot");
json_data.getString("Prep_MenuItem");
json_data.getString("Prep_Quantity");
json_data.getString("Prep_Order_Number");
json_data.getString("Prep_Notes");
TableLayout tl = (TableLayout) findViewById(R.id.tableLayout1);
TableRow tr = new TableRow(DashBoard.this);
if(count%2!=0) {
tr.setBackgroundColor(Color.rgb(222, 219, 219));
}else{
tr.setBackgroundColor(Color.rgb(255, 255, 255));
}
tr.setId(20);
tr.setLayoutParams(new LayoutParams());
TextView lotno = new TextView(DashBoard.this);
lotno.setId(200+count);// define id that must be unique
lotno.setText(""+json_data.getString("PreparationLot")); // set the text for the header
lotno.setTextColor(Color.BLACK); // set the color
lotno.setPadding(10, 10, 0, 10); // set the padding (if required)
lotno.setTextSize(18);
tr.addView(lotno);
TextView itemname = new TextView(DashBoard.this);
itemname.setId(200+count);// define id that must be unique
itemname.setText(""+json_data.getString("Prep_MenuItem")); // set the text for the header
itemname.setTextColor(Color.BLACK); // set the color
itemname.setPadding(30, 10, 0, 10); // set the padding (if required)
itemname.setTextSize(18);
tr.addView(itemname);
TextView qty = new TextView(DashBoard.this);
qty.setId(200+count);// define id that must be unique
qty.setText(""+json_data.getString("Prep_Quantity")); // set the text for the header
qty.setTextColor(Color.BLACK); // set the color
qty.setPadding(70, 10, 0, 10); // set the padding (if required)
qty.setTextSize(18);
tr.addView(qty);
TextView order = new TextView(DashBoard.this);
order.setId(200+count);// define id that must be unique
order.setText(""+json_data.getString("Prep_Order_Number")); // set the text for the header
order.setTextColor(Color.BLACK); // set the color
order.setPadding(60, 10, 0, 10); // set the padding (if required)
order.setTextSize(18);
tr.addView(order);
TextView notes = new TextView(DashBoard.this);
notes.setId(200+count);// define id that must be unique
notes.setText(""+json_data.getString("Prep_Notes")); // set the text for the header
notes.setTextColor(Color.BLACK); // set the color
notes.setPadding(60, 10, 0, 10); // set the padding (if required)
notes.setTextSize(18);
tr.addView(notes);
Button start = new Button(DashBoard.this);
start.setText("Start");
start.setMaxWidth(10);
tr.addView(start);
Button complete = new Button(DashBoard.this);
complete.setText("Complete");
complete.setMaxWidth(10);
tr.addView(complete);
Button delivered = new Button(DashBoard.this);
delivered.setText("Delivered");
delivered.setMaxWidth(10);
tr.addView(delivered);
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
count++;
tr.setClickable(true);
tr.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View pos) {
// TODO Auto-generated method stub
// Toast.makeText(DashBoard.this, v.getId(), 1000).show();
System.out.println("Row clicked: " + pos.getId());
}
});
}
}
catch(JSONException e1){
Toast.makeText(getBaseContext(), "No data Found" ,Toast.LENGTH_LONG).show();
}
Problem: if I click on Start, Completed or Delivered I need to update that row's status. How to do it?
you will need to add the onclicklistners for each items within the row, instead of the row itself.
Button start = new Button(DashBoard.this);
start.setText("Start");
start.setMaxWidth(10);
start.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View pos) {
// update fields for start
// your code for update your status of the row
}
});
tr.addView(start);
Button complete = new Button(DashBoard.this);
complete.setText("Complete");
complete.setMaxWidth(10);
complete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View pos) {
// update fields for complete
// your code for update your status of the row
}
});
tr.addView(complete);
Button delivered = new Button(DashBoard.this);
delivered.setText("Delivered");
delivered.setMaxWidth(10);
delivered.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View pos) {
// update fields for delivered
// your code for update your status of the row
}
});
tr.addView(delivered);
edit: added the 2 other onclicklistners and added some commets
Problem: if I click on Start, Completed or Delivered I need to update
that row's status. How to do it?
First, I don't know what do you understand from updating the row's status. If you want to "see" click events for any of the Buttons in any of the TableRows create a single OnCLickListener :
private OnClickListener mListener = new OnClickListener() {
#Override
public void onClick(View v) {
TableRow tr = (TableRow) v.getParent();
// now you have a reference to the row where this Button that was
// clicked exists
// do whatever row updates you want.
}
};
This mListener would be set to each of the TableRow's Buttons:
//...
Button start = new Button(DashBoard.this);
start.setOnClickListener(mListener);
start.setText("Start");
start.setMaxWidth(10);
tr.addView(start);
Button complete = new Button(DashBoard.this);
complete.setOnClickListener(mListener);
complete.setText("Complete");
complete.setMaxWidth(10);
tr.addView(complete);
Button delivered = new Button(DashBoard.this);
delivered.setOnClickListener(mListener);
delivered.setText("Delivered");
delivered.setMaxWidth(10);
tr.addView(delivered);
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());
}
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.
I'm creating a Tablelayout with many TableRows dynamically, for example:
for(int i = 0; i<cont; i++)
{
id[i] = customers[i].CustomerNumber;
//Create a new row to be added.
tr = new TableRow(this);
//Create text views to be added to the row.
tv = new TextView(this);
//Put the data into the text view by passing it to a user defined function createView()
createView(tr, tv, id[i].ToString());
//Add the new row to our tableLayout tl
tl.AddView(tr);
}
And this is the createView code:
private void createView(TableRow tr, TextView t, String viewdata) {
t.SetText(viewdata, TextView.BufferType.Editable);
//adjust the porperties of the textView
//t.SetLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
//You have to use Android.Graphics.Color not System.ConsoleColor;
t.SetTextColor(Color.Blue);
t.SetBackgroundColor(Color.Cyan);
t.SetPadding(5, 0, 0, 0);
tr.SetPadding(0, 1, 0, 1);
tr.SetBackgroundColor(Color.Black);
tr.AddView(t); // add TextView to row.
}
My issue is that I want to select from the TableLayout that contains everything in a single row to be able to select and respond a click event in order to use it for further purpose.
change your code as for making TableRow Clickable set tr.setClickable(true) and add a setOnClickListener:
private void createView(TableRow tr, TextView t, String viewdata) {
t.SetText(viewdata, TextView.BufferType.Editable);
//adjust the porperties of the textView
//t.SetLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
//You have to use Android.Graphics.Color not System.ConsoleColor;
t.SetTextColor(Color.Blue);
t.SetBackgroundColor(Color.Cyan);
t.SetPadding(5, 0, 0, 0);
tr.SetPadding(0, 1, 0, 1);
tr.SetBackgroundColor(Color.Black);
tr.setClickable(true);
tr.setOnClickListener(tablerowOnClickListener);//add OnClickListener Here
tr.AddView(t); // add TextView to row.
}
private OnClickListener tablerowOnClickListener = new OnClickListener() {
public void onClick(View v) {
//GET TEXT HERE
String currenttext = ((TextView)v).getText().toString());
}
};
Set on click listener on table row.
tr.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//TODO:
}
});
I found something cool and its working fine...
TableLayout tl=(TableLayout)findViewById(R.id.maintable);
....
TableRow tr1 = new TableRow(this);
tr1.setLayoutParams(newLayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
TextView textview1 = new TextView(this);
textview1.setText(etFirstname.getText());
textview1.setPadding(5, 0, 0, 0);
textview1.setTextColor(Color.YELLOW);
textview1.setBackgroundColor(Color.GREEN);
tr1.addView(textview1);
TextView textview2 = new TextView(this);
textview2.setText(etAge.getText());
//textview2.setText(etAge.getText());
textview2.setPadding(5, 0, 0, 0);
textview2.setTextColor(Color.RED);
textview2.setBackgroundColor(Color.GRAY);
tr1.addView(textview2);
tr1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TableRow tr1=(TableRow)v;
TextView tv1= (TextView)tr1.getChildAt(0);
Toast.makeText(getApplicationContext(),tv1.getText().toString(),Toast.LENGTH_SHORT).show();
}
});
tl.addView(tr1);
/************ if table row is dynamic then this method else method 2 is perfect************/
//if we want to applay listener on dynamic tablerow then use this
//sure that perfect
TablRowe tr = new TableRow(this);
tr.setClickable(true);
tr.setId(100);// if in loop then add 1 counter with 100 like (100+counter) at end count++ it
tr.setOnClickListener(this);
#Override
public void onClick(View v)
{
switch (v.getId())
{
case 100:
Toast.makeText(getApplicationContext(), "100", Toast.LENGTH_SHORT).show();
break;
case 101:
Toast.makeText(getApplicationContext(), "101", Toast.LENGTH_SHORT).show();
break;
}
/************************** for simple like this ************************/
TableRow row1 = (TableRow)findViewById(R.id.row1);
row1.setonClickListener(this);
public void onClick(View v)
{
switch (v.getId())
{
case R.id.row1:
// do work
break;
}
}
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();