I am developing a sample app.I am able to show alert on button click having some tittle and button .But now I want to show a pop up window having username (Label) and text field (Edit field) and a button. on click on button.can I make another popup xml file for that ?
public void selfDestruct(View view) {
// Kabloey
Log.d("Naveen", "Test====");
System.out.println("----------------------ghfgjhf-----------------");
AlertDialog alertDialog = new AlertDialog.Builder(SecondActivity.this).create();
alertDialog.setTitle("Reset...");
alertDialog.setMessage("R u sure?");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//here you can add functions
} });
alertDialog.show();
}
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/self_destruct"
android:onClick="selfDestruct" />
We will use the PopupWindow class to create the popup.
One thing I would like to mention is that we want the popup to be attached to the button that opened it. For example if the “Show Popup” button from the screenshot above would be positioned in the middle of the screen, we want the popup window stick to the button’s position. To achieve this, first we should get the button’s “x” and “y” position on the screen, and pass them to the popup window. Then will we use an offset to align the popup properly – a bit to the right, and a bit down, so it won’t overlap the whole button.
Another think I would like to mention is that we will use a 9 patch background image for the popup, so it will look more fancy. But of course you can skip it and put any background you want, or no background at all.
Create layout/main.xml file and add a button:
Create a new layout file: layout/popup_layout.xml that defines the layout of popup.
And now the most interesting part. Open the TestPopupActivity and fill it with below code. Carefully read the comments to understand what’s going on.
Here is xml code and activity code see it.
Activity file
You can make a normal activity and set the
android:theme
to dialog:and in your
xml
file for this activity you can decide the height and width of this dialog field. For example:The important thing is the manifest part.