8

I have a fragment in which I need to show a custom dialog.

Please check out my code below.

public class MyFragment extends Fragment{

  @Override
  public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.send_layout, container, false);
    TextView txtView = (TextView) rootView.findViewById(R.id.tv);

    txtView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            openDialog();
        }
    });
   return rootView;
  }
  public void openDialog(){
      AppCompatDialog dialog = new AppCompatDialog(getContext(), R.style.package_types__dialog);
      dialog.setContentView(R.layout.package_types_dialog);
      dialog.show();
  }
}

When removing the line:

dialog.setContentView(R.layout.package_types_dialog);

there is no error, but if I use the same the following error is throwing:

FATAL EXCEPTION: main
10-20 19:42:57.489 23866-23866/in.edelworks.pickedup E/AndroidRuntime: Process: in.edelworks.pickedup, PID: 23866
10-20 19:42:57.489 23866-23866/in.edelworks.pickedup E/AndroidRuntime: android.util.AndroidRuntimeException: Window feature must be requested before adding content
10-20 19:42:57.489 23866-23866/in.edelworks.pickedup E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplV7.throwFeatureRequestIfSubDecorInstalled(AppCompatDelegateImplV7.java:1584)
10-20 19:42:57.489 23866-23866/in.edelworks.pickedup E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplV7.requestWindowFeature(AppCompatDelegateImplV7.java:509)
10-20 19:42:57.489 23866-23866/in.edelworks.pickedup E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplBase.onCreate(AppCompatDelegateImplBase.java:117)
10-20 19:42:57.489 23866-23866/in.edelworks.pickedup E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplV7.onCreate(AppCompatDelegateImplV7.java:148)
10-20 19:42:57.489 23866-23866/in.edelworks.pickedup E/AndroidRuntime:     at android.support.v7.app.AppCompatDialog.onCreate(AppCompatDialog.java:60)
10-20 19:42:57.489 23866-23866/in.edelworks.pickedup E/AndroidRuntime:     at android.app.Dialog.dispatchOnCreate(Dialog.java:361)
10-20 19:42:57.489 23866-23866/in.edelworks.pickedup E/AndroidRuntime:     at android.app.Dialog.show(Dialog.java:262)
halfer
  • 19,824
  • 17
  • 99
  • 186
rpn
  • 121
  • 1
  • 4

7 Answers7

1

I've implemented and tried many alternatives for your kind of situation, It's working really fine, so that I couldn't get a chance to review your error. But what I can suggest you is replace AppCompatDialog with AlertDialog.Builder which is a class of android.support.v7.app.

Replace this codes

 public void openDialog(){
      AppCompatDialog dialog = new AppCompatDialog(getContext(), R.style.package_types__dialog);
      dialog.setContentView(R.layout.package_types_dialog);
      dialog.show();
  }

with

   public void openDialog(){    
        AlertDialog.Builder dialog = new AlertDialog.Builder(getContext(),R.style.package_types__dialog);       
        dialog.setView(R.layout.package_types_dialog);   
    }

Note :

And if you have any classes that handles the dialog events then extend DialogFragment of android.support.v4.app.DialogFragment. and don't forget to override onCreateDialog method. The full class will look something like this

import android.os.Bundle;
import android.support.v4.app.DialogFragment;

/**
 * Created by Shreekrishna on 2/29/2016.
 */
public class PackageTypesDialog extends DialogFragment { 

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return super.onCreateDialog(savedInstanceState);
    }
}

This will probably solve your issue !

Shree Krishna
  • 8,474
  • 6
  • 40
  • 68
0

Override onCreateDialog and use a v7 AlertDialog Builder instead. or

AlertDialog.Builder bulder = new AlertDialog.Builder(this);
bulder
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {

}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int id) {
        dialog.cancel();
   }
});
bulder.setTitle("Title");
bulder.setView(getLayoutInflater().inflate(R.layout.dialog, null));
AlertDialog dialog = bulder.create();
dialog.show();
Vasil Valchev
  • 5,701
  • 2
  • 34
  • 39
0

That's bug that appears on library version 23.0.0, and was released a fix in version 23.0.1. But some users reports that the problem still continue appearing.

Source

One thing that you can try is override onCreateDialog and use a v7 AlertDialog Builder instead. Just use DialogFragment for now, but you'll get an AppCompatDialog if you use the v7 Builder and a custom setView.

Gustavo Morales
  • 2,614
  • 9
  • 29
  • 37
0

Your error is a bug, it has been fixed in support library 23.0.1. You could try with v7 AlertDialog.Builder onCreateDialog():

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
   AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
   builder.setTitle("Simple Dialog");
   builder.setMessage("Some message here");

   builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
       @Override
       public void onClick(DialogInterface dialog, int which) {
       dismiss();
    }
});

builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        dismiss();
    }
});

    return builder.create();
}
Stanojkovic
  • 1,612
  • 1
  • 17
  • 24
0

you have to used below code to set custom view in alert dialog.This code is work in my project.

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
// ...Irrelevant code for customizing the buttons and title
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.alert_label_editor, null);
dialogBuilder.setView(dialogView);

EditText editText = (EditText) dialogView.findViewById(R.id.label_field);
editText.setText("test label");
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
Darshan Mistry
  • 3,294
  • 1
  • 19
  • 29
0

In my case, a library had a dependency on the appcompat v23.1.1 library. Forcing the library to use my current version of the app compat library fixed the issue.

To view the dependency heirarchy you can use the following command:

gradlew -p MODULE_NAME dependencies

Where MODULE_NAME is the name of your app module (usually just 'app')

To force the library to upgrade the dependency:

compile('com.example.m:m:1.0') {
    exclude group: 'com.android.support', module: 'appcompat-v7'
}
TheIT
  • 11,919
  • 4
  • 64
  • 56
-2

Forgive me if I'm not directly answering your question but I'll recommend an alternate solution.

I haven't directly used AppCompatDialog but I have used AlertDialog with AppCompatTheme. I find AlertDialog to be very simple in terms of using it.

Answer here gives a pretty good example of how to use AlertDialog.

Hope it helps.

Community
  • 1
  • 1
Pirdad Sakhizada
  • 608
  • 7
  • 12