Search code examples
androidactionbarsherlock

Show AlertDialog using Sherlock with the Holo theme?


Using Sherlock on Android 2.3.4: I would like to show an AlertDialog containing:

1)A title
2)A content
3)2 buttons
I'm using the below class:

public class MyAlertDialog extends SherlockDialogFragment{

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
          // Use the Builder class for convenient dialog construction

        AlertDialog.Builder builder = new AlertDialog.Builder(getSherlockActivity());
        builder.setMessage("Title")
               .setPositiveButton("Fire!", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // FIRE ZE MISSILES!
                   }
               })
               .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // User cancelled the dialog
                   }
               });
        // Create the AlertDialog object and return it
        return builder.create();
    }

}

in my Activity i'm calling :

MyAlertDialog m = new MyAlertDialog();
m.show(getSupportFragmentManager(), "hey");

It is showing the AlertDialog but with the old theme(Remember i'm using Android 2.3.4)

Here is my entire main activity class if you want:

public class MainActivity extends SherlockFragmentActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActionBar ab;
        ab = getSupportActionBar();
        ab.setTitle("Testing Sherlock"); 

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
        getSupportMenuInflater().inflate(R.menu.main, menu);

        return super.onCreateOptionsMenu(menu);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        switch(item.getItemId())
        {
        case R.id.action_one:
            MyAlertDialog m = new MyAlertDialog();
            m.show(getSupportFragmentManager(), "hey");
            break;
        }
        return super.onOptionsItemSelected(item);
    }
}

The style i'm setting for my app is:

<resources>
<style name="AppBaseTheme" parent="@style/Theme.Sherlock.Light">
</resources>

P.S: I have the ActionBar displayed and everything is working fine except the theme of AlertDialog.
I want it to appear like this one:

enter image description here

and not like this one:

enter image description here


Solution

  • Create Style:

    <resources>
        <style name="MyFragment">
            <item name="android:windowFrame">@null</item>
            <item name="android:windowBackground">@android:color/transparent</item>
            <item name="android:windowIsFloating">true</item>
            <item name="android:windowContentOverlay">@null</item>
            <item name="android:windowTitleStyle">@null</item>
            <item name="android:colorBackground">#ffffff</item>
            <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
            <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
            <item name="android:backgroundDimEnabled">true</item>
            <item name="android:textColor">#FF0000</item>
        </style>
    </resources>
    

    In your SherlockFragmentActivity:

    public class MyDialogFragment extends SherlockDialogFragment {
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.MyFragment);
            // Get the layout inflater
            LayoutInflater inflater = getActivity().getLayoutInflater();
            final View view = inflater.inflate(R.layout.custom_layout, null);
            ....
        }
    }
    

    R.style.MyFragment is the style.

    R.style.custom_layout is your custom layout for AlertDialog.