HelloMojo
HelloMojo

Reputation: 367

Showing Android Alert With Black Background

Currently I have a null check which displays and alert (which is functioning correctly) however I'd like this alert to appear with a black bacground instead of just the alert dialog box.

I've tried removing finish(); but that simply displays the alert with the previous layout behind it - and leaving it causes the alert to appear over the user's home screen - my question is: how can this be modified to display the alert with a simple black background instead?

StartActivity.java source snippet:

   } else {
                // ICS and UP
                if (isMDNPresent) {
                    //start SaveMDN activity
                    Intent i = new Intent(StartActivity.this, SaveMDN.class);
                    startActivity(i);
                    finish();
                } else {
                    //start Update Activity
                    Intent i = new Intent(StartActivity.this,
                            UpdateActivity.class);
                    startActivity(i);
                    finish();
                }
            }

SaveMDN.java source code snippet:

 public class SaveMDN extends Activity {

EditText saveMDN;
TextView enterMdn;
Button save;
public static String mdn = "";
int version;
String temp;
TelephonyManager tm;
AlertDialog mErrorAlert = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    if (tm.getLine1Number()== null){
        showAlert(getString(R.string.insert_sim_dialog));

Upvotes: 1

Views: 1387

Answers (1)

Vilgos Mako
Vilgos Mako

Reputation: 66

Here is an example that shows you how to change the background of the alertDialog.

public class MainActivity extends Activity implements OnClickListener{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button btn = (Button)findViewById(R.id.mybtn);
    btn.setOnClickListener(this);

}



public void showDialog(){

    AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.AlertDialogCustom));
    builder.setMessage("Test")

           .setPositiveButton("YES", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   // FIRE ZE MISSILES!
               }
           })
           .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   // User cancelled the dialog
               }
           });

   builder.show();
    // Create the AlertDialog object and return it

}
@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.mybtn:
        showDialog();   
        break;

    default:
        break;
    }   
}

}

You need to add to styles.xml this code:

    <style name="AlertDialogCustom">
      <item name="android:textColor">#FF0000</item>
      <item name="android:background">#FF0000</item>
      <item name="android:typeface">normal</item>
      <item name="android:textSize">10sp</item>
     </style>

And in your ui:

    <Button 
    android:id="@+id/mybtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TEST BUTTON"
    />

Here is a simple example that I've made quick for your problem. Hope this helps. Cheers

Upvotes: 5

Related Questions