Table with clickable lines? - android

Is it possible to do a table like the sudoku's one and make the lines which divide the holes clickable? If it's not, what view should I use to make a grid with clickable lines?
Thanks!

Try this:
// create a new TableRow
TableRow row = new TableRow(this);
row.setClickable(true); //allows you to select a specific row
row.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
v.setBackgroundColor(Color.GRAY);
System.out.println("Row clicked: " + v.getId());
//get the data you need
TableRow tablerow = (TableRow)v.getParent();
TextView sample = (TextView) tablerow.getChildAt(2);
String result=sample.getText().toString();
}
});

Related

Removing TableRows programmatically

I have successfully added a TableRow to the TableLayout,now I would like to programmatically remove the TableRow and add the original row to the TableLayout.Here is the code that I use to generate the TableRow:
public TableRow getTableRow(String text,String hint,boolean addCollapseOption)
{
TableRow tr=new TableRow(getBaseContext());
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.WRAP_CONTENT));
TextView temp=new TextView(getBaseContext());
temp.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));
temp.setText(text);
tr.addView(temp);
tempId="edit_"+count;
count++;
EditText edit_temp=new EditText(getBaseContext());
TableRow.LayoutParams edit_params=new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT,1f);
edit_params.setMargins(10, 0, 10, 0);
edit_temp.setLayoutParams(edit_params);
edit_temp.setHint(hint);
tr.addView(edit_temp);
if(addCollapseOption)
{
ImageButton btn_less=new ImageButton(getBaseContext());
btn_less.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));
btn_less.setImageDrawable(getResources().getDrawable(R.drawable.icon_less));
btn_less.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v)
{
shouldCollapse=true;
}
});
tr.addView(btn_less);
}
return tr;
}
The Activity crashes when I try to add this TableRow to the TableLayout:
TableRow tr=getTableRow("First Name","Enter first name here",true);
I guess you cannot remove a View from the Layout and still be able to use the click event in it(that crashes the activity),layout and shouldCollapse are global variables,if set to true:
if(shouldCollapse)
{
layout.removeAllViews();
layout.addView(originalRow);
}
EDIT:Changing the implementation to use removeChildAt does not work:
if(shouldCollapse)
{
int childCount=layout.getChildCount();
for(int i=1;i<childCount;i++)
layout.removeViewAt(i);
originalRow.setVisibility(TableRow.VISIBLE);
shouldCollapse=false;
}
Now,clicking on the btn_less does absolutely nothing.This is the Logcat's opinion of the problem:
Less clicked
The value of shouldCollapse true
So,this means that even though shouldCollapse is set to true but this method is never called...strange.
You can use this code to remove the programmatically generated TableRows:
public void onClick(View v)
{
final TableRow parent = (TableRow) v.getParent();
tr.removeView(parent);
}
Or if you want to use this out side the onClick, you must pass the value of View v to whatever function it is.

getting values of the row on a button click which is in that row

My android app is populating a table layout which is created programmatically from the sqlite database.the last column of the table layout has two buttons. those two buttons are assigned to one column by a linear layout.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
35, 35);
params.setMargins(10, 0, 10, 0);
seventhCol1=new Button(this);
seventhCol1.setText("A");
seventhCol1.setLayoutParams(params);
//seventhCol1.setLayoutParams(new LayoutParams(30, 30));
seventhCol1.setId(71);
seventhCol1.setOnClickListener(mListener);
seventhCol2=new Button(this);
seventhCol2.setText("C");
seventhCol2.setLayoutParams(params);
seventhCol2.setId(72);
seventhCol2.setOnClickListener(nListener);
btn_linear=new LinearLayout(this);
btn_linear.setOrientation(LinearLayout.HORIZONTAL);
btn_linear.addView(seventhCol1);
btn_linear.addView(seventhCol2);
btn_linear.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
btn_linear.setGravity(Gravity.CENTER);
tr.addView(btn_linear);
on click of the button seventhCol1 i want to get the data from the particular row from where that button was clicked...how can I do that????
I tried doing it by this method:
tr.setOnClickListener(rowclickListner);
private OnClickListener rowclickListner=new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
TableLayout tl=(TableLayout) v.getParent();
/*TableRow tr=(TableRow) tl.getParent();*/
TableRow tr=(TableRow) tl.getParent();
TextView tv_mobile=(TextView)tr.getChildAt(2);
String mobile=tv_mobile.getText().toString();
Toast.makeText(LeadManagement.this, mobile, Toast.LENGTH_LONG).show();
/*ArrayList<String> leaddetails=mDbHelper.getTableDetails("lead_table", mobile);
prefEditor.putBoolean("leadbundle", true);
prefEditor.commit();
startActivity(new Intent(LeadManagement.this,CreateLead.class).putExtra("leadlist", leaddetails));*/
}
};
but it is showing an error!! how can i do it?
Don't register an onClickListener as a class member, but rather as an anonymous object. Something like:
Button tr = // your button corresponding to N-th row
final RowData rowData = ... // get the data from row N
tr.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
someMethodCall(rowData)
}
});

How to click an specific TableRow within a TableLayout

I made my own compound control that display a data grid using TableLayout and adding programmatically Tablerows within a loop depending of the Array of Object that im binding to it and now i want to select an specific row with its specific data in order to be used by a method. So how can i select an specific row retrieving its data to delegate a method?
hi you can try something like this,
// create a new TableRow
TableRow row = new TableRow(this);
row.setClickable(true); //allows you to select a specific row
row.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
v.setBackgroundColor(Color.GRAY);
System.out.println("Row clicked: " + v.getId());
//get the data you need
TableRow tablerow = (TableRow)v.getParent();
TextView sample = (TextView) tablerow.getChildAt(2);
String result=sample.getText().toString();
}
});
For more info refer Android TableRow
I tried Parth Doshi's answer and found it to be not quite correct. The view parameter in onClick is a TableRow, so when v.getParent() is called, it returns a TableLayout object, and so an exception is thrown when casting it to TableRow. As such the code that works for me is:
tableRow.setClickable(true); //allows you to select a specific row
tableRow.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
TableRow tablerow = (TableRow) view;
TextView sample = (TextView) tablerow.getChildAt(1);
String result=sample.getText().toString();
Toast toast = Toast.makeText(myActivity, result, Toast.LENGTH_LONG);
toast.show();
}
});

How to retrieve TextView value when we Touch on that and The TextView is added into LinearLayout?

Here is my code for how i make adding Dynamically TextView in to Table Layout using LinearLayout for it.
table_personalData.removeAllViews();
int i=0;
for(String header : headerDetail){
TextView label=new TextView(this);
TextView value=new TextView(this);
label.setTextAppearance(this, R.style.TextBase);
value.setTextAppearance(this, R.style.TextBase);
label.setText(String.format("%s:", header));
value.setText(String.format("%s", lead.data[i]==null?"":lead.data[i]));
i++;
LinearLayout rowview=new LinearLayout(this);
rowview.setOrientation(0);
rowview.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
rowview.addView(label);
rowview.addView(value);
table_personalData.addView(rowview);
}
By Above code i get my table layout with dynamically added textview in that .
here i use Liner Layout for added two textview in One Row .
headerDetail is a ArrayList<String> headerDetail
lead.data[i] i used this to get my values for particular label.
All this work fine for me now what my problem is
Now i want to do one thing that is when we click one of the TextView of my LinearLayout i want that TextView in dynamically.
So finally my question is about to get retrieve textview value when we touch or click any textview which is in Linearlayout and LinearLayout is in TableLayout.
Anyone have idea what should i have to do with this .
i also try something like below but i don't get i can i get TextView Value from below code.
table_personalData.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
for(int i=0;i<table_personalData.getChildCount();i++){
//CharSequence desc=table_personalData.getChildAt(i).getContentDescription();
//Log.v("log_tag", "the values are"+ desc);
}
}
});
EDIT:
if you want the Value of TextView when we Touch on that There is Two way that i write Below :
value.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TextView txt;
txt=(TextView)findViewById(v.getId());
String tempEmail;
tempEmail=txt.getText().toString().trim();
String aEmailList[]={tempEmail};
pattern=Pattern.compile(EMAIL_PATTERN);
if(validate(tempEmail)){
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,aEmailList );
startActivity(emailIntent);
}else{
pattern=Pattern.compile(PHONE_No_PATTERN);
if(validate(tempEmail)){
tempEmail=tempEmail.replaceAll("[^\\d]", "");
String uri="tel:" +tempEmail;
Intent mIntent=new Intent(Intent.ACTION_CALL);
mIntent.setData(Uri.parse(uri));
startActivity(mIntent);
}else{
//Toast.makeText(LeadDetailActivity.this, "Please Touch on Email or Mo:Number", Toast.LENGTH_SHORT).show();
}
}
}
});
1)Create how many text view u want,but set id for each one,and aslo set same click listener for all
TextView value1=new TextView(this);
value1.setid(1);
value1.setOnClickListener(clickListener);
TextView value2=new TextView(this);
value1.setid(2);
value2.setOnClickListener(clickListener);
TextView value3=new TextView(this);
value3.setid(3);
value3.setOnClickListener(clickListener);
2)second create one onclick listener,inside this listener u will get all cliks. so based on view id u can dynamically do anything
OnClickListener clickListener=new OnClickListener() {
public void onClick(View v) {
int id=v.getId();
}
};

How to delete a row in TableLayout dynamically

I am having a TableLayout for which I added rows dynamically.In each of the row there are 2 elements of which one is TextView other is Button.When I click the button that is present in a row,that row should be deleted.How can this be done in Android ? How to find rowid and how to delete a row dynamically.
can anyone help me in sorting out this issue.
Thanks in Advance,
Try this kind of approach:
TableRow tableRow;
TextView tv;
Button button;
//...
tableRow.addView(tv)
tableRow.addView(button);
//somewhere where you want to delete
ViewGroup parent=button.getParent();
if(parent instanceof TableRow)
//do deleting task
Try this:
public void onClick(View v) {
// TODO Auto-generated method stub
TableRow t = (TableRow) v.getParent();
TextView firstTextView = (TextView) t.getChildAt(0);
code = firstTextView.getText().toString();
System.out.println("code>>>>>>" + code);
View row = (View) v.getParent();
// container contains all the rows, you could keep a
// variable somewhere else to the container which you
// can refer to here
ViewGroup container = ((ViewGroup) row.getParent());
// delete the row and invalidate your view so it gets
// redrawn
container.removeView(row);
container.invalidate();
}
You can assign tags or id's when you adding a row. Then just use that tag/id to delete that row.
TableLayout table; // global access, probably initialized in onCreate()
// initialization, etc.
Create element that will be added to the TableLayout, a TableRow with the TextView and the Button then call addDeleteClick(yourButton, uniqueTag) before adding it to the TableLayout.
// example of adding a text view and a button the the TableLayout
void addToTableLayout(String text, String uniqueTag) {
TableRow tr = new TableRow(yourActivity);
// set the unique tag that will be used when deleting this row
tr.setTag(uniqueTag);
// do what you need with the button and textview
TextView tv = new TextView(yourActivity);
tv.setText(text);
Button bt = new Button(yourActivity);
// add delete click capability to the button
addDeleteClick(bt, uniqueTag);
table.addView(tr, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
// Adds the delete on click capability to a button
// to delete a row from TableLayout based on its tag
void addDeleteClick(Button bt, final String tag) {
bt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Log.d("TableLayout", " deleting row with tag " + tag);
deleteRow(tag);
}
});
}
// delete a row from TableLayout based on its tag
void deleteRow(String tag) {
View removedRow = table.findViewWithTag(tag);
table.removeView(removedRow);
table.invalidate();
}

Categories

Resources