Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

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();      
    }     
like image 364
HelloCW Avatar asked Dec 06 '13 06:12

HelloCW


People also ask

How to create custom dialog box in Android Studio?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. In the above code, we have taken button. When user click on button, it will show custom dialog.

What is the use of Android checkbox?

CheckBox belongs to android.widget.CheckBox class. Android CheckBox class is the subclass of CompoundButton class. It is generally used in a place where user can select one or more than choices from a given list of choices. For example, selecting hobbies.

How do I make a checkbox clickable in Android?

When the user selects a checkbox, the CheckBox object receives an on-click event. To define the click event handler for a checkbox, add the android:onClick attribute to the <CheckBox> element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event.

How to show custom dialog when user click on button?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. In the above code, we have taken button. When user click on button, it will show custom dialog. Step 3 − Add the following code to src/MainActivity.java


2 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>
like image 175
Santhosh Avatar answered Sep 23 '22 00:09

Santhosh


I use this line of code and it works fine. It will make dialog background same colour as you have on 11+ api

alertDialogBuilder.setInverseBackgroundForced(true);
alertDialogBuilder.create();
like image 32
Martin Vandzura Avatar answered Sep 21 '22 00:09

Martin Vandzura