Note that there are some explanatory texts on larger screens.

plurals
  1. POExtending AlertDialogs in Android (where to find examples, how to get title and buttons)
    text
    copied!<p>I have been looking a lot for examples on how to correctly extend AlertDialogs and get the expected behaviour, but I can hardly find any.</p> <p>The docs on google doesnt really say much either.</p> <p>Yes, I know I can use a AlertDialog.Builder to build the most commons things, but for some reasons I want to create my own AlertDialogs (I have code that I want contained in separate java files for one reason).</p> <p>I have created my PausDialog.java (see code below), it shows up but I am unable to get the <em>title</em> or any of the <em>buttons</em> (positive, negative etc) to show in the Dialog. See this picture:</p> <p><img src="https://i.stack.imgur.com/SAkJL.jpg" alt="No buttons and no title in the PausDialog"></p> <p>So, question 1: where can I find good, clean and useful examples on how to correctly extend AlertDialogs and how to use them thereafter</p> <p>question 2: why can I not see the title or any buttons using the custom AlertDialog below?</p> <hr> <p><strong>PausDialog.java</strong></p> <pre><code>package Test; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.os.Handler; import android.os.SystemClock; import android.text.Editable; import android.text.TextWatcher; import android.view.Gravity; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.view.WindowManager; import android.view.inputmethod.InputMethodManager; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class PausDialog extends AlertDialog { protected PausDialog(Context context) { super(context, R.style.DialogTheme); } @Override protected void onCreate(android.os.Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.paus); WindowManager.LayoutParams params = getWindow().getAttributes(); params.gravity = Gravity.TOP; final EditText ed1= (EditText) findViewById(R.id.editTextPausArea); final EditText ed2= (EditText) findViewById(R.id.EditTextPausTimeFrom); final EditText ed3= (EditText) findViewById(R.id.EditTextPausTimeTo); TextView tv1 = (TextView)findViewById(R.id.textViewPausArea); tv1.setText(LanguageHandler.GetString("AREA")); tv1 = (TextView)findViewById(R.id.textViewPausTime); tv1.setText(LanguageHandler.GetString("TIME")); setButton(DialogInterface.BUTTON_POSITIVE, "Positive", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { int fromArea = 0; int fromTime = 0; int toTime = 0; try { fromArea = Integer.parseInt(ed1.getText().toString()); fromTime = Integer.parseInt(ed2.getText().toString()); toTime = Integer.parseInt(ed3.getText().toString()); } catch(Exception e) { // TODO fail } } }); setButton(DialogInterface.BUTTON_NEGATIVE, "Negative", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // Do something } }); } } </code></pre> <p><strong>MainActivity.java</strong>, calling the PausDialog:</p> <pre><code>PausDialog pd = new PausDialog(MainActivity.this); pd.show(); </code></pre> <p><strong>The layout for my PausDialog, paus.xml:</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:weightSum="1"&gt; &lt;RelativeLayout android:gravity="top" android:layout_height="200dp" android:layout_weight="0.11" android:layout_width="304dp"&gt; &lt;TextView android:layout_alignParentLeft="true" android:text="Område" android:layout_width="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:id="@+id/textViewPausArea" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_marginLeft="17dp" android:layout_marginTop="18dp"&gt;&lt;/TextView&gt; &lt;EditText android:layout_alignBaseline="@+id/textViewPausArea" android:id="@+id/editTextPausArea" android:layout_width="80dp" android:layout_alignBottom="@+id/textViewPausArea" android:inputType="number" android:layout_height="wrap_content" android:layout_toRightOf="@+id/textViewPausArea" android:layout_marginLeft="17dp"&gt; &lt;requestFocus&gt;&lt;/requestFocus&gt; &lt;/EditText&gt; &lt;TextView android:text="Tid" android:layout_width="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:layout_below="@+id/editTextPausArea" android:id="@+id/textViewPausTime" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:layout_alignLeft="@+id/textViewPausArea"&gt;&lt;/TextView&gt; &lt;EditText android:layout_alignBaseline="@+id/textViewPausTime" android:id="@+id/EditTextPausTimeFrom" android:layout_width="80dp" android:layout_alignBottom="@+id/textViewPausTime" android:inputType="time" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editTextPausArea"&gt;&lt;/EditText&gt; &lt;TextView android:text=" - " android:layout_width="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:id="@+id/textView3" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/EditTextPausTimeFrom" android:layout_alignBottom="@+id/EditTextPausTimeFrom" android:layout_toRightOf="@+id/EditTextPausTimeFrom"&gt;&lt;/TextView&gt; &lt;EditText android:id="@+id/EditTextPausTimeTo" android:layout_width="80dp" android:inputType="time" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/textView3" android:layout_alignBottom="@+id/textView3" android:layout_toRightOf="@+id/textView3"&gt;&lt;/EditText&gt; &lt;/RelativeLayout&gt; &lt;/LinearLayout&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload