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>