Home > OS >  How to setText to a PopUp Window In Android Studio?
How to setText to a PopUp Window In Android Studio?

Time:09-22

So basically, I have a editText in my activity in which the user enters his/her phone number and then clicks on a Next button. Now, on clicking on the Button, I have inflated a popup view which shows the user his/her entered number and asks him if it is Okay or not.
The problem here is that, I am not able to access the number from the EditText to display it in the Popup View. Could anyone tell how can I do it?

Here is the edit text:

<EditText
    android:id="@ id/Phone_Number"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:importantForAutofill="no"
    android:inputType="phone"/>

The Confirm Number pop-up TextView :

<TextView
    android:id="@ id/txt_phone_number_confirm"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="" />

Layout Inflator:

public void onButtonShowPopupWindowClick(View view) {
    // inflate the layout of the popup window

    LayoutInflater inflater = (LayoutInflater)
            getSystemService(LAYOUT_INFLATER_SERVICE);
    @SuppressLint("InflateParams") View popupView = inflater.inflate(R.layout.confirm_number_popup, null);

    ViewGroup root = (ViewGroup) getWindow().getDecorView().getRootView();
    Button btn1 = popupView.findViewById(R.id.btn_edit);

    hideSoftKeyboard(Register.this);

    // create the popup window
    int width = LinearLayout.LayoutParams.WRAP_CONTENT;
    int height = LinearLayout.LayoutParams.WRAP_CONTENT;

    final PopupWindow popupWindow = new PopupWindow(popupView, width, 
 height, false);

    popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
}

Edit: When I change the inflator like this to set the text, I get a null pointer exception:

public void onButtonShowPopupWindowClick(View view) {
    // inflate the layout of the popup window

    LayoutInflater inflater = (LayoutInflater)
            getSystemService(LAYOUT_INFLATER_SERVICE);
    @SuppressLint("InflateParams") View popupView = inflater.inflate(R.layout.confirm_number_popup, null);

    ViewGroup root = (ViewGroup) getWindow().getDecorView().getRootView();
    Button btn1 = popupView.findViewById(R.id.btn_edit);

    hideSoftKeyboard(Register.this);

    // create the popup window
    int width = LinearLayout.LayoutParams.WRAP_CONTENT;
    int height = LinearLayout.LayoutParams.WRAP_CONTENT;

    final PopupWindow popupWindow = new PopupWindow(popupView, width, 
    height, false);

    popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);

    //Edit
    TextView txt_phone_confirm = 
    findViewById(R.id.txt_phone_number_confirm);
    EditText txt_Phone = findViewById(R.id.Phone_Number);
    txt_phone_confirm.setText(txt_Phone.getText().toString());

    btn1.setOnClickListener(v -> {
        clearDim(root);
        
  getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
        popupWindow.dismiss();
    });
}

On doing this,I get:

 java.lang.NullPointerException: Attempt to invoke virtual method 'void 
 android.widget.TextView.setText(java.lang.CharSequence)' on a null 
 object reference

CodePudding user response:

Invoke the find view by id method on the popupView, as this is the view that has your TextView:

 TextView txt_phone_confirm = popupView.findViewById(R.id.txt_phone_number_confirm);

This will get you the text view you just inflated.

  • Related