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
After migrating to
- AppCompat
- Theme.AppCompat.Light.DarkActionBar
- android:minSdkVersion="10", android:targetSdkVersion="22"
It looks like the following
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);
}