1

So the Negative and Positive buttons of my AlertDialog are greyed-out, but they shouldn't be. greyed-out text screen

I suspect it has something to do with Context, becouse once i had identical problem with my ListView. I have repaired that by changing argument in ArrayAdapter's reference from getApplicationContext() to getBaseContext(). Can someone explain it to me? I don't really understand the 'Context'

This is my code

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("What do you want to do with " + getArrayList("ListOfRecipes").get(position) );
            builder.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    List<String> list = new ArrayList<>(getArrayList("ListOfRecipes"));
                    Toast.makeText(getBaseContext(), list.get(position) + "has been removed", Toast.LENGTH_SHORT).show();
                    list.remove(position);
                    saveList(list, "ListOfRecipes");
                }
            });

            builder.setNegativeButton("Modify", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    List<String> list = new ArrayList<>(getArrayList("ListOfRecipes"));
                    SharedPreferences sp = getSharedPreferences("Recip", MODE_PRIVATE);
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putString("Recip", list.get(position));
                    editor.apply();

                    startActivity(new Intent(getBaseContext(), ManageRecipeActivity.class));

                }
            });
            AlertDialog alertDialog = builder.create();
            alertDialog.show();
TheD3luxed
  • 178
  • 1
  • 8

2 Answers2

3

First of all, if that piece of code is inside an activity, you can simply declare context with "this" (which is what you have done by passing MainActivity.this) to the dialog builder.

What I'm suspecting is that it may be that your MainActivity is getting a theme for the AlertDialog that is making the buttons look gray. You could check that out in your styles.xml (if there is a style defined for the dialog) and in the AndroidManifest file for the theme you are passing to your MainActivity.

If you don't find anything wrong/don't want to change the theme, I can think of two ways to solve that problem.

First way - Changing the button color (less work, but less flexible)

The first is actually changing the dialog button color as it's done in this post to whatever color you want.

Second way - inflating a custom view that meets your needs (more work, but more flexible)

The second way would be to inflate a view and pass it to the dialog. Actually, you don't really have to use the standard dialog style at all, you can inflate your own view inside it to fit your needs.

To do that, you must:

1) Inflate a chosen view

As an example:

    LayoutInflater factory = LayoutInflater.from(this);
    final View view = factory.inflate(R.layout.image_dialog_layout, null);            

2) Pass the inflated view to your dialog builder:

    final AlertDialog dialog = new AlertDialog.Builder(this);
    dialog.setView(view);
    //Additional code to set click listeners, i.e.
    dialog.create().show();
}

That way, you'll be inflating whatever layout you want, so you can just put the buttons you want inside it (with the color, size, font type you want).

It is important to notice that, even after inflating a view to it, you can still use methods setPositiveButton and setNegativeButton, they will appear below your inflated layout in the dialog. So, beware inflating buttons AND using those methods, because the buttons will appear duplicated.

Since, in this case, you don't want them to be gray, you want to put buttons inside your layout, with whatever style you want, and inflate them (and reference them in your code through findViewById).

The biggest advantage of the second way is that you can inflate whatever you want, with the styles you want. You can even put images inside it, if you wish.

Hope it helps, let me know if it worked for you.

Community
  • 1
  • 1
FabioR
  • 696
  • 9
  • 18
  • 1
    That is brilliant. I need to use more LayoutInflater. Thanks for help ;) – TheD3luxed Oct 22 '16 at 20:22
  • I'm glad I could help : ) Whenver I know I'll have to customize a lot of the dialog, I prefer the second option, which allows you to work more freely with styles – FabioR Oct 22 '16 at 20:39
0

Context is an interesting topic in android. And one thing to understand is Application Context and Activity Context are different. You should make sure that any thing that is related to UI, you should be using Activity Context.

This can be things like

  • Showing a dialog
  • Starting another activity
  • Inflating a new layout

    This is because Activity is the only Context on which the themes defined in your manifest are actually attached.

I also recommend Context, What Context article to get a complete picture.

Happy Coding :)

Arun C
  • 9,035
  • 2
  • 28
  • 42