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