Android Custom Dialog
I'm trying to make a custom dialog, following the tutorial on the Android developer site, but it crashes every time I try to show the dialog. Here's my code:
Context mContext = getApplicationContext();
Dialog dialog = new Dialog(mContext);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Custom Dialog");
dialog.show();
And here's my XML for the layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
andr开发者_运维技巧oid:id="@+id/btnConfirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add text"
android:layout_below="@+id/txtNewText"
android:layout_alignParentLeft="true">
</Button>
<EditText
android:id="@+id/txtNewText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true">
</EditText>
</RelativeLayout>
Consider the pattern:
private static final int MY_DIALOG= 0;
protected Dialog onCreateDialog(int id) {
Dialog dialog;
switch(id) {
case MY_DIALOG:
dialog= getInstanceMyDialog();
break;
default:
dialog = null;
}
return dialog;
}
private Dialog getInstanceMyDialog() {
final Dialog d= new Dialog(this); //<=====THIS
d.setContentView(R.layout.custom_dialog);
d.setTitle("Custom Dialog");
return d;
}
JAL
This worked for me: problem-creating-a-custom-dialog
Use this instead of getApplicationContext() when instantiating the dialog:
Dialog dialog = new Dialog(this);
Kotlin way to create custom dialog in android:
Dialog(activity!!, R.style.LoadingIndicatorDialogStyle)
.apply {
// requestWindowFeature(Window.FEATURE_NO_TITLE)
setCancelable(true)
setContentView(R.layout.define_your_custom_view_id_here)
//access your custom view buttons/editText like below.z
val createBt = findViewById<TextView>(R.id.clipboard_create_project)
val cancelBt = findViewById<TextView>(R.id.clipboard_cancel_project)
val clipboard_et = findViewById<TextView>(R.id.clipboard_et)
val manualOption =
findViewById<TextView>(R.id.clipboard_manual_add_project_option)
//if you want to perform any operation on the button do like this
createBt.setOnClickListener {
//handle your button click here
val enteredData = clipboard_et.text.toString()
if (enteredData.isEmpty()) {
Utils.toast("Enter project details")
} else {
navigateToAddProject(enteredData, true)
dismiss()
}
}
cancelBt.setOnClickListener {
dismiss()
}
manualOption.setOnClickListener {
navigateToAddProject("", false)
dismiss()
}
show()
}
Create LoadingIndicatorDialogStyle in style.xml
<style name="LoadingIndicatorDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:statusBarColor">@color/black_transperant</item>
<item name="android:layout_gravity">center</item>
<item name="android:background">@android:color/transparent</item>
<!--<item name="android:windowAnimationStyle">@style/MaterialDialogSheetAnimation</item>-->
精彩评论