Search code examples
javaandroidandroid-alertdialog

How to add a 'Click for more details' expandable field to AlertDialog in android


I have a AlertDialog that is shown when there is an error and it prints 'Error, could not be added/whatever", I also have the result of the exception that I'd like to parse but not shown to all users, only to those that want to click on 'details' and read the exception.

            AlertDialog.Builder dialogo1 = new AlertDialog.Builder(activity);
            dialogo1.setTitle("Error");
            dialogo1.setMessage("Could not update movie: " + result);
            dialogo1.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialogo1, int id) {
                    activity.finish();
                }
            });
            dialogo1.show();

There is my code, pretty simple, right? I haven't been able to find this answer anyway and I'm starting to think it is just not possible


Solution

  • what you want is a custom alert dialog. so for that you will have to us a layout and define your dialog.Then initialize your listeners etc like it was a normal view that you are defining

    dialog_layout.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <ImageView
        android:src="@drawable/header_logo"
        android:layout_width="match_parent"
        android:layout_height="64dp"
        android:scaleType="center"
        android:background="#FFFFBB33"
        android:contentDescription="@string/app_name" />
    <TextView
        android:id="@+id/error"
        andorid:text="Error Message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/moreDetailsButton"
        android:text="Click for more details"
        android:inputType="textPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
     <TextView
        android:id="@+id/moreDetailsView"
        android:text="Click for more details"
        android:inputType="textPassword"
        android:layout_width="match_parent"
        android:visibility="gone";
        android:layout_height="wrap_content"/>
         <Button
        android:id="@+id/dialogButtonOK"
        android:layout_width="100px"
        android:layout_height="wrap_content"
        android:text=" Ok "
        android:layout_marginTop="5dp"
        android:layout_marginRight="5dp"
        android:layout_below="@+id/moreDetailsButton"
        />
    </RelativeLayout>
    

    and in your class

    final Dialog dialog = new Dialog(context);
    dialog.setContentView(R.layout.dialog_layout);
    TextView error=(TextView)dialog.findViewById(R.id.error);
    Button errorButton=(Button)dialog.findViewById(R.id.moreDetailsButton);
    Button okButton=(Button)dialog.findViewById(R.id.ok);
    
    
    //Do whatever you want with the rest of the dialog
    // initialize all the onclick listeners a usual
    
    
    errorButton.setOnClickListener(new View.onClickListener(){
    
        @Override
        public void onClick(View v){
            TextView errorDetails=(TextView)dialog.findViewById(R.id.moreDetailsView);
            errorDetails.setText(detailedErrorMessage)  //add the details to this text view;
            errorDetails.setVisibility(View.VISIBLE);
            }    
    });
    
    dialog.show();
    

    Sorry if there are any syntactically errors if the ctrl+c,ctrl+v of this doesnt work.My IDE decided to misbehave today.

    Follow this for details on a custom alert dialog.