4

Previously, I have a DialogFragment, which I doesn't specific its UI components (Spinner, EditText) colors explicitly.

dialog_fragment_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"       
    android:paddingLeft="10dp" 
    android:paddingRight="10dp"
    android:orientation="vertical" >

    <Spinner
        android:id="@+id/country_spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="200dp"
        android:layout_marginBottom="10dp" /> 

    <EditText
        android:id="@+id/name_edit_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textNoSuggestions|textCapWords"
        android:maxLength="50"
        android:ems="10" >

        <requestFocus />
    </EditText>

</LinearLayout>

I'm using

  • SherlockActionBar
  • Theme.Sherlock.Light.DarkActionBar
  • android:minSdkVersion="10", android:targetSdkVersion="21"

It looks as following

enter image description here

After migrating to

  • AppCompat
  • Theme.AppCompat.Light.DarkActionBar
  • android:minSdkVersion="10", android:targetSdkVersion="22"

It looks like the following

enter image description here

I was wondering, am I doing something wrong during my migration process? Is there really a need, to specific color for DialogFragment UI components (Spinner, EditText) colors explicitly?

Is there a way, to make the DialogFragment looks good, without having the manually assigning colors to Spinner, EditText, ... just like what we do (We let system to decide the best color for Spinner, EditText, ...) when we use SherlockActionBar?

Note, I don't specific theme in Activity or DialogFragment. I only specific theme in Application, and I expect my Activity or DialogFragment will inherit from Application's.

<application
    android:theme="@style/..." >

The code for the DialogFragment is

Source code for DialogFragment

public class MyDialogFragment extends android.support.v4.app.DialogFragment {

    public static MyDialogFragment newInstance() {
        return new MyDialogFragment();
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.dialog_fragment_layout, null);

        final Spinner countrySpinner = (Spinner)view.findViewById(R.id.country_spinner);
        final EditText nameEditText = (EditText)view.findViewById(R.id.name_edit_text);
        final CountryArrayAdapter countryArrayAdapter = new CountryArrayAdapter(this.getActivity());
        countrySpinner.setAdapter(countryArrayAdapter);
        nameEditText.setHint(R.string.new_watchlist_hint);
        nameEditText.setText(org.yccheok.jstock.watchlist.Utils.getDefaultWatchlistName());
        Utils.placeCursorAtEndOfText(nameEditText);

        final AlertDialog dialog = new AlertDialog.Builder(this.getActivity())
        .setTitle(R.string.new_watchlist_title)
        .setView(view)
        // Add action buttons
        .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
            }
        })
        .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
            }
        })           
        .create();

        dialog.setCanceledOnTouchOutside(true);

        // http://stackoverflow.com/questions/2620444/how-to-prevent-a-dialog-from-closing-when-a-button-is-clicked
        dialog.setOnShowListener(new DialogInterface.OnShowListener() {

            @Override
            public void onShow(DialogInterface dialogInterface) {

                Button b = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
                b.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        ...
                    }
                });
            }
        });            

        return dialog;
    }

Code to show DialogFragment

private void showMyDialogFragment() {        
    FragmentManager fm = this.getActivity().getSupportFragmentManager();
    MyDialogFragment myDialogFragment = MyDialogFragment.newInstance();
    myDialogFragment.setTargetFragment(this, 0);
    myDialogFragment.show(fm, MY_DIALOG_FRAGMENT);        
}
Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875

2 Answers2

2

The Support Library version of AlertDialog, support.v7.app.AlertDialog, added in Android Support Library 22.1, brings a single material style dialog (and theming to the views within) to all API7+ devices. If you are using AppCompat, you should also be using the support library AlertDialog.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
0

The theme Theme.Sherlock.Light is technically a compatibility version of Holo.Light of Holo family. In appcompat library, the theme Theme.AppCompat.Light is the compatibility version of Material family. So the behavior above is to be expected because widgets like Spinner and EditText are styled with new material design.

If you want to preserve Holo looks then you will have to specify your own styles starting from values-v21 which you later inherit in v14 and v11. This post shows how to do it for spinner: Styling a Spinner, appcompat, material

Community
  • 1
  • 1
inmyth
  • 8,880
  • 4
  • 47
  • 52