customdialog https://www.e-learn.cn/tag/customdialog zh-hans Custom AlertDialog Borders https://www.e-learn.cn/topic/3219704 <span>Custom AlertDialog Borders</span> <span><span lang="" about="/user/151" typeof="schema:Person" property="schema:name" datatype="">时间秒杀一切</span></span> <span>2020-01-15 01:20:31</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I am creating a custom dialog. Its example code is:</p> <pre><code>final AlertDialog dialog; protected AlertDialog createDialog(int dialogId) { AlertDialog.Builder builder; builder = new AlertDialog.Builder(parent); AlertDialog fDialog = null; switch(dialogId) { case Constants.cusDialogtId: builder = new AlertDialog.Builder(parent); builder.setTitle("Title"); LayoutInflater inflater = (LayoutInflater)parent.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.customdialog, null); builder.setView(view); fDialog = builder.create(); break; } dialog = fDialog; return dialog; } </code></pre> <p>The problem is that when the dialog is shown, it has a gray background of the native dialog whose some top and bottom border is also shown with my custom dialog. Is there some way to show only my custom dialog view...???</p> <p>The XML I am using is:</p> <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:layout_width="fill_parent" android:orientation="vertical" android:background="@drawable/bgsmall" &gt; &lt;EditText android:id="@+id/redeemamount" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10dip" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:hint="Enter amount" android:inputType="numberDecimal"&gt; &lt;/EditText&gt; &lt;Button android:id="@+id/submitRedeemAmountButton" android:layout_width="fill_parent" android:layout_height="40dip" android:text="Submit" android:textColor="#FFFFFF" android:textStyle="bold" android:background="@drawable/buttoncorner" android:layout_marginTop="20dip" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:layout_marginBottom="20dip"&gt; &lt;/Button&gt; &lt;/LinearLayout&gt; </code></pre> <br /><h3>回答1:</h3><br /><p>I don't think you can remove the borders by using <code>AlertDialog.Builder</code>.</p> <p>What you can do is create a <code>CustomDialog</code> class that extends <code>Dialog</code> and in the constructor of your <code>CustomDialog</code> you inflate your <code>customdialog.xml</code>.</p> <p>Also you will need to create a custom <code>style</code> for your dialog, that hides the borders. Here is an example:</p> <pre><code> &lt;style name="CustomStyle" parent="android:Theme.Dialog"&gt; &lt;item name="android:windowBackground"&gt;@color/transparent&lt;/item&gt; &lt;item name="android:windowContentOverlay"&gt;@null&lt;/item&gt; &lt;/style&gt; </code></pre> <p>Also define the transparent color:</p> <pre><code> &lt;color name="transparent"&gt;#00000000&lt;/color&gt; </code></pre> <p>And you will create your dialog using :</p> <pre><code> CustomDialog dialog=new CustomDialog(this,R.style.CustomStyle); </code></pre> <br /><br /><br /><h3>回答2:</h3><br /><p>Create a custom theme:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;resources&gt; &lt;style name="CustomDialog" parent="android:style/Theme.Dialog"&gt; &lt;item name="android:windowBackground"&gt;@null&lt;/item&gt; &lt;/style&gt; &lt;/resources&gt; </code></pre> <p>then use it:</p> <pre><code>builder = new AlertDialog.Builder(parent, R.style.CustomDialog); </code></pre> <p><strong>Update</strong></p> <p>The constructor above is indeed API 11+. To work around this you need to extend AlertDialog (since its constructors are <code>protected</code>) and and then use constructor with theme parameter. To insert your custom view follow the instructions here - the <code>FrameLayout</code> trick described at the beginning.</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/7442450/custom-alertdialog-borders</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/android" hreflang="zh-hans">android</a></div> <div class="field--item"><a href="/tag/customdialog" hreflang="zh-hans">customdialog</a></div> </div> </div> Tue, 14 Jan 2020 17:20:31 +0000 时间秒杀一切 3219704 at https://www.e-learn.cn Why I am not able to see proper custom dialog Box in Motorola Atrix Emulator? https://www.e-learn.cn/topic/3204771 <span>Why I am not able to see proper custom dialog Box in Motorola Atrix Emulator?</span> <span><span lang="" about="/user/92" typeof="schema:Person" property="schema:name" datatype="">可紊</span></span> <span>2020-01-13 19:36:13</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I have created Custom Dialog for my application. While i run that application in normal Samsung Galary Ace then it shows proper in that device but while i am going to run that app in Motorola Atrix then the Custom Dialog Box not seen proper with the border. . . Please see the Image of Motorola Atrix Emulator with my Custom Dialog in it. </p><p></p><img class="b-lazy" data-src="https://i0.wp.com/i.stack.imgur.com/imkfN.png" data-original="https://i0.wp.com/i.stack.imgur.com/imkfN.png" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><p></p> <p>Should i have to remove the border of the custom dialog or what else i have to do to see only my custom dialog in any device ? If i have to remove the border from the custom dialog then let me know how ??</p> <p>Thanks.</p> <br /><h3>回答1:</h3><br /><p>I know this has been answered but here is how I did it...</p> <p>I saw this on all my Motorola phones X2, Razr...Seems to definitely be a bug in the styles for Motorola. </p> <p>I fixed it by creating my own style and copying panel_background from my \android-sdk\platforms\android-10\data\res\drawable-hdpi and placing it in my drawable. Eclipse wouldn't compile if I referenced it using @android:drawable/panel_background.</p> <p>styles.xml</p> <pre><code>&lt;style name="Theme.CustomDialog" parent="@android:style/Theme.Dialog"&gt; &lt;item name="android:windowBackground"&gt;@drawable/panel_background&lt;/item&gt; &lt;item name="android:windowIsFloating"&gt;true&lt;/item&gt; &lt;item name="android:windowContentOverlay"&gt;@null&lt;/item&gt; &lt;item name="android:windowAnimationStyle"&gt;@android:style/Animation.Dialog&lt;/item&gt; &lt;item name="android:windowSoftInputMode"&gt;stateUnspecified|adjustPan&lt;/item&gt; &lt;/style&gt; </code></pre> <p>Then just call the dialog with the Theme parameter added</p> <pre><code>Dialog dialog = new Dialog(this, R.style.Theme_CustomDialog); </code></pre> <p>There fixed Motorola issues!</p> <br /><br /><br /><h3>回答2:</h3><br /><p>Yes you can try by removing border.And set <code>requestWindowFeature(Window.FEATURE_NO_TITLE);</code> And make sure that you are using a different class for dialog by extending it<code>.fix the height and with for your dialog and then try</code></p> <p>But as you said it looks different in only Motorola device.Then its difficult to tell what's going wrong. The important thing i want to share with you.I was developing application for Motorola milestone.After completing it i installed in Tablet.Then dialog size changes and its appearance too. And this was not only with custom dialog but Progress Dialog in which no properties was set, changes.Hope you got my point.Finally i want to say dialog behave sometimes unexpectedly.</p> <p><strong>Edited</strong></p> <p>Create a dialog class and its layout</p> <pre><code>public class DisplayDialog extends Dialog implements { private ImageButton cancel,submit; private Context context; private ProgressDialog pd; public DisplayDialog(Context c) { super(c, R.style.Theme_Dialog_Translucent); context = c; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setCanceledOnTouchOutside(false); setContentView(R.layout.dialog); email_id = (EditText) findViewById(R.id.email_id); cancel = (ImageButton) findViewById(R.id.btn_cancel); cancel.setOnClickListener(this); } </code></pre> <p>}</p> <p>Then from activity just make its object and call it where ever you want</p> <pre><code>DisplayDialog dd=new DisplayDialog(this); dd.show() </code></pre> <br /><br /><br /><h3>回答3:</h3><br /><p>The thing is that there are some troubles in Motorola styles, that override default Android styles. So, you should just override this styles once again.</p> <p>As for me, I created my own theme and overrided <code>android:windowBackground</code> parameter. You can use your own background image, but I just took default image from Android resources. The resulting style looked like this:</p> <pre><code>&lt;style name="Theme.GreenDialog" parent="@android:style/Theme.Dialog"&gt; &lt;item name="android:windowBackground"&gt;@drawable/panel_background&lt;/item&gt; &lt;/style&gt; </code></pre> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/8488128/why-i-am-not-able-to-see-proper-custom-dialog-box-in-motorola-atrix-emulator</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/android" hreflang="zh-hans">android</a></div> <div class="field--item"><a href="/tag/android-layout" hreflang="zh-hans">android-layout</a></div> <div class="field--item"><a href="/tag/android-emulator" hreflang="zh-hans">android-emulator</a></div> <div class="field--item"><a href="/tag/dialog" hreflang="zh-hans">dialog</a></div> <div class="field--item"><a href="/tag/customdialog" hreflang="zh-hans">customdialog</a></div> </div> </div> Mon, 13 Jan 2020 11:36:13 +0000 可紊 3204771 at https://www.e-learn.cn Why I am not able to see proper custom dialog Box in Motorola Atrix Emulator? https://www.e-learn.cn/topic/3204760 <span>Why I am not able to see proper custom dialog Box in Motorola Atrix Emulator?</span> <span><span lang="" about="/user/51" typeof="schema:Person" property="schema:name" datatype="">大兔子大兔子</span></span> <span>2020-01-13 19:36:07</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I have created Custom Dialog for my application. While i run that application in normal Samsung Galary Ace then it shows proper in that device but while i am going to run that app in Motorola Atrix then the Custom Dialog Box not seen proper with the border. . . Please see the Image of Motorola Atrix Emulator with my Custom Dialog in it. </p><p></p><img class="b-lazy" data-src="https://i0.wp.com/i.stack.imgur.com/imkfN.png" data-original="https://i0.wp.com/i.stack.imgur.com/imkfN.png" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><p></p> <p>Should i have to remove the border of the custom dialog or what else i have to do to see only my custom dialog in any device ? If i have to remove the border from the custom dialog then let me know how ??</p> <p>Thanks.</p> <br /><h3>回答1:</h3><br /><p>I know this has been answered but here is how I did it...</p> <p>I saw this on all my Motorola phones X2, Razr...Seems to definitely be a bug in the styles for Motorola. </p> <p>I fixed it by creating my own style and copying panel_background from my \android-sdk\platforms\android-10\data\res\drawable-hdpi and placing it in my drawable. Eclipse wouldn't compile if I referenced it using @android:drawable/panel_background.</p> <p>styles.xml</p> <pre><code>&lt;style name="Theme.CustomDialog" parent="@android:style/Theme.Dialog"&gt; &lt;item name="android:windowBackground"&gt;@drawable/panel_background&lt;/item&gt; &lt;item name="android:windowIsFloating"&gt;true&lt;/item&gt; &lt;item name="android:windowContentOverlay"&gt;@null&lt;/item&gt; &lt;item name="android:windowAnimationStyle"&gt;@android:style/Animation.Dialog&lt;/item&gt; &lt;item name="android:windowSoftInputMode"&gt;stateUnspecified|adjustPan&lt;/item&gt; &lt;/style&gt; </code></pre> <p>Then just call the dialog with the Theme parameter added</p> <pre><code>Dialog dialog = new Dialog(this, R.style.Theme_CustomDialog); </code></pre> <p>There fixed Motorola issues!</p> <br /><br /><br /><h3>回答2:</h3><br /><p>Yes you can try by removing border.And set <code>requestWindowFeature(Window.FEATURE_NO_TITLE);</code> And make sure that you are using a different class for dialog by extending it<code>.fix the height and with for your dialog and then try</code></p> <p>But as you said it looks different in only Motorola device.Then its difficult to tell what's going wrong. The important thing i want to share with you.I was developing application for Motorola milestone.After completing it i installed in Tablet.Then dialog size changes and its appearance too. And this was not only with custom dialog but Progress Dialog in which no properties was set, changes.Hope you got my point.Finally i want to say dialog behave sometimes unexpectedly.</p> <p><strong>Edited</strong></p> <p>Create a dialog class and its layout</p> <pre><code>public class DisplayDialog extends Dialog implements { private ImageButton cancel,submit; private Context context; private ProgressDialog pd; public DisplayDialog(Context c) { super(c, R.style.Theme_Dialog_Translucent); context = c; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setCanceledOnTouchOutside(false); setContentView(R.layout.dialog); email_id = (EditText) findViewById(R.id.email_id); cancel = (ImageButton) findViewById(R.id.btn_cancel); cancel.setOnClickListener(this); } </code></pre> <p>}</p> <p>Then from activity just make its object and call it where ever you want</p> <pre><code>DisplayDialog dd=new DisplayDialog(this); dd.show() </code></pre> <br /><br /><br /><h3>回答3:</h3><br /><p>The thing is that there are some troubles in Motorola styles, that override default Android styles. So, you should just override this styles once again.</p> <p>As for me, I created my own theme and overrided <code>android:windowBackground</code> parameter. You can use your own background image, but I just took default image from Android resources. The resulting style looked like this:</p> <pre><code>&lt;style name="Theme.GreenDialog" parent="@android:style/Theme.Dialog"&gt; &lt;item name="android:windowBackground"&gt;@drawable/panel_background&lt;/item&gt; &lt;/style&gt; </code></pre> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/8488128/why-i-am-not-able-to-see-proper-custom-dialog-box-in-motorola-atrix-emulator</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/android" hreflang="zh-hans">android</a></div> <div class="field--item"><a href="/tag/android-layout" hreflang="zh-hans">android-layout</a></div> <div class="field--item"><a href="/tag/android-emulator" hreflang="zh-hans">android-emulator</a></div> <div class="field--item"><a href="/tag/dialog" hreflang="zh-hans">dialog</a></div> <div class="field--item"><a href="/tag/customdialog" hreflang="zh-hans">customdialog</a></div> </div> </div> Mon, 13 Jan 2020 11:36:07 +0000 大兔子大兔子 3204760 at https://www.e-learn.cn How to align custom dialog centre in android ? https://www.e-learn.cn/topic/3199457 <span>How to align custom dialog centre in android ?</span> <span><span lang="" about="/user/62" typeof="schema:Person" property="schema:name" datatype="">喜夏-厌秋</span></span> <span>2020-01-13 09:37:33</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I am working on the application where i wanted to display the dialog to be screen size. So i used code below.I got the solution through here Alert message is not displaying in alert dialog box?</p> <pre><code> AlertDialog.Builder builder = new AlertDialog.Builder(this); TextView title = new TextView(this); title.setText("DM2"); title.setBackgroundColor(Color.DKGRAY); title.setPadding(10, 10, 10, 10); title.setGravity(Gravity.CENTER); title.setTextColor(Color.WHITE); title.setTextSize(20); TextView text = new TextView(this); text.setText("Hello This text"); text.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); text.setTextSize(20); text.setGravity(Gravity.CENTER); //Creates a linearlayout layout and sets it with initial params LinearLayout ll = new LinearLayout(this); ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); ll.setGravity(Gravity.CENTER); ll.addView(text); builder.setCustomTitle(title); builder.setPositiveButton( "Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); } }); Dialog d = builder.setView(ll).create(); //Fills up the entire Screen WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); lp.copyFrom(d.getWindow().getAttributes()); lp.width = WindowManager.LayoutParams.FILL_PARENT; lp.height = WindowManager.LayoutParams.FILL_PARENT; d.show(); d.getWindow().setAttributes(lp); </code></pre> <p>But I want dialog to be appeared in center aligned. Now it is display in top of the window. I tried with lp.garvity = Gravity.center, but didn't work. and If the device orientation changes, i need to press ok button more time to close the dialog. How to fix this?</p> <p>Thanks in advance Pushpa</p> <br /><h3>回答1:</h3><br /><p>I'm little late, but better late then never :-) you can use the code below: (I saw also here)</p> <pre><code>dialog = new Dialog(activity, android.R.style.Theme_Translucent_NoTitleBar); dialog.setContentView(R.layout.activity_upload_photo_dialog); Window window = dialog.getWindow(); window.setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); window.setGravity(Gravity.CENTER); //The below code is EXTRA - to dim the parent view by 70% LayoutParams lp = window.getAttributes(); lp.dimAmount = 0.7f; lp.flags = LayoutParams.FLAG_DIM_BEHIND; dialog.getWindow().setAttributes(lp); //Show the dialog dialog.show(); </code></pre> <p>Hope I helped...</p> <br /><br /><br /><h3>回答2:</h3><br /><p>This works for me:</p> <pre><code>lp.height = WindowManager.LayoutParams.WRAP_CONTENT; </code></pre> <br /><br /><br /><h3>回答3:</h3><br /><p>Maybe this works: </p> <pre><code>WindowManager.LayoutParams WMLP = dialog.getWindow().getAttributes(); WMLP.gravity = Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL dialog.getWindow().setAttributes(WMLP); </code></pre> <br /><br /><br /><h3>回答4:</h3><br /><p>Try to use android:layout_gravity="center" instead of android:gravity="center"... It may be helpful for you..</p> <pre><code>android:layout_gravity="center" </code></pre> <p>or</p> <p>you can try this..</p> <pre><code> &lt;activity android:theme="@android:style/Theme.Dialog" android:name=".Splash"&gt; &lt;/activity&gt; </code></pre> <blockquote> <p>Write full xml code and make simple Activty and add one attribute in AndroidManifest.xml, when you registering your Activity into AndroidManifest.xml</p> </blockquote> <br /><br /><br /><h3>回答5:</h3><br /><p><strong>Note</strong> :- This is for "<strong>custom layout</strong>"... :)</p> <p>I was struggling with the same, and after struggling for hours i came to this result </p> <p>Earlier i was creating the dilaoge like this : -</p> <pre><code> final Dialog dialog = new Dialog(PopupforeditActivity.this); </code></pre> <p>After adding style "No titleBar" it started fetching the layout the way I had created.</p> <p>For this you have to create the layout with params "fillParent" and create the dialoge like this</p> <pre><code> final Dialog dialog = new Dialog(PopupforeditActivity.this,android.R.style.Theme_Translucent_NoTitleBar_Fullscreen); dialog.setContentView(R.layout.dialog_billing_cart); dialog.setCancelable(true); </code></pre> <p>The above is coding part</p> <p>You in <code>Layout</code> part put your whole layout in <code>RelativeLayout</code></p> <p>and give your layout property <code>android:layout_centerInParent="true"</code></p> <p>Ex:-</p> <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@android:color/transparent" &gt; &lt;!--Your Layout--&gt; &lt;ScrollView android:id="@+id/scrlView_FormHolder" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_marginBottom="10dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" &gt; &lt;/ScrollView&gt; &lt;/RelativeLayout&gt; </code></pre> <p>And if you don't want to miss "title" You can have that in your custom layout as TextView .. </p> <p>Hope it will help.. As it helped me :) </p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/10447410/how-to-align-custom-dialog-centre-in-android</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/android" hreflang="zh-hans">android</a></div> <div class="field--item"><a href="/tag/android-alertdialog" hreflang="zh-hans">android-alertdialog</a></div> <div class="field--item"><a href="/tag/customdialog" hreflang="zh-hans">customdialog</a></div> </div> </div> Mon, 13 Jan 2020 01:37:33 +0000 喜夏-厌秋 3199457 at https://www.e-learn.cn Easiest way to create a custom dialog box which returns a value? https://www.e-learn.cn/topic/2975679 <span>Easiest way to create a custom dialog box which returns a value?</span> <span><span lang="" about="/user/187" typeof="schema:Person" property="schema:name" datatype="">允我心安</span></span> <span>2019-12-28 12:01:54</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I want to create a custom dialog box for my C# project .. I want to have a DataGridView in this custom dialog box, and there will also be a button .. When the user clicks this button, an integer value is returned to the caller, and the dialog box then terminates itself ..</p> <p>How can I achieve this ?</p> <br /><h3>回答1:</h3><br /><p>There is no prompt dialog box in C#. You can create a custom prompt box to do this instead. </p> <pre><code> public static class Prompt { public static int ShowDialog(string text, string caption) { Form prompt = new Form(); prompt.Width = 500; prompt.Height = 100; prompt.Text = caption; Label textLabel = new Label() { Left = 50, Top=20, Text=text }; NumericUpDown inputBox = new NumericUpDown () { Left = 50, Top=50, Width=400 }; Button confirmation = new Button() { Text = "Ok", Left=350, Width=100, Top=70 }; confirmation.Click += (sender, e) =&gt; { prompt.Close(); }; prompt.Controls.Add(confirmation); prompt.Controls.Add(textLabel); prompt.Controls.Add(inputBox); prompt.ShowDialog(); return (int)inputBox.Value; } } </code></pre> <p>Then call it using:</p> <pre><code> int promptValue = Prompt.ShowDialog("Test", "123"); </code></pre> <br /><br /><br /><h3>回答2:</h3><br /><ol><li>On your button set the DialogResult property to DialogResult.OK</li> <li>On your dialog set the AcceptButton property to your button</li> <li>Create a public property in your form called Result of int type</li> <li>Set the value of this property in the click event of your button</li> <li><p>Call your dialog in this way </p> <pre><code>using(myDialog dlg = new myDialog()) { if(dlg.ShowDialog() == DialogResult.OK) { int result = dlg.Result; // whatever you need to do with result } } </code></pre></li> </ol><br /><br /><br /><h3>回答3:</h3><br /><pre><code>public static DialogResult InputBox(string title, string promptText, ref string value,bool isDigit=false) { Form form = new Form(); Label label = new Label(); TxtProNet textBox = new TxtProNet(); if (isDigit == true) textBox.TypeNumricOnly = true; textBox.Width = 1000; Button buttonOk = new Button(); Button buttonCancel = new Button(); form.Text = title; label.Text = promptText; textBox.Text = value; buttonOk.Text = "OK"; buttonCancel.Text = "Cancel"; buttonOk.DialogResult = DialogResult.OK; buttonCancel.DialogResult = DialogResult.Cancel; label.SetBounds(9, 20, 372, 13); textBox.SetBounds(12, 36, 372, 20); buttonOk.SetBounds(228, 72, 75, 23); buttonCancel.SetBounds(309, 72, 75, 23); label.AutoSize = true; textBox.Anchor = textBox.Anchor | AnchorStyles.Right; buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; form.ClientSize = new Size(396, 107); form.Controls.AddRange(new Control[] { label, textBox, buttonOk, buttonCancel }); form.ClientSize = new Size(Math.Max(300, label.Right + 10), form.ClientSize.Height); form.FormBorderStyle = FormBorderStyle.FixedDialog; form.StartPosition = FormStartPosition.CenterScreen; form.MinimizeBox = false; form.MaximizeBox = false; form.AcceptButton = buttonOk; form.CancelButton = buttonCancel; DialogResult dialogResult = form.ShowDialog(); value = textBox.Text; return dialogResult; } </code></pre> <br /><br /><br /><h3>回答4:</h3><br /><p>One uses the MessageBox object for this type of behavior. In code, if you asked a Yes/No question to a user, it looks like this.</p> <p>In the example, I'm asking the user if he or she wishes to save their changed document in a method called <em>AskToSave()</em>.</p> <pre><code>private void AskToSave() { DialogResult result = MessageBox.Show("Save Document?", "Wait!",MessageBoxButtons.YesNo, MessageBoxIcon.Question); if(result == DialogResult.No) { return; } else { SaveDocument(); } } </code></pre> <br /><br /><br /><h3>回答5:</h3><br /><pre><code>//combo box dialog c# // public static string DialogCombo(string text,DataTable comboSource,string DisplyMember,string ValueMember) { //comboSource = new DataTable(); Form prompt = new Form(); prompt.RightToLeft = RightToLeft.Yes; prompt.Width = 500; prompt.Height = 200; Label textLabel = new Label() { Left = 350, Top = 20, Text = text }; ComboBox combo = new ComboBox { Left = 50, Top = 50, Width = 400 }; combo.DataSource = comboSource; combo.ValueMember = ValueMember; combo.DisplayMember = DisplyMember; Button confirmation = new Button() { Text = "تایید", Left = 350, Width = 100, Top = 70 }; confirmation.Click += (sender, e) =&gt; { prompt.Close(); }; prompt.Controls.Add(confirmation); prompt.Controls.Add(textLabel); prompt.Controls.Add(combo); prompt.ShowDialog(); return combo.SelectedValue.ToString(); } </code></pre> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/9569489/easiest-way-to-create-a-custom-dialog-box-which-returns-a-value</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/c" hreflang="zh-hans">c#</a></div> <div class="field--item"><a href="/tag/winforms" hreflang="zh-hans">winforms</a></div> <div class="field--item"><a href="/tag/dialog" hreflang="zh-hans">dialog</a></div> <div class="field--item"><a href="/tag/customdialog" hreflang="zh-hans">customdialog</a></div> </div> </div> Sat, 28 Dec 2019 04:01:54 +0000 允我心安 2975679 at https://www.e-learn.cn Android custom dialog with MVVM https://www.e-learn.cn/topic/2950555 <span>Android custom dialog with MVVM</span> <span><span lang="" about="/user/54" typeof="schema:Person" property="schema:name" datatype="">↘锁芯ラ</span></span> <span>2019-12-25 18:57:22</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I am creating custom dialog and i want when the user click add button go and call retrofit and observe on changes but i don't know how to pass lifecycleowner to the observer</p> <pre><code> private void observeViewModel(ProjectListViewModel viewModel) { // Update the list when the data changes viewModel.getProjectListObservable().observe( ***what to pass here ??*** , new Observer&lt;List&lt;Project&gt;&gt;() { @Override public void onChanged(@Nullable List&lt;Project&gt; projects) { if (projects != null) { //… projectAdapter.setProjectList(projects); } } }); </code></pre> <p>thanks in advance</p> <br /><h3>回答1:</h3><br /><p>Try this solution. It worked for me. </p> <p>Create a field of your activity from where you are calling the dialog and pass this in place of lifecycleowner</p> <pre><code>public class YourDialog extends DialogFragment { private YourActivity activity; public static YourDialog newInstance(YourActivity activity) { YourDialog dialog = new YourDialog(); dialog.activity = activity; return dialog; } private void observeViewModel(ProjectListViewModel viewModel) { // Update the list when the data changes viewModel.getProjectListObservable().observe( activity , new Observer&lt;List&lt;Project&gt;&gt;() { @Override public void onChanged(@Nullable List&lt;Project&gt; projects) { if (projects != null) { //… projectAdapter.setProjectList(projects); } } }); } </code></pre> <p>You can refer the example of mvvm here if you want </p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/49878279/android-custom-dialog-with-mvvm</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/android" hreflang="zh-hans">android</a></div> <div class="field--item"><a href="/tag/mvvm" hreflang="zh-hans">mvvm</a></div> <div class="field--item"><a href="/tag/retrofit" hreflang="zh-hans">retrofit</a></div> <div class="field--item"><a href="/tag/customdialog" hreflang="zh-hans">customdialog</a></div> <div class="field--item"><a href="/tag/android-livedata" hreflang="zh-hans">android-livedata</a></div> </div> </div> Wed, 25 Dec 2019 10:57:22 +0000 ↘锁芯ラ 2950555 at https://www.e-learn.cn How to pass a variable or object to dialog in android https://www.e-learn.cn/topic/2898075 <span>How to pass a variable or object to dialog in android</span> <span><span lang="" about="/user/155" typeof="schema:Person" property="schema:name" datatype="">和自甴很熟</span></span> <span>2019-12-24 20:29:33</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I have a custom dialog with one editText view and two buttons ok and cancel. I have a custom list view displaying some rows of data fetched from database. When the user clicks on the row of the list view, custom dialog box is shown to the user to edit the selected row. What i want to do is to be able to pass the object binded with the selected row to the dialog box so that i could display the data being edited. </p> <p>Here is my activity class:</p> <pre><code>public class TestDatabaseActivity extends ListActivity { private CommentsDataSource datasource; private CommentAdapter adt; static final int CUSTOM_DIALOG_ID = 0; private TextView dialog_editComment; private EditText dialog_txtEditComment; private Button dialog_btnOk, dialog_btnCancel; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); datasource = new CommentsDataSource(TestDatabaseActivity.this); datasource.open(); getList(); } private void getList() { List&lt;Comment&gt; values = datasource.getAllComments(); adt=new CommentAdapter(TestDatabaseActivity.this,R.layout.comment_row,values); setListAdapter(adt); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); CommentAdapter adapter= (CommentAdapter) getListAdapter(); final Comment cmt = adapter.mListComment.get(position); System.out.println(cmt.getId()+cmt.getComment()); //cmt is the object which i want to pass to my dialog showDialog(CUSTOM_DIALOG_ID); } private Button.OnClickListener customDialog_UpdateOnClickListener = new Button.OnClickListener(){ @Override public void onClick(View arg0) { // TODO Auto-generated method stub //save the value and update list } }; private Button.OnClickListener customDialog_DismissOnClickListener = new Button.OnClickListener(){ @Override public void onClick(View arg0) { // TODO Auto-generated method stub dismissDialog(CUSTOM_DIALOG_ID); } }; @Override protected Dialog onCreateDialog(int id) { // TODO Auto-generated method stub Dialog dialog = null;; switch(id) { case CUSTOM_DIALOG_ID: dialog = new Dialog(TestDatabaseActivity.this); dialog.setContentView(R.layout.comment_edit_dialog); dialog.setTitle("Edit"); dialog_editComment = (TextView)dialog.findViewById(R.id.editComment); dialog_txtEditComment = (EditText)dialog.findViewById(R.id.txtComment); dialog_btnOk = (Button)dialog.findViewById(R.id.btnOk); dialog_btnCancel = (Button)dialog.findViewById(R.id.btnCancel); dialog_btnOk.setOnClickListener(customDialog_UpdateOnClickListener); dialog_btnCancel.setOnClickListener(customDialog_DismissOnClickListener); break; } return dialog; } } </code></pre> <br /><h3>回答1:</h3><br /><p>Instead of using showDialog(CUSTOM_DIALOG_ID) use you can create a your own method with argument and in that you can use AlertDialog to display your view that contains textview and buttons.</p> <pre><code>i) private AlertDialog alert; should be declared in class scope above oncreate(). </code></pre> <p>ii) Instead of showDialog(CUSTOM_DIALOG_ID) use createDialog(cmt)</p> <pre><code>iii) private void createDialog(Comment cmt){ AlertDialog.Builder dialog = new AlertDialog.Builder(TestDatabaseActivity.this); View view = _inflater.inflate(R.layout.comment_edit_dialog,null); dialog.setTitle("Edit"); dialog_editComment = (TextView)view .findViewById(R.id.editComment); dialog_txtEditComment = (EditText)dialog.findViewById(R.id.txtComment); dialog_btnOk = (Button)view .findViewById(R.id.btnOk); dialog_btnCancel = (Button)view .findViewById(R.id.btnCancel); dialog_btnOk.setOnClickListener(customDialog_UpdateOnClickListener); dialog_btnCancel.setOnClickListener(customDialog_DismissOnClickListener); dialog.setView(view); //dialog.show(); alert = dialog.create(); alert.show(); } </code></pre> <p>iV) also instead of dismissDialog(CUSTOM_DIALOG_ID) use alert.dismiss();</p> <br /><br /><br /><h3>回答2:</h3><br /><pre><code>Also another solution to your problem is change the scope of cmt. i.e., Above onCreate() declare private Comment cmt; now it can be access the TestDatabaseActivity. in your code make a minor change and try @Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); CommentAdapter adapter= (CommentAdapter) getListAdapter(); cmt = adapter.mListComment.get(position); System.out.println(cmt.getId()+cmt.getComment()); //cmt is the object which i want to pass to my dialog showDialog(CUSTOM_DIALOG_ID); } also declare private Comment cmt = null; above oncreate() and then in onCreateDialog() you can access System.out.println(cmt.getId()+cmt.getComment()); Try ..... </code></pre> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/9956323/how-to-pass-a-variable-or-object-to-dialog-in-android</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/android" hreflang="zh-hans">android</a></div> <div class="field--item"><a href="/tag/customdialog" hreflang="zh-hans">customdialog</a></div> </div> </div> Tue, 24 Dec 2019 12:29:33 +0000 和自甴很熟 2898075 at https://www.e-learn.cn NullPointerException while changing textView text in an AlertDialog https://www.e-learn.cn/topic/2878033 <span>NullPointerException while changing textView text in an AlertDialog</span> <span><span lang="" about="/user/92" typeof="schema:Person" property="schema:name" datatype="">可紊</span></span> <span>2019-12-24 11:49:23</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p><strong>Background:</strong></p> <ul><li>I just want to create a customDialog with a specific layout and add the content.</li> <li>Its a DialogFragment</li> </ul><p><strong>Code:</strong></p> <pre><code>TextView text; @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); text = (TextView)getView().findViewById(R.id.lorem); text.setText("Test"); } @Override public Dialog onCreateDialog(Bundle savedInstanceState){ AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); LayoutInflater inflater = getActivity().getLayoutInflater(); builder.setView(inflater.inflate(R.layout.custom_dialog, null)); return builder.create(); } </code></pre> <p><strong>Problem:</strong></p> <ul><li>The Dialog worked, but after I started to add the the lines in "onCreate" I got a NullPointerException Error. I hope someone can help me.</li> </ul><hr /><p>Thanks @Raghunandan for the Answer + explanation and here is the working code:</p> <pre><code> @Override public Dialog onCreateDialog(Bundle savedInstanceState){ AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); LayoutInflater inflater = getActivity().getLayoutInflater(); View view = inflater.inflate(R.layout.custom_dialog_style, null); textView = (TextView)view.findViewById(R.id.your_textview); textView.setText("Test"); builder.setView(view); return builder.create(); } </code></pre> <br /><h3>回答1:</h3><br /><p>At first i thought its a <code>Activity</code> now i see its a <code>DialogFragment</code>.</p> <p>I am guessing the view belongs to <code>custom_dialog.xml</code></p> <pre><code>View view = inflater.inflate(R.layout.custom_dialog, null); text = (TextView)view.findViewById(R.id.lorem); builder.setView(view); </code></pre> <p>So use the view object to initialize <code>TextView</code>. </p> <p><code>text = (TextView)getView().findViewById(R.id.lorem);</code> here <code>getView()</code> returns null. </p> <p>You are calling <code>setText</code> on null leading to <code>NullPointerException</code>.</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/25363263/nullpointerexception-while-changing-textview-text-in-an-alertdialog</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/android" hreflang="zh-hans">android</a></div> <div class="field--item"><a href="/tag/nullpointerexception" hreflang="zh-hans">nullpointerexception</a></div> <div class="field--item"><a href="/tag/dialog" hreflang="zh-hans">dialog</a></div> <div class="field--item"><a href="/tag/textview" hreflang="zh-hans">textview</a></div> <div class="field--item"><a href="/tag/customdialog" hreflang="zh-hans">customdialog</a></div> </div> </div> Tue, 24 Dec 2019 03:49:23 +0000 可紊 2878033 at https://www.e-learn.cn How to create loading screen in android? https://www.e-learn.cn/topic/2876654 <span>How to create loading screen in android?</span> <span><span lang="" about="/user/77" typeof="schema:Person" property="schema:name" datatype="">只愿长相守</span></span> <span>2019-12-24 11:28:31</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I am developing an android app, in which I need a loading screen like the one given below. In this screen I want a custom progress bar and a loading text in the centre of the screen, and the background effect should be blurred. When the loading starts, the user should not be able to perform any action.</p> <p></p> <p>How can I create this type of loading screen?</p> <br /><h3>回答1:</h3><br /><p><strong>@Ganpat Kaliya</strong></p> <p>Please check Transparent progress dialog on Android </p> <p>Just set </p> <pre><code>setTitle(Loading...); </code></pre> <br /><br /><br /><h3>回答2:</h3><br /><p>I would suggest you to use the custom Progress dialog. The purpose of using progress dialog is that as you wanted the screen to be blur, so when ever you would bring Progress dialog to front the activity would get blur , additionally it is highly customize able , you can do what you want.</p> <p>A very useful explanation is here and here. please check them out and apply. </p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/32439145/how-to-create-loading-screen-in-android</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/android" hreflang="zh-hans">android</a></div> <div class="field--item"><a href="/tag/progress-bar" hreflang="zh-hans">progress-bar</a></div> <div class="field--item"><a href="/tag/loading" hreflang="zh-hans">loading</a></div> <div class="field--item"><a href="/tag/customdialog" hreflang="zh-hans">customdialog</a></div> </div> </div> Tue, 24 Dec 2019 03:28:31 +0000 只愿长相守 2876654 at https://www.e-learn.cn Android - Change custom dialog title background https://www.e-learn.cn/topic/2691207 <span>Android - Change custom dialog title background</span> <span><span lang="" about="/user/173" typeof="schema:Person" property="schema:name" datatype="">孤街浪徒</span></span> <span>2019-12-20 12:38:45</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I am creating a custom dialog and I want to know how to change the background of the title bar.</p> <p>I've tried two approaches:</p> <p>1 - I've tried the AlertDialog.Builder method 'setCustomTitle'. I created a simple layout view comprising of a textview with layout width and height 'match_parent' and background color. When I run the app, only the top half of the title bar is showing the background color. The bottom half is still showing the default theme background color. Does anyone know why?</p> <p>2 - I've created my own dialog theme. Ive created a style with parent inheritance to '@android:style/Theme.Holo.Light.Dialog'. I've then passed that in the AlertDialog.Builder constructor - new AlertDialog.Builder(this, R.style.test_dialog). It seems good but somehow the dialog is wrapped within a dialog. A square box is surrounding the dialog. Does anyone know why?</p> <br /><h3>回答1:</h3><br /><p>You can create a style like,</p> <pre><code>&lt;style name="cust_dialog" parent="@android:style/Theme.Dialog"&gt; &lt;item name="android:windowTitleStyle"&gt;@style/dialog_title_style&lt;/item&gt; &lt;/style&gt; &lt;style name="dialog_title_style" parent="android:Widget.TextView"&gt; &lt;item name="android:background"&gt;@android:color/black&lt;/item&gt; &lt;item name="android:padding"&gt;10dp&lt;/item&gt; &lt;/style&gt; </code></pre> <p>And you can instantiate dialog:</p> <pre><code>Dialog dialog=new Dialog(this,R.style.cust_dialog); dialog.setContentView(R.layout.fragment_features_dialog); dialog.setTitle(R.string.features); </code></pre> <p>Now the dialog shows up with black title background color. </p> <br /><br /><br /><h3>回答2:</h3><br /><p>The dialog-wrapped-within-a-dialog appearance is caused by the dialog's window background. Every dialog has this, but the default Android dialogs have the window background set to transparent. To do this, add this item in your custom dialog theme:</p> <pre><code>&lt;style name="CustomDialog" parent="@android:style/Theme.Holo.Dialog"&gt; &lt;item name="android:windowBackground"&gt;@android:color/transparent&lt;/item&gt; &lt;/style&gt; </code></pre> <br /><br /><br /><h3>回答3:</h3><br /><p>I am using Mono Android(Xamarin); am showing you another alternative that am using in my app to create a dialog in a fragment:</p> <pre><code>Dialog itemDialog = new Dialog(this.Activity); TextView alertTitle=(TextView)itemDialog.Window.DecorView.FindViewById(Android.Resource.Id.Title); alertTitle.SetTextColor(Android.Graphics.Color.Blue); alertTitle.SetBackgroundColor(Android.Graphics.Color.Orange); itemDialog.SetContentView(Resource.Layout.listview_custom_dialog); string[] options = new string[] { "Open", "Mark as Unread","Mute","View Profile","Block Connection","Delete Conversation" }; ArrayAdapter&lt;string&gt; adapter = new ArrayAdapter&lt;string&gt;(this.Activity, Resource.Layout.listitem_custom_dialog,Resource.Id.textViewDialogDescription, options); </code></pre> <p>Resource.Layout.listitem_custom_dialog: this is custom listview layout, here is the xml file: </p> <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/transparent" &gt; &lt;TextView android:id="@+id/textViewDialogDescription" android:layout_width="match_parent" android:layout_height="44dp" android:background="#ffffff" android:textColor="#386B96" android:paddingLeft="4dp" android:textSize="14dp" /&gt; &lt;/RelativeLayout&gt; ListView lv = itemDialog.FindViewById&lt;ListView&gt; (Resource.Id.listViewDialogItems); lv.Adapter = adapter; adapter.NotifyDataSetChanged(); itemDialog.SetCancelable(true); itemDialog.SetTitle("Conversation"); itemDialog.Show(); </code></pre> <p><strong>Android.Resource.Id.Title</strong>: this is the id of the textview containing the dialog title. and it is predefined by android. this way you will get a dialog that you can style in way you want.</p> <br /><br /><br /><h3>回答4:</h3><br /><p>Create an xml layout file for your title and inflate it and set to to the AlertDialog as</p> <pre><code>View view = getLayoutInflater().inflate(R.layout.cust_dialog_title, null); alertDialog.setCustomTitle(view); </code></pre> <br /><br /><br /><h3>回答5:</h3><br /><p>You can just set custom title like this</p> <pre><code>LayoutInflater inflater = this.getLayoutInflater(); View titleView = inflater.inflate(R.layout.custom_title, null); new AlertDialog.Builder(SubCategoryActivity.this) .setCustomTitle(titleView); </code></pre> <p>and in custom_title layout you can create custom title like this</p> <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"&gt; &lt;LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:paddingLeft="10dp" android:paddingRight="10dp" android:id="@+id/llsubhead" android:background="@color/colorPrimary"&gt; &lt;TextView android:id="@+id/exemptionSubHeading4" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:layout_weight="1" android:text="Exemption Sub Head" android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium" android:textColor="@color/white" /&gt; &lt;/LinearLayout&gt; &lt;/LinearLayout&gt; </code></pre> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/9302449/android-change-custom-dialog-title-background</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/android" hreflang="zh-hans">android</a></div> <div class="field--item"><a href="/tag/dialog" hreflang="zh-hans">dialog</a></div> <div class="field--item"><a href="/tag/customdialog" hreflang="zh-hans">customdialog</a></div> </div> </div> Fri, 20 Dec 2019 04:38:45 +0000 孤街浪徒 2691207 at https://www.e-learn.cn