0

So far, I have tried displaying custom dialog at specific position on the screen using below code :

    Dialog dlg = new Dialog(MainActivity.this);
    dlg.setContentView(R.layout.customlayout)
    Window window = dlg.getWindow();
    window.setLayout(200, 200);
    WindowManager.LayoutParams wlp = window.getAttributes();
    wlp.x = 20;
    wlp.y = 160;
    wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
    window.setAttributes(wlp);
    dlg.show();

Now, the problem is that the Position of the dialog not remains same in different android devices due to I set it using :

 wlp.x = 20;
 wlp.y = 160;

So, the question is to set dialog below specific control, in my case below particular textview. Is there any property available for WindowManager ?

Or Any other solution ?

EDITED AS BELOW ACCORDING TO COMMENT:

public class MainActivity extends AppCompatActivity {
private TextView txt1;
private int location[] = new int[2];

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    txt1 = (TextView) findViewById(R.id.mtxt);



    txt1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showPictureialog();
        }
    });


}

private void showPictureialog() {
    Dialog dlg = new Dialog(MainActivity.this);
    dlg.setContentView(R.layout.customlayout);
    Window window = dlg.getWindow();
    window.setLayout(200, 200);
    WindowManager.LayoutParams wlp = window.getAttributes();
    txt1.getLocationOnScreen(location);

    wlp.x = location[0];
    wlp.y = location[1];
    wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
    window.setAttributes(wlp);
    dlg.show();
    Toast.makeText(MainActivity.this, "X : " + location[0], Toast.LENGTH_SHORT).show();
    Toast.makeText(MainActivity.this, "Y : " + location[1], Toast.LENGTH_SHORT).show();
}

}

Jaimin Modi
  • 124
  • 1
  • 15
  • Use the position of the `TextView` to configure your dialog's `x` and `y` parameters? Have a look at [`getLocationInWindow()`](http://developer.android.com/intl/es/reference/android/view/View.html#getLocationInWindow%28int%5B%5D%29) and [`getLocationOnScreen()`](http://developer.android.com/intl/es/reference/android/view/View.html#getLocationOnScreen%28int%5B%5D%29). – MH. Nov 19 '15 at 08:19
  • I have tried but the dialog is displayed at bottom-right corner of screen. Why ? – Jaimin Modi Nov 19 '15 at 09:07
  • @MH. Please check, I have edited my question. – Jaimin Modi Nov 19 '15 at 09:11
  • 1
    Could be because of `window.setLayout(200, 200)`, could be because of an implicit (center) gravity. Try commenting out that line first. Then try setting an explicit gravity (top|left). What values does your toast show by the way? And what are you displaying in your dialog? Using a `PopupMenu` or `PopupWindow` may be more appropriate/straightforward. – MH. Nov 19 '15 at 09:18
  • X : 288 , Y : 654 .. I have done window.setLayout(200,200) for customizing size of dialog. My dialog just displaying a textview. – Jaimin Modi Nov 19 '15 at 09:29
  • 1
    Those values look okay to me. Have you tried not setting the window size? Have you considered using a [`PopupWindow`](http://developer.android.com/intl/es/reference/android/widget/PopupWindow.html)? That appears more appropriate for what you're attempting to accomplish. It provides various options to anchor the popup (and its content) to a specific view or display it at a specific location out-of-the-box. – MH. Nov 19 '15 at 10:42
  • Hey MH, thanks for suggesting me about PopUpMenu. Its works fine now. – Jaimin Modi Nov 19 '15 at 11:22

0 Answers0