0

I have a custom dialog in which I include another layout. In this layout there are 3 buttons and a TextView none of these is null. If I click the buttons they work correctly. But the TextView doesn't show any text. The text should appears when I click another button. That's how it should works

Custom dialog layout:

<LinearLayout 
   ............

.....
<androidx.cardview.widget.CardView
        android:id="@+id/result_card"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        card_view:cardElevation="2dp"
        card_view:cardMaxElevation="2dp"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="8dp"
        android:visibility="gone"
        app:cardCornerRadius="8dp">

    <include android:id="@+id/result_layout" layout="@layout/result_block" />

    </androidx.cardview.widget.CardView>

....
....
</LinearLayout>

The result_block layout

<?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="wrap_content"
    android:padding="16dp"
    android:background="@color/colorPrimary"
    android:orientation="vertical">

    <TextView
        android:id="@+id/result_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="17dp"
        android:text=""
        android:textColor="@color/colorWhite"
        android:fontFamily="sans-serif-medium"
        android:padding="8dp"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="end"
        android:orientation="horizontal">
        <ImageButton
            android:id="@+id/btn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_copy_24px_outlined"
            android:layout_marginEnd="16dp"
            android:tint="@color/colorWhite"
            android:background="?attr/selectableItemBackgroundBorderless"
            />

        <ImageButton
            android:id="@+id/btn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:tint="@color/colorWhite"
            android:src="@drawable/ic_share_24px_outlined"
            android:background="?attr/selectableItemBackgroundBorderless"
            />

        <ImageButton
            android:id="@+id/btn3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:tint="@color/colorWhite"
            android:src="@drawable/ic_volume_up_24px_outlined"
            android:background="?attr/selectableItemBackgroundBorderless"
            />
    </LinearLayout>

</LinearLayout>

And now the dialog

private void showDiag() {
        final View dialogView = View.inflate(getActivity(), R.layout.custom_dialog_layout,null);
        final View resultLayout = View.inflate(getActivity(), R.layout.result_block,null);
        final Dialog dialog = new Dialog(getActivity(), R.style.MyAlertDialogStyle);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(dialogView);

        final TextView resultTxt = resultLayout.findViewById(R.id.result_txt);
        final ImageButton btn1 = resultLayout.findViewById(R.id.btn1);
        final CardView resultCardView = dialog.findViewById(R.id.result_card_view);
        final TextInputEditText editText = dialog.findViewById(R.id.text_edit);
        final ImageButton clearText = dialog.findViewById(R.id.clear_text);
        MaterialButton resultConfirm = dialog.findViewById(R.id.result_confirm);
        ImageButton btn2 = dialogView.findViewById(R.id.copy_btn);
        ImageButton btn3 = dialogView.findViewById(R.id.share_btn);

        resultConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!editText.getText().toString().equals("")) {
                    String theWord = editText.getText().toString();
                    String result = Utils.getSecondWord(theWord);
                    // result it shows me the correct string with no errors or something else
                    resultTxt.setText(result); // doesn't work. Not set the text
                    insertWord(theWord, result);
                    resultCardView.setVisibility(View.VISIBLE);
                    resultCardView.setVisibility(View.VISIBLE);
                } else {
                    Toast.makeText(getActivity(), "Error", Toast.LENGTH_SHORT).show();
                }
            }
        });

        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i(TAG, "result: " + resultTxt.getText().toString());
            }
        });

        // Share btn
        bt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               Log.i(TAG, "result: " + resultTxt.getText().toString());
            }
        });

        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               Log.i(TAG, "result: " + resultTxt.getText().toString());
            }
        });

        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialogInterface) {
                Utils.revealShow(dialogView, true, null, resultConfirm);
            }
        });

        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
                if (i == KeyEvent.KEYCODE_BACK){
                    Utils.revealShow(dialogView, false, dialog, resultConfirm);
                    return true;
                }
                return false;
            }
        });
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        dialog.show();
    }

Everything inside the dialog works correctly but the TextView not. Even if I try to write something else like resultTxt.setText("Hello there"); the text not appears. Any suggestions?

Atlas91
  • 5,754
  • 17
  • 69
  • 141
  • try this https://stackoverflow.com/a/10803746/8528047 and make sure the root layout is the same as the one you cast it to, like FrameLayout in the example – Pemba Tamang Sep 19 '19 at 08:46
  • Instead of reverseMeResultLayout use resultLayout layout. – Rajnish suryavanshi Sep 19 '19 at 08:46
  • @PembaTamang but I expect a NullPointerException in that case. Instead I haven't. The TextView is there, exists but not write the text. The layout is correct. I edited the question – Atlas91 Sep 19 '19 at 08:54
  • 4
    You're inflating a completely separate instance of `result_block` there, one that is not the instance created from the `` in `custom_dialog_layout`. That's going to be inflated and added automatically. Get rid of the `View resultLayout` altogether, and just find your `TextView` in `dialogView`; i.e., `dialogView.findViewById(R.id.result_txt);`. – Mike M. Sep 19 '19 at 08:54
  • 1
    @MikeM. Thanks, you were right. I didn't know that the included layout is inflated automatically. – Atlas91 Sep 19 '19 at 09:03

1 Answers1

0

Please you remove the android:text=""in TextView because textview is get default text is empty or you write anything in TextView like android:text="abc"

Nidhi Savaliya
  • 162
  • 2
  • 9