Display Messages for Android Toast

I have a list of products. When the user clicks on the line, the Toastproduct name is displayed on msg.

I want to display Toastproduct lines on each line. I want to show Toast where the user clicks on the list bar

See my photo;

enter image description here The center of the list is currently displayed:

I like it:

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();
    }
});
+5
source share
3 answers

Try it,

int[] values= new int[2];
view.getLocationOnScreen(values);
int x = values[0];
int y = values[1] ;

toast.setGravity(Gravity.TOP|Gravity.LEFT, x, y);
+6
source

setGravity() - x y , . x & y , . x & y .

setGravity(Gravity.TOP|Gravity.LEFT,0,0)

+1

, onClick onItemClick -listener. , View -. View - , ( ). Y-, getY().

Toast .

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

, .

+1

All Articles