JJD
JJD

Reputation: 51882

ActionBarSherlock dialog is not transparent and not dark

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?


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:

Upvotes: 2

Views: 4565

Answers (2)

devunwired
devunwired

Reputation: 63303

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...

Upvotes: 4

Iain_b
Iain_b

Reputation: 1053

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.

Upvotes: 1

Related Questions