James_Duh
James_Duh

Reputation: 1371

AlertDialog styling

We have a project that will run on different screen sizes the AlertDialog v7 AppComp has a style.

I have written a CustomDialog as an Activity with it's own xml file and that seems to work fine except the emulator shows a ghost like view of the xml file when run! I have seen one post recent that implies that the text size of the message can not be changed. I have some knowledge of how to use DisplayMetrics but would rather not use this convention.

Design code for the AletDialog and style below. If someone can assure me the ghost image will not show up on a real device I might just give up and use this method, which seems clunky

    private void doWhat() {
    // R.style.MyAlertDialogStyle see res/values/styles
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(this, R.style.MyAlertDialogStyle);

    // Setting Dialog Title
    alertDialog.setTitle("Confirm Reset of Password");

    // Setting Dialog Message
    alertDialog.setMessage("Click YES to create a new master password");

    // Setting Icon to Dialog
    alertDialog.setIcon(R.drawable.caution);

    // Setting Positive "Yes" Button
    alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
            // Write your code here to invoke YES event
            db = helper.getReadableDatabase();

            String q = "SELECT * FROM masterPW";
            Cursor cursor = db.rawQuery(q,null);
            // Above query gets TABLE_PW data from Col_IDI
            // TABLE_PW will only ever have one row of data

            int rowID = 99;
            if(cursor.moveToFirst()){
                rowID = cursor.getInt(cursor.getColumnIndex(Col_IDI));
                str = cursor.getString(cursor.getColumnIndex(Col_MPW));
            }
            cursor.close();

            // Line of code below WORKS deletes entire TABLE <=====
            // Not a recomended way to re-set the master password
            // db.delete(TABLE_PW, null, null);

            String num = Integer.toString(rowID);

            db.delete(TABLE_PW, Col_IDI + " = ?", new String[] { num });
            db.close();

            Intent intentYY = new Intent(DetailsActivity.this, MainActivity.class );
            startActivity( intentYY );
        }
    });

    // Setting Negative "NO" Button
    alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // Write your code here to invoke NO event
            Toast.makeText(getApplicationContext(), "Password NOT Changed", Toast.LENGTH_SHORT).show();
            dialog.cancel();
        }
    });
    // Showing Alert Message and set the SIZE of the alertDialog
    alertDialog.show().getWindow().setLayout(1300, 500);// was 1100 500

}
    <!--Code below styles the AlertDialog.Builder on DetailsActivity -->
<style name="MyAlertDialogStyle" parent="Theme.AppCompat.Dialog.Alert">
    <!-- Used for the buttons -->
    <item name="colorAccent">@color/color_deepBlue</item>
    <!-- Used for the title and text -->
    <item name="android:textColorPrimary">@color/color_Black</item>
    <item name="android:textSize">25sp</item>
    <!-- Used for the background -->
    <item name="android:background">@color/color_lightGray</item>
</style>

Upvotes: 0

Views: 915

Answers (2)

Vector
Vector

Reputation: 3235

@James_Duh I deleted my prior answer after some extensive testing. Inflating the activity_custom.xml file has many problems. So the better idea is to use setContentView. You will still need to create activity_custom.xml for all device screens you will develop for code is below

Declare this like any other variable

 private Context context = this;

Then here is the method to open and display the activity_custom.xml file and show your new and improved Dialog I tested on various devices and it works great

    public void doWhat(){

    final Dialog openDialog = new Dialog(context);
    openDialog.setContentView(R.layout.activity_custom);
    Button btnYES = (Button)openDialog.findViewById(R.id.btnYES);
    // if YES delete Master Password from TABLE_MPW

    btnYES.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            openDialog.dismiss();
            Intent intent = new Intent( DetailsActivity.this, ListActivity.class );
            startActivity( intent );

            Toast.makeText(getApplicationContext(), "Password WAS Changed", Toast.LENGTH_SHORT).show();
        }
    });
    openDialog.show();
}

Upvotes: 1

TankRaj
TankRaj

Reputation: 89

For your kind of customization you need your own layout for alertdialog(not the default one) and to change the size of alert dialog you can use :

WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); 
lp.copyFrom(alertDialog.getWindow().getAttributes());
lp.width = 150;
lp.height = 500;
lp.x=-170;
lp.y=100;
alertDialog.getWindow().setAttributes(lp);

and to change the alert dialog theme, define your theme in your style.xml as:

<resources>
<style name="AlertDialogTheme" parent="@android:style/Theme.Dialog">
    <item name="android:textColor">#00FF00</item>
    <item name="android:typeface">monospace</item>
    <item name="android:textSize">10sp</item>
</style>
</resources>

and set the theme to dialog as:

 AlertDialog.Builder builder = new AlertDialog.Builder(new 
ContextThemeWrapper(this, R.style.AlertDialogTheme));

hope this is all you need.

Upvotes: 0

Related Questions