1

this is my main point of confusion:

windowPopUp.showAtLocation(descroot, Gravity.NO_GRAVITY, 0, loc[1]+tv.getHeight()-tv.getPaddingTop());

I am passing 0 as the left coordinate, but this code aligns the popup perfectly in center, however I thought the correct value to be passed should be

int left = (getResources().getDisplayMetrics().widthPixels - windowPopUp.getWidth())/2;

but this doesn't work this pushes the window more towards right, can you point me out if I have done anything wrong? or if 0 is correct value to pass, can you justify why?

Below is my whole code:

tc = new TextView(c);
tc.setBackgroundResource(R.drawable.bg_training_baloon_hc_2x);
tc.setPadding(toPix(20), toPix(20), toPix(20), toPix(20));
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
tc.setLayoutParams(params);

final PopupWindow windowPopUp = new PopupWindow(tc,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT ,false);
final View descriptiontv = (View) findViewById(R.id.descriptiontv);     

windowPopUp.setWidth(getResources().getDisplayMetrics().widthPixels-toPix(40));
descriptiontv.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int code = event.getAction();
        switch (code) {

        case MotionEvent.ACTION_DOWN:
            descriptiontv.setBackgroundResource(R.drawable.button_pressed);
            break;
        case MotionEvent.ACTION_UP:
            descriptiontv.setBackgroundResource(R.drawable.button_normal);
            {
                if (!windowPopUp.isShowing()) {
                    LinearLayout descroot = (LinearLayout) findViewById(R.id.descroot);
                    TextView tv = (TextView) findViewById(R.id.descriptiontv);
                    int[] loc = new int[2];
                    tv.getLocationOnScreen(loc);
                    windowPopUp.showAtLocation(descroot, Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, loc[1] + tv.getHeight() - tv.getPaddingBottom());
                } else {
                    windowPopUp.dismiss();
                }
                Log.d("TrainingScreen----->", "description clicked");
            }
            break;
        default:
            break;
        }
        return true;
    }
});
4

0 回答 0