I'm developing an Android application and I have a question about custom dialogs.
I do this to open a custom dialog:
protected void showSetFriendEmailDialog()
{
// Create the dialog.
final Dialog emailDialog =
new Dialog(FriendHomeActivity.this, android.R.style.Theme_DeviceDefault);
emailDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
emailDialog.setCancelable(true);
emailDialog.setContentView(R.layout.dialog_add_friend_email);
// Get dialog widgets references.
final EditText editFriendsEmail = (EditText)emailDialog.findViewById(R.id.editEmailAddFriendEmail);
Button btnAccept = (Button)emailDialog.findViewById(R.id.btnAddFriendEmail);
// Set on click lister for accept button
btnAccept.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
// Get selected values
String friendEmail =
editFriendsEmail.getText().toString();
// Close dialog.
emailDialog.dismiss();
// TODO: Call api to send email to web service using friendsEmail var.
Log.v(TAG, "Friend email: " + friendEmail);
}
});
//now that the dialog is set up, it's time to show it
emailDialog.show();
}
And this is its layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textAddFriendEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/layout_set_friend_email"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editEmailAddFriendEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textEmailAddress" >
<requestFocus />
</EditText>
<Button
android:id="@+id/btnAddFriendEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ok" />
</LinearLayout>
But it opens full screen.
What do I have to do to open it as an AlertDialog
?
you have to use android.R.style.Theme.Dialog
instenad of android.R.style.Theme_DeviceDefault
Use R.style.Theme_AppCompat_Dialog
in order to get the proper dialog.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With