2

I am using ActionBarSherlock (ABS) and would like to add a dialog to my application as one can see in the ABS Demos Sample application provided by the project. The dialog sample look like this:

ActionBarSherlock Dialog

I created an activity myself. Here is relevant source code:

public class Dialog extends SherlockActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.Sherlock___Theme_DarkActionBar);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);
    }
}

For some reason, Android forces me to add setTheme() although the ABS sample does not do this. If I leave it out, I will run into the following error.

java.lang.IllegalStateException: You must use Theme.Sherlock, Theme.Sherlock.Light, Theme.Sherlock.Light.DarkActionBar, or a derivative.

My AndroidManifest.xml has the following settings, which are very similar to the ones from the ABS sample.

<activity
    android:name=".activities.Dialog"
    android:label="@string/title_activity_dialog"
    android:theme="@style/Theme.Sherlock.Dialog" >
</activity>

The following screenshot shows how my dialog activity looks like.

Custom Dialog

I am using ActionBarSherlock 4.1.0 with maps support, the Android Support library v4.

Question: Can you figure out, why it looks so different?

  • Dark vs. light user interface
  • Transparent vs. opaque background
  • With and without actionbar

Update:

The ABS sample starts the dialog activity as follows:

protected void onListItemClick(ListView l, View v, int position, long id) {
    Map<String, Object> map = (Map<String, Object>)l.getItemAtPosition(position);
    Intent intent = (Intent) map.get("intent");
    startActivity(intent);
}

I start the dialog activity as follows:

public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(R.string.title_menuItemDialogActivtiy)
        .setIcon(R.drawable.ic_action_dialog)
        .setIntent(new Intent(this, Dialog.class))
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    return true;
}

Meanwhile, I saw that this pattern is deprecated. Instead, I could use a DialogFragment. The question that occurs here: How can I integrate the fragment with the action menu item?


Alternative solution:

I decided to use a DialogFragment instead of an Activity as I estimate it to be more "future-safe". I basically followed the very informative tutorial Using DialogFragments (posted June 3, 2012), which I like to recommend as perfect starting point for any interest reader. Further, I like to add related and useful posts:

Community
  • 1
  • 1
JJD
  • 50,076
  • 60
  • 203
  • 339

2 Answers2

4

The output you are seeing definitely comes from setting the Theme in Java code (which will override the value set in XML). I just stood up the following sample application (this is literally all there is) and replicated the issue by adding the extra setTheme() call.

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".Dialog"
        android:theme="@style/Theme.Sherlock.Dialog">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

And the Dialog...

public class Dialog extends SherlockActivity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView text = new TextView(this);
        text.setText("This is a dialog!");

        setContentView(text);
    }
}

Now as to why you got an exception without that extra method, that's another matter (and quite strange). As you can see it should work with as little code as I provided above.

Perhaps make sure that both the library project and your project are being compiled with at least Android 4.0 (API 14) as this is a requirement of the library.

Beyond that, if you just want to show a Dialog in your application, does it need to be a themed Activity? This is not common. You can always create a simple Dialog or AlertDialog subclass to display as well. Take a look here for more information...

devunwired
  • 62,780
  • 12
  • 127
  • 139
  • I checked the ActionBarSherlock library project and my own: both target API level 16. What I did not read from your answer: did you manage to build a transparent, modal dialog? I tested your code and the result is the same as my second screenshot. -- Nevertheless, I will checkout the `Dialog` documentation once again and [`DialogFragment` specifically](http://android-developers.blogspot.de/2012/05/using-dialogfragments.html). – JJD Aug 13 '12 at 17:21
  • Yes, the output matched the sample application's output exactly (your first screenshot) on a 2.3.3 and 4.0 device. – devunwired Aug 13 '12 at 18:00
  • I should add that it looked like your second screenshot when the `setTheme(R.style.Sherlock___Theme_DarkActionBar);` was added back in. – devunwired Aug 13 '12 at 18:16
1

Try setting a theme for the application.

<application
    android:theme="@style/Theme.Sherlock"

The dialogue should inherit the theme. If you've already set that then remove the android:theme tag in the activity declaration and the setTheme() call and see what happens. The reason you get the error without setTheme is because SherlockActivities must have one of the themes in the error message and you had it set to something else in the manifest setTheme() overrode this before you got into trouble.

Iain_b
  • 1,043
  • 5
  • 13
  • Your suggested configuration lets the dialog appear with a black background color and white input fields. Also, there is still an actionbar visible. The dialog, however, is not transparent. – JJD Aug 13 '12 at 14:11
  • 1
    Could you show how you launch this activity? It could be that you're falling foul of one of the caveats here (see NOTE:) : http://developer.android.com/guide/topics/ui/dialogs.html#ShowingADialog – Iain_b Aug 13 '12 at 14:37