The text of checkbox control in custom dialog can't be displayed in some android devices

Viewed 779

I use the following code to create and open a custom dialog, it works well in android 4.2.2 , but the text of checkbox control can't be displayed in android 2.3.6.

I find the text of checkbox control is black,and the BackgroundColor of dialog is white in android 4.2.2 , so it's OK, but and the BackgroundColor of dialog is black in android 2.3.6, so it's bad.

How can I do?

sms_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="10dp" >

    <CheckBox
        android:id="@+id/chNoDisplayAgain"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"        
        android:text="" />

</LinearLayout>

Related java code:

 private void DisplayPrompt(final String key,final boolean isClose,String msg){
        LayoutInflater li = LayoutInflater.from(this);
        View promptsView = li.inflate(R.layout.sms_dialog, null);       
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder.setView(promptsView);

        alertDialogBuilder.setTitle(getString(R.string.TitleWarning));      
        alertDialogBuilder.setMessage(msg); 
        alertDialogBuilder.setCancelable(false);

        final CheckBox myCheckBox=(CheckBox)promptsView.findViewById(R.id.chNoDisplayAgain);
        myCheckBox.setText(getString(R.string.NoDisplayAgain));     

        alertDialogBuilder
            .setPositiveButton(getString(R.string.BtnOK), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                   SharedPreferences prefs =PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                   if (prefs.contains(key)){                     
                      SharedPreferences.Editor editor =prefs.edit();
                      editor.putBoolean(key, !myCheckBox.isChecked());
                      editor.commit();

                   }
                   if (isClose){
                      finish();
                   }
                }
            });

        AlertDialog alertDialog = alertDialogBuilder.create();  
        alertDialog.show();      
    }     
5 Answers

try this with textcolor and background color as below..

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertial"
android:background="@android:color/white"
android:padding="10dp" >
<CheckBox
    android:id="@+id/chNoDisplayAgain"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center" 
    android:textColor="@android:color/black"
    android:text="" /></LinearLayout>

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

create style as u like...

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="customDialogTheme" parent="@android:style/AlertDialog">
        <item name="android:textColor">#000000</item>
        <item name="android:background">#ffffff</item>
    </style>
</resources>
Related