Sunday, April 8, 2012

Android Custom Alert Dialog Display Error after changing the Build Version


I am developing one simple demo . Here in this demo i just create one simple custom alert dialog . Its work fine.



It shows me the perfect result when i build application in 1.6, but when i change the android version from 1.6 to 2.2 its shows the unexpected result. It doesn't show the background screen on which i display the custom alert dialog.



Here is my xml file. Custom Dialog Theme File




<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomDialogTheme" parent="@android:style/AlertDialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowAnimationStyle">@android:style/Theme.Dialog</item>
</style>
</resources>



Here is My CustomConfirmOkDialog Class




package com.utility.org;

import android.app.Activity;
import android.app.Dialog;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class CustomConfirmOkDialog extends Dialog implements OnClickListener
{
private Button okButton = null;
private TextView infoText=null,confirmBody=null;
private int errorMessage=0;
@SuppressWarnings("unused")
private Activity activity;

public CustomConfirmOkDialog(Activity context,int customdialogtheme,int errorMessage)
{
super(context,customdialogtheme);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.confirm_ok);
this.errorMessage = errorMessage;
this.activity = context;
initControls();
}

private void initControls()
{
okButton = (Button) findViewById(R.id.ok_button);
okButton.setOnClickListener(this);

infoText = (TextView)findViewById(R.id.infoText);
confirmBody = (TextView)findViewById(R.id.confirmBody);

switch (this.errorMessage)
{

case Utility.INVALID_USERNAME_PASSWORD:
try
{
infoText.setText(R.string.signIn);
confirmBody.setText(R.string.invalidUsernameAndPassword);
}
catch (Exception e)
{
e.printStackTrace();
}
break;


default:
break;
}
}
public void onClick(View v)
{
dismiss();
}
}



Calling this class from my main activity using the below code.




CustomConfirmOkDialog dialog = new CustomConfirmOkDialog(MainActivity.this, R.style.CustomDialogTheme, Utility.INVALID_USERNAME_PASSWORD);
dialog.show();



enter image description here enter image description here



Here you can clearly notice that 1st image shows the background . Its build in android 1.6 version while 2nd image doesn't shows the background . It shows the entire black screen. Its build in android version 2.2 . I am very thankful if anyone can solve this issue.



Can anyone help me to solve this simple and silly issue ?



Thanks in Advance.


Source: Tips4all

3 comments:

  1. I also faced the same problem. the problem is when I called constructor of Dialog class

    Dialog(Context context, int themeId)

    it will hide the background activity. The only solution that i found is don't call this constructor, instead only call

    Dialog(Context context)

    and set your style in the layout file.

    So in your code, only write

    super(context)

    instead of

    super(context, themeid);

    ReplyDelete
  2. Apparently, this is a known issue.


    This only happens when you try inheriting from the framework themes.
    Using @android:style directly will still treat them as non-
    fullscreen, which punches through the black background as expected.

    One workaround is to start with a nearly-blank theme (like Panel or
    Translucent) and then render what you need in your own layout (such as
    dialog edges, etc).


    Thought, I still don't fully understand this solution myself yet.

    And actually, I'm no longer sure they're talking about the exact same bug you've seen, since they're talking about it not working for an older version of the sdk (not a newer one like yours). See the bug report.

    ReplyDelete
  3. It resolved my problem by changing the following code in Custom Dialog Theme xml file.

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    <style name="CustomDialogTheme" parent="@android:style/Theme.Translucent.NoTitleBar">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:backgroundDimEnabled">true</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowNoTitle">true</item>
    </style>
    </resources>

    ReplyDelete