0

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
Kirguduck
  • 748
  • 1
  • 9
  • 20
  • 1
    Apparently getting a title on a `DialogFragment` is kind of a pain, these days. Have a look at [this answer](https://stackoverflow.com/a/41798042). I had to do that to get it to work, but after I did, your code works as expected. – Mike M. Nov 18 '18 at 18:53
  • 1
    yep. it works for me. thanx a lot – Kirguduck Nov 18 '18 at 21:02
  • @MikeM. but why? why did it work correctly with previous device ? – Kirguduck Nov 19 '18 at 06:52
  • Dunno. I didn't go digging through the code, and I'm not able to do any significant testing, at the moment. It was a quick test and fix. I felt guilty for having misled you when I got my questions mixed up earlier, so I figured it was the least I could do. :-) – Mike M. Nov 19 '18 at 06:57

2 Answers2

0

this may help, pass the textsize as well as the unit.

* @param unit The desired dimension unit.
 * @param size The desired size in the given units.
 *
 * @attr ref android.R.styleable#TextView_textSize
 */
public void setTextSize(int unit, float size)
Irfan Yaqub
  • 140
  • 10
0

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;
}
Kirguduck
  • 748
  • 1
  • 9
  • 20