Reputation: 103
i want to make a table layout in which there are 2 rows.
In both rows there are one Label and a Text Field when i press the 1st row or 2nd row the prompt Dialog will open and u enter any value it will set on Text field of selected row.
please guide me how to make a click listener on row with example and how to call a dialog when the row is selected.
Upvotes: 2
Views: 9456
Reputation: 305
//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;
}
Upvotes: 0
Reputation: 305
TableRow row1 = findViewById(R.id.row2);
row1.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
// do any thing
}
});
// Method 2 ********************
TableRow row1 = findViewById(R.id.row2);
row1.setonClickListner(this);
public void onClicl(View v)
{
switch (v.getId())
{
case R.id.row1:
break;
}
}
Upvotes: 1
Reputation: 459
Simply give each TableRow element a unique id and define an onClick
method:
<TableRow
android:id="@+id/one"
android:onClick="rowClick">
Find the row by id from layout and then add following in java class
tableRow= (TableRow) findViewById(R.id.one);
tableRow.setClickable(true);
tableRow.setOnClickListener(onClickListener);
private OnClickListener onClickListener= new OnClickListener() {
public void onClick(View v) {
show_dialog();
}
};
Then Call Following Method
public void show_dialog() {
final Dialog dialog = new Dialog(getApplicationContext());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow();
dialog.setContentView(R.layout.yourlayout);
dialog.setTitle("yor title");
dialog.setCancelable(false);
final Button btnOkDialog = (Button) dialog.findViewById(R.id.ResetOkBtn);
btnOkDialog.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
}
});
try {
dialog.show();
} catch (Exception e) {
e.printStackTrace();
}
}
Upvotes: 0
Reputation: 836
First you have to give your TableRow an id on your xml file
<TableRow
android:id="@+id/row1"
...
>
Set the children of your row(in your case a TextView and an EditText probably) to NOT clickable
android:clickable="false"
Now on your java file find your tablerow with the id and add onClickListener
TableRow row1 = findViewById(R.id.row1);
d.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
Now to open a dialog read this
Just a guess. Make the EditText clickable. It might be what you are really looking for
Upvotes: 0