0

I got this issue and dont know how to fix it. here my manifest

<activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main"
        android:theme="@style/Theme.AppCompat.NoActionBar" />

here my onclick in listAdapter

holder.iconDelete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //need to confirm it first
            new AlertDialog.Builder(context)
                    .setTitle("Delete Setcard")
                    .setMessage("Do you really want to delete setcard?")
                    .setIcon(android.R.drawable.ic_dialog_alert)
                    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int whichButton) {
                            //run API volley
                            deleteSetcard(setcard_id);
                        }})
                    .setNegativeButton(android.R.string.no, null).show();
        }
    });

I tried other solution like changing the theme using Theme.AppCompat.Light

but none working

Gabriel
  • 229
  • 4
  • 19
  • 1
    post the full error stack trace and your `styles.xml` – Belladonna Dec 20 '17 at 09:29
  • Possible duplicate of [You need to use a Theme.AppCompat theme (or descendant) with this activity. Change to Theme.AppCompat causes other error](https://stackoverflow.com/questions/30180052/you-need-to-use-a-theme-appcompat-theme-or-descendant-with-this-activity-chan) – Ramesh sambu Dec 20 '17 at 09:35
  • Possible duplicate of [You need to use a Theme.AppCompat theme (or descendant) with this activity. Change to Theme.AppCompat causes other error](https://stackoverflow.com/questions/30180052/you-need-to-use-a-theme-appcompat-theme-or-descendant-with-this-activity-chan) – Vidhi Dave Dec 20 '17 at 09:41

4 Answers4

4

I hope this one is help to you

 <style name="myDialog" parent="Theme.AppCompat.Dialog">
    <item name="android:windowNoTitle">true</item>
    ...
 </style>

new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.myDialog));
Nikunj Paradva
  • 15,332
  • 5
  • 54
  • 65
1

I face such a case and I was managed to solve it. Here it is:

  • define your Alert Dialog in MainActivity class, example:

       public class MainActivity extends AppCompatActivity {
    
          AlertDialog.Builder alertDialog;
    
  • then initialize it on OnCreate() method just like below:

    alertDialog = new AlertDialog.Builder(this);
    
  • then you can do the rest of alert Dialog customization like icon, title, message anywhere you like. in my case i used it in onFinish() of count down timer as shown below:

    public void onFinish() {
    
                alertDialog.setIcon(android.R.drawable.alert_light_frame);
                alertDialog.setTitle("You are done");
                alertDialog.setMessage("you got it");
                alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
    
                        runTheCode();
                    }
                });
                alertDialog.show();
    
0

When using an AppCompat theme, you must ensure that your Activity extends AppCompatActivity, and that you import android.support.v7.app.AlertDialog from the Android Support Library instead of android.app.AlertDialog

Headcracker
  • 522
  • 4
  • 19
0

Try this

     <style name="InvitationDialog" 
     parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorAccent">@color/blue</item>
    <item name="android:textColorPrimary">@color/gray</item>
    <item name="android:editTextColor">@color/gray</item>
    <item name="android:background">@color/white</item>
    </style> 

Code

       new AlertDialog.Builder(context,R.style.InvitationDialog)
       .setTitle("Delete Setcard")
                .setMessage("Do you really want to delete setcard?")
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setPositiveButton("Yes", new 
                DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int 
                   whichButton) {
                        //run API volley
                        deleteSetcard(setcard_id);
                    }})
                .setNegativeButton("No", null).show();
                }
}); 
mehul chauhan
  • 1,792
  • 11
  • 26