3

I have a custom alertdialog that inflates this custom layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/infoLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:theme="@style/AppTheme" >


    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:weightSum="2" >

        <TextView
            android:id="@+id/infoVersionTitle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="right|center"
            android:text="@string/infoVersion"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/infoVersionTitleValue"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </LinearLayout>

    <TextView
        android:id="@+id/infoDetailText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/linearLayout1"
        android:padding="10dp" />

</RelativeLayout>

I have this in my manifest file:

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />
........
<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"  >

And I've got style.xml defined this way:

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="AppTheme" parent="android:Theme.Light"></style>
</resources>

style.xml v11 defined like this:

<resources>
    <style name="AppTheme" parent="android:Theme.Light"></style>
</resources>

And style.xml v14 defined like this:

<resources>
    <style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar" />
</resources>

When running this on a 4.0.3 device it shows like this:

enter image description here

And running on a 2.3.3 device it shows like this:

enter image description here

On 2.3.3 devices, text is almost unreadable and I want to show white background (title bar included)

Is it possible to make the custom alertdialog show on all devices like it shows on 4.0.3?

I've saw this but cant't seem to figure it out.

Any solutions?

Favolas
  • 6,963
  • 29
  • 75
  • 127

3 Answers3

8

Try calling AlertDialog.Builder#setInverseBackgroundForced(true) when you create your dialog.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
1

You can use ActionBarSherlock for this purpose

Check the description in Theming page for Dialog

Amir
  • 203
  • 1
  • 8
  • Thanks but was looking for something without using third party solutions. Will look into that to see. But what can I do to when on 2.3.3 devices, the custom alertdialog shows black but with white letters. This way text will be readable – Favolas Sep 04 '12 at 11:24
  • 1
    If you don't want to use any third party solutions, then you can have a look at their source codes and get the idea how they have done it. Here is samples and source codes for [Holo every where](https://github.com/ChristopheVersieux/HoloEverywhere) – Amir Sep 04 '12 at 11:42
  • Ok will look into that also. Isn't there a way to force, on gingerbread devices, the alertdialog show with default gingerbread theme? – Favolas Sep 04 '12 at 14:23
  • I think with this [suggested approach](http://stackoverflow.com/questions/3423754/retrieving-android-api-version-programmatically) you can get the API version dynamically and based on that set your favorite theme for dialog. – Amir Sep 05 '12 at 12:21
  • ActionBarSherlock doesn't have a dialog theme anymore. – android developer Aug 02 '14 at 08:43
0

To ensure compatibility for all devices:

setInverseBackgroundForced(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? false
                                    : true)

This makes sure devices below HoneyCromb display normally and devices below are inverted!

Here is an example:

final AlertDialog.Builder dialog = new AlertDialog.Builder(this)
dialog.setInverseBackgroundForced(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? false
                        : true);
dialog.setMessage("Test Dialog!");
dialog.create().show();

See the docs:

http://developer.android.com/reference/android/app/AlertDialog.Builder.html#setInverseBackgroundForced(boolean)

Jared Burrows
  • 54,294
  • 25
  • 151
  • 185