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();
}
});
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 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 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);
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'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;
}
}