0

I have a custom popup dialog, but the textview didn't show correctly. For example, I have activity A and activity B, and now I am activity B, and when I back to activity A, my custom popup dialog will show, but sometimes the textview in custom dialog didn't show, and I find that the height of the textview is 0, I don't know why! Is there anyone who can help me to figure out? thanks!

The code below is the textview that didn't show correctly sometimes!

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/popup_avatar_layout_margin"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:id="@+id/nick_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/popup_avatar_layout_margin"
        android:gravity="center_horizontal"
        android:textColor="@color/kcpopup_avatar_kidDialogContext"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="[NickName]" />


public class DialogPopupActivity extends Dialog{}
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState){
setContentView(R.layout.kid_dialog_avatar_layout)
useDialogCustomAvatar();
super.onCreate(savedInstanceState);
}

I set the text on method useDialogCustomAvatar. The code below is what I do.

TextView nickNameView = findViewById(R.id.nick_name);
nickNameView.setText(title);
Bboy_Jack
  • 1
  • 2

1 Answers1

0

Use DialogFragment class instead of the Dialog class and override onCreateDialog method. It is the preferred way how to create a dialog. The Dialog class doesn't handle lifecycle events, which can cause the message to not display sometimes.

Creating Dialog fragment

Differences between Dialog and Dialog Fragment

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Vít Kapitola
  • 499
  • 1
  • 5
  • 9