Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending AlertDialogs in Android (where to find examples, how to get title and buttons)

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:

No buttons and no title in the PausDialog

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>
like image 508
Ted Avatar asked Oct 19 '11 17:10

Ted


People also ask

How many buttons can be set up on an AlertDialog?

AlertDialog. A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout.

Which method is used to set a title to a alert dialog?

In the following code setTitle() method is used for set Title to alert dialog. setMessage() is used for setting message to alert dialog.

What are the three buttons that can be added to a dialog?

Android Alert Dialog is built with the use of three fields: Title, Message area, and Action Button. Alert Dialog code has three methods: setTitle() method for displaying the Alert Dialog box Title.


1 Answers

There 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 calling super.onCreate() Example:

public class PausDialog extends AlertDialog {
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                View content = LayoutInflater.from(getContext()).inflate(R.layout.dialog_some_view, null);
                setView(content);
                setTitle("Some Title");
                setMessage("Some Message");
                setButton(DialogInterface.BUTTON_POSITIVE, "Ok", new OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int which) {
                       Log.d("TAG", "BUTTON_POSITIVE");
                   }
                });
                ((TextView) content.findViewById(R.id.data)).setText(R.string.some_custom_data);
                ((TextView) content.findViewById(R.id.description)).setText(getContext().getString(R.string.description));
                setCancelable(false);
                setOnKeyListener((dialog, keyCode, event) -> keyCode == KeyEvent.KEYCODE_BACK);

                super.onCreate(savedInstanceState);
            }
}
like image 66
Djek-Grif Avatar answered Oct 01 '22 05:10

Djek-Grif