Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct text color/appearance in AlertDialog

I am struggling around with this problem for quite a long time. I searched for solutions and there were actually some suggestions for related problems, but nothing really worked correctly for me. So let's say I want to create an AlertDialog with a (long) message, a checkbox and two buttons.

// create dialog
AlertDialog.Builder dlg = new AlertDialog.Builder(this);
dlg.setTitle(R.string.dlg_title);
dlg.setMessage(R.string.dlg_msg);

// add checkbox
final CheckBox checkbox = new CheckBox(this);
dlg.setView(checkbox);

// add buttons
dlg.setPositiveButton(...);
dlg.setNegativeButton(...);

// show dialog
dlg.show();

This won't work, because if the dialog message becomes too long, the checkbox will not be completely shown. Additionally, it is going to be completely hidden in landscape mode.

Next try would be to lookup the original dialog layout file (for me it is located under android-sdk-linux_86/platforms/android-17/data/res/layout/alert_dialog.xml) and attempt to copy the relevant part to create our own "clone" of the inner dialog layout, just like this:

dialog_checkbox.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="vertical" >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:overScrollMode="ifContentScrolls"
        android:paddingBottom="12dip"
        android:paddingLeft="14dip"
        android:paddingRight="10dip"
        android:paddingTop="2dip" >

        <TextView
            android:id="@+id/message"
            style="?android:attr/textAppearanceMedium"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="5dip" />
    </ScrollView>

    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

We could try to add this view to our dialog like this:

// create dialog
AlertDialog.Builder dlg = new AlertDialog.Builder(this);
dlg.setTitle(R.string.dlg_title);

// add checkbox
LinearLayout checkboxLayout = (LinearLayout) View.inflate(mContext, R.layout.dialog_checkbox, null);
((TextView) checkboxLayout.findViewById(R.id.message)).setText(R.string.dlg_msg);
mCheckbox = (CheckBox) checkboxLayout.findViewById(R.id.checkbox);
mCheckbox.setText(R.string.dlg_checkbox_msg);
setView(checkboxLayout);

// add buttons
dlg.setPositiveButton(...);
dlg.setNegativeButton(...);

// show dialog
dlg.show();

This actually works quite well. Almost. We will not see any problem until we use Theme.Light for our app. But as soon as we use Theme.Light, the dialog text colors will become black and unreadable on the dark background. WHY?!

  • We've cloned the original Android alert dialog xml layout source code. But although we use attr/textAppearanceMedium as a text style, the text color becomes black under Theme.Light anyway.
  • If we use Theme.Light and create a "normal" dialog with a normal message via setMessage, the text color is white and readable.

What's wrong? How to solve this? I don't want to programmatically set the text color to white or black, this will not look good on custom user themes. Any suggestions?

like image 415
flxapps Avatar asked Dec 25 '12 18:12

flxapps


People also ask

How do I change the text color on my dialog box alert?

For changing the font color and button background color, try this: AlertDialog. Builder builder = new AlertDialog. Builder(this); builder.

How do I change the font in AlertDialog?

If you only want to change text format, you can just override alertDialogTheme attribute to change the theme for the AlertDialog . <style name="MyTheme" parent="Theme. AppCompat. Light.

How do I change button color in AlertDialog?

So whenever you want to change the color of AlertDialog box, just change color in styles. xml and all the dialog boxes will be updated in the whole application.

What is property changes the color of text android?

Android TextView – Text Color. TextView Text Color – To change the color of text in TextView, you can set the color in layout XML file using textColor attribute or change the color dynamically in Kotlin file using setTextColor() method.


2 Answers

I had the same problem and solved it by creating a ContextThemeWrapper for my desired Dialog Theme and used this when creating the AlertDialog Builder and when inflating the View to attach:

ContextThemeWrapper wrapper = new ContextThemeWrapper(this, R.style.DialogBaseTheme);
AlertDialog.Builder builder = new AlertDialog.Builder(wrapper);
View view = LayoutInflater.from(wrapper).inflate(R.layout.create_warning, null); 
builder.setView(view);

The style R.style.DialogBaseTheme is defined in "/values/styles.xml" for Gingerbread and below as:

<style name="DialogBaseTheme" parent="android:Theme.Dialog"/>

Then it is also defined in "/values-v11/styles.xml" for Honeycomb and upward to use the Holo theme as:

<style name="DialogBaseTheme" parent="android:Theme.Holo.Light.Dialog" />

So on Gingerbread my custom View's TextViews are automatically coloured white and Honeycomb+ they are automatically coloured black for Holo Light.

like image 72
Mark Avatar answered Sep 23 '22 22:09

Mark


I THINK I finally found out how to solve it. On the basis of my initial code, this seems to solve my problem:

// add checkbox layout
LinearLayout checkboxLayout = (LinearLayout) View.inflate(new ContextThemeWrapper(context, android.R.style.Theme_Dialog), R.layout.dialog_checkbox, null);
((TextView) checkboxLayout.findViewById(R.id.message)).setText(DIALOG_TEXT);
mCheckbox = (CheckBox) checkboxLayout.findViewById(R.id.checkbox);
mCheckbox.setText(CHECKBOX_NOT_AGAIN);
setView(checkboxLayout);

Note the ContextThemeWrapper. It makes that the dialog theme is assigned to the inflated layout. I hope, this finally solves that problem for the future.

like image 37
flxapps Avatar answered Sep 23 '22 22:09

flxapps