Reputation: 7004
I have list of product. When the user click on the row, the Toast
msg will be displayed the name of the product.
I want to display the Toast
on the each product row line.I want to show the Toast in the place where user click on list' row
See my picture;
Currently displaying center of List:
I did like this :
tablerow.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Toast prName = Toast.makeText(RetailerOrderActivity.this,
" Name : " + productList.get(Integer.parseInt(view.getTag().toString())).getDescription(),
Toast.LENGTH_SHORT);
prName.setGravity(Gravity.AXIS_SPECIFIED, 0, 0);
prName.show();
}
});
Upvotes: 5
Views: 5494
Reputation: 67296
Try this,
int[] values= new int[2];
view.getLocationOnScreen(values);
int x = values[0];
int y = values[1] ;
toast.setGravity(Gravity.TOP|Gravity.LEFT, x, y);
Upvotes: 6
Reputation: 25755
I guess you have an onClick
or onItemClick
-listener somewhere. If so, a View
-parameter is passed to it. The passed View
is the view that was clicked (the entry of your list). You can get the Y-position of it by using the getY()
-method.
You should always center the Toast horizontally and let the Toast
decide it's width.
So your code might look something like this (using an onItemClick
-listener):
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Get the Y-position:
int y = (int) view.getY();
// Create the Toast:
Toast toast = Toast.makeText(RetailerOrderActivity.this,
" Name : " + productList.get(Integer.parseInt(view.getTag().toString())).getDescription(),
Toast.LENGTH_SHORT
);
// Set the position:
toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, y);
// Show the Toast:
toast.show();
}
I did not test it, but it should work.
Upvotes: 1
Reputation: 7099
The 2nd and 3rd params of setGravity()
are x and y offsets from the constant specified in the 1st param. You will need to get the x&y position of the row view object you wish to position the toast above. Or you could get the x&y of the touch event.
Personally I would start with setGravity(Gravity.TOP|Gravity.LEFT,0,0)
Upvotes: 1