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.
The docs on google doesnt really say much either.
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).
I have created my PausDialog.java (see code below), it shows up but I am unable to get the title or any of the buttons (positive, negative etc) to show in the Dialog. See this picture:
So, question 1: where can I find good, clean and useful examples on how to correctly extend AlertDialogs and how to use them thereafter
question 2: why can I not see the title or any buttons using the custom AlertDialog below?
PausDialog.java
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
}
});
}
}
MainActivity.java, calling the PausDialog:
PausDialog pd = new PausDialog(MainActivity.this);
pd.show();
The layout for my PausDialog, paus.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<RelativeLayout android:gravity="top" android:layout_height="200dp" android:layout_weight="0.11" android:layout_width="304dp">
<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"></TextView>
<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">
<requestFocus></requestFocus>
</EditText>
<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"></TextView>
<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"></EditText>
<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"></TextView>
<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"></EditText>
</RelativeLayout>
</LinearLayout>
Below is simple example from Android Documentation,
Both methods for
setMessage()
andsetTitle()
available from builder class itself.In case you want to create custom view then use,
setView(int layoutResId)
from builder classThere are few options to create custom AlertDialog. I just would like to give you answer for current question. You can set title, message and other components of AlertDialog in
onCreate()
method. But make sure you doing it before you callingsuper.onCreate()
Example:I got my custom AlertDialog to work using something similiar to this code.
First make your constructor publicly accessible
Then you can simply instantiate and show it as so:
I've successfully made my own custom
AlertDialog
by extending theBuilder
class.Then, from the code, I instantiate them like this:
The only caveat is you have to take care of dismissing the dialog properly on orientation change or other events that don't explicit call the dismiss() method, like the back button or something else. Otherwise, you'll have memory leaks from that dialog.