2

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

Alex
  • 1,639
  • 3
  • 18
  • 33

3 Answers3

4

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.

Meirza
  • 1,318
  • 10
  • 15
  • the CTOR you've used for the builder is available only from API11 . also, even if you use AlertDialog.Builder(new ContextThemeWrapper(this, R.style.MyFragment)); , it won't work. tested on galaxy s2 with android 2.3.5 . – android developer Jun 19 '13 at 14:23
4

This library looks good for holo style android dialogs https://github.com/inmite/android-styled-dialogs

android styled dialogs

Borislav Gizdov
  • 1,323
  • 12
  • 22
1

I have already done this, I don't remember the exact steps I took, but the general approach is:

Copy the XML alert_dialog_holo (I think that is the name) from:
android sdk folder\platforms\android-version using holo\data\res\layout
to your project.

I think you have to make some changes in order to make it work correctly (I don't know anymore what I have done exactly, but I believe to remember you must remove the "android:" prefix from some style definitions, then they will be recognized by ABS.

Maybe you also need styles generated from this tool, but I don't know if they are implied to make it work.

Also take a look at this question the corresponding answers will lead you the way how to inflate your own XML in your Dialog

Community
  • 1
  • 1
Nickolaus
  • 4,785
  • 4
  • 38
  • 60
  • you need to make some changes to the XML you have copied, so that it will work correctly, then you should be able to inflate the XML in the DialogFragment – Nickolaus Mar 17 '13 at 17:22
  • That would be my alternative choice as I want it to simply work with Sherlock. I don't want to make my own new "Sherlock" class.. haha – Alex Mar 17 '13 at 17:24
  • you don't need to make a custom class just use your AlertDialog class and inflate the custom layout instead of androids standard layout – Nickolaus Mar 17 '13 at 17:28
  • see the last sentence of my answer I have updated it, the link will hint you how inflate your layout – Nickolaus Mar 17 '13 at 17:32