Reputation: 1071
I have been move the dialog box. Here I am posted my complete source code and Screen shots. Why my dialog box has been lost some part.
Code :
public class MoveDialogBox extends Activity
{
Dialog dialog;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
showDialog(1);
}
});
}
@Override
protected Dialog onCreateDialog( int id )
{
switch ( id )
{
case 1:
AlertDialog.Builder builder = null;
Context mContext = this;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.info, (ViewGroup) findViewById(R.id.root));
Button exit = (Button) layout.findViewById(R.id.exit);
exit.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
dismissDialog(1);
}
});
final LinearLayout LL = (LinearLayout) layout.findViewById(R.id.root);
LL.bringToFront();
LL.setOnTouchListener(new View.OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
double X = event.getX();
double Y = event.getY();
String.valueOf(event.getX() + "x" + String.valueOf(event.getY()));
Toast.makeText(getBaseContext(), X + " : " + Y, Toast.LENGTH_SHORT).show();
LL.bringToFront();
LL.setX((float) X);
LL.setY((float) Y);
return false;
}
});
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
dialog = builder.create();
dialog.setCanceledOnTouchOutside(true);
break;
}
return dialog;
}
}
XML Code : info.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_light"
android:orientation="horizontal" >
<TextView
android:id="@+id/about_Title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:textSize="25dip"
android:textColor="@android:color/white"
android:textStyle="bold|italic"
android:text="Android" />
<Button
android:id="@+id/exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Exit" />
</LinearLayout>
XML Code : main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/androidimage" >
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open Dialog Box" />
</LinearLayout>
Screen Shot : 1
Screen Shot : 2
Screen Shot : 3
Please help some one how do move the dialog box in the entire screen.
Where I am Mistaken. How do solve.
Upvotes: 4
Views: 5230
Reputation: 131
You are moving the layout inside the dialog, you need to move the dialog itself.
WindowManager.LayoutParams layoutParams = dialog.getWindow().getAttributes();
layoutParams.gravity = Gravity.TOP | Gravity.LEFT;
layoutParams.x = youNewX;
layoutParams.y = yourNewY;
dialog.getWindow().setAttributes(layoutParams);
Upvotes: 4