3

How to Display dialog box in the top right corner in android activity.

Below is my code for dialog box

item.xml

 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="150dp"
    android:layout_height="wrap_content" >

    <com.CustomToggleButton
        android:id="@+id/toggleButton2"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:textOff="@string/off_txt"
        android:textOn="@string/on_txt" />

</TableLayout>

ActivityMy.java

 dialog_box = new Dialog(ActivityMy.this);
                        dialog_box.setContentView(R.layout.item);
                        dialog_box.show();
Lokesh
  • 5,180
  • 4
  • 27
  • 42
eswaat
  • 733
  • 1
  • 13
  • 31

2 Answers2

7

Do like this -

Window window = dialog.getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();
wlp.gravity = Gravity.TOP | Gravity.RIGHT;

wlp.width = LayoutParams.MATCH_PARENT;
wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
window.setAttributes(wlp);
ppreetikaa
  • 1,149
  • 2
  • 15
  • 22
Imtiyaz Khalani
  • 2,037
  • 18
  • 32
2

If you want give some margin then use

Window window = dialog.getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();
wlp.gravity = Gravity.TOP | Gravity.RIGHT;
wlp.width = LayoutParams.MATCH_PARENT;
wlp.y=100   //if you want give margin from top
wlp.x=100     //if you want give margin from left
wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
window.setAttributes(wlp);
ppreetikaa
  • 1,149
  • 2
  • 15
  • 22
Jatin Bansal
  • 123
  • 8