I`m trying to set title text size for DialogFragment
I coded for my old test device (Android 5.0 API 21) and all was ok
But app falls after I began to use new test device (Android 8.1 API 27)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_multiple_info_dialog, null, false);
getDialog().setTitle(R.string.dialog_fragment_BAC_info_title);
TextView title = getDialog().findViewById(android.R.id.title);
title.setTextSize(30);
title.setTextColor(getResources().getColor(R.color.white));
it falls at the line setTextSize(30) with error
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setTextSize(float)' on a null object reference
Yes, log say me that title == null
I tried to replace part of code to onDialogCreate(Bundle savedInstanceState)...
But same thing - now app falls on same line inside onDialogCreate...
Any ideas how to fix it?
from Gradle
compileSdkVersion 27
minSdkVersion 21
targetSdkVersion 27
thanx to Mike M.
correct solution is
to define style for dialog fragment inside onCreate() and set title params inside onCreateDialog() methods of DialogFragment class
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogFragmentStyle);
}
need to add style in styles.xml
<style name="DialogFragmentStyle" parent="Theme.AppCompat.Light.Dialog">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">false</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">false</item>
</style>
and after need to design dialog
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
TextView title = dialog.findViewById(android.R.id.title); //here it works!
title.setTextSize(30);
title.setText(R.string.dialog_fragment_title);
title.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
title.setTextColor(getResources().getColor(R.color.orange));
dialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
return dialog;
}