omega
omega

Reputation: 43913

How to open a dialog box and use a layout to show it?

In android programming, I want to open a pop up box which has the title "Login". The contents should be like:

Login

Username
[________] (input field)

Password
[________] (password field)

[Cancel] [Login]

But I want to show this using a layout file. I don't want to add all this programatically.

Can anyone show me an example of how to do this?

Thanks

Upvotes: 0

Views: 1623

Answers (1)

Vikram
Vikram

Reputation: 51581

Here are the steps you need to follow:

-=- Create a xml layout file (say, "my_popup_window.xml"). Using the information you provided, this can be:-

<LinearLayout(Vertical)>

    <TextView("Login") />

    <TextView("Username") />

    <EditText />

    <TextView("Password") />

    <EditText />

    <LinearLayout>

        <Button("Cancel") />

        <Button("Login") />

    </LinearLayout>

</LinearLayout>

-=- In your activity, create a method "showPopupWindow()":

void showPopupWindow() { 
    // inflate your layout
    View myPopupView = getLayoutInflater().inflate(R.layout.my_popup_window, null);

    // Create the popup window; decide on the layout parameters
    PopupWindow myPopupWindow = new PopupWindow(myPopupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    // find and initialize your TextView(s), EditText(s) and Button(s); setup their behavior

    // display your popup window
    myPopupWindow.showAtLocation(myPopupView, Gravity.CENTER, 0, 0);
}

Call this method when you need to show this popup window.

Upvotes: 1

Related Questions