TheD3luxed TheD3luxed - 1 year ago 83
Java Question

Why my text is getting greyed-out? Probably Context has to do with something

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() {
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();
saveList(list, "ListOfRecipes");

builder.setNegativeButton("Modify", new DialogInterface.OnClickListener() {
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));

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

AlertDialog alertDialog = builder.create();;

Answer Source

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);
    //Additional code to set click listeners, i.e.

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download