Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Toast message display

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;

enter image description here 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();
    }
});
like image 922
Piraba Avatar asked Oct 06 '11 08:10

Piraba


2 Answers

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);
like image 104
Lalit Poptani Avatar answered Sep 27 '22 19:09

Lalit Poptani


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)

like image 45
Rob Avatar answered Sep 27 '22 18:09

Rob