0
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_check_attendance);

    Intent intent = getIntent();



    final ArrayList<String> names = intent.getStringArrayListExtra("names");
    final ListView name_list = findViewById(R.id.name_list);
    final ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
            android.R.layout.simple_list_item_1, names);
    name_list.setAdapter(adapter);



    name_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Log.i("Name",names.get(i));

            LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
            View popupview = inflater.inflate(R.layout.popup_window, null);


            int width = LinearLayout.LayoutParams.WRAP_CONTENT;
            int height = LinearLayout.LayoutParams.WRAP_CONTENT;
            popupWindow  = new PopupWindow(popupview, width, height, true);
            popupWindow.showAtLocation(view, Gravity.CENTER, 0,0);



        }
    });
}

In the above code, everything works perfectly fine, except that I want to set the Popup window text to be the clicked element in the list view.

I use Log.i(), and the clicked element is being printed, however when I try to set the text to this clicked element like:

TextView sub_name = findViewById(R.id.subject_name);
sub_name.setText(names.get(i));

I get the following error:

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

XML for the popup window:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/close"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="161dp"
        android:layout_marginTop="587dp"
        android:layout_marginEnd="162dp"
        android:layout_marginBottom="96dp"
        android:onClick="quit"
        android:text="Close"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/subject_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="176dp"
        android:layout_marginTop="258dp"
        android:layout_marginEnd="176dp"
        android:layout_marginBottom="455dp"
        android:text="TextView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Anubhav Dinkar
  • 343
  • 4
  • 16

2 Answers2

2

Put it after you inflate the View

LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View popupview = inflater.inflate(R.layout.popup_window, null);
int width = LinearLayout.LayoutParams.WRAP_CONTENT;
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
popupWindow  = new PopupWindow(popupview, width, height, true);
popupWindow.showAtLocation(view, Gravity.CENTER, 0,0);
TextView sub_name = popupview.findViewById(R.id.subject_name);
sub_name.setText(names.get(i));
Ashish
  • 6,791
  • 3
  • 26
  • 48
  • hello....can you help me over here..--> https://stackoverflow.com/questions/59526045/how-to-run-java-and-xml-code-fetched-from-api-into-another-fragment – Wini Dec 31 '19 at 12:53
  • The details you have provided is not compatiable. If your trying to make Application something like [Flutter Website provided](https://flutter.dev/). – Ashish Dec 31 '19 at 13:00
0

From API level 1 and onward you can place your content into a PopupWindow, which is a new temporary window in which you can place views that will be displayed on top of the current activity window. PopupWindow can be shown anywhere onscreen, either by providing an explicit location or by providing an existing view that the PopupWindow should be anchored to.

PopupWindow is used to show floating view on display at specified position, but without inserting or otherwise modifying the existing view hierarchy. It’s a floating container that appears on top of current activity. PopupWindow can have their own layout and can be set after inflating with setContentView(View).

From API level 18 and onward you can use the newer ViewOverlay to draw content on top of your views. ViewOverlay allows you to add any number of Drawable objects to a private layer managed by the parent view. Those objects will be drawn on top of the corresponding view as long as their bounds are within the bounds of the parent.

In order to draw content on top of our view hierarchy, we first need to create the content to display. Following listing (file res/layout/popup.xml) constructs a simple group of views that will be the content of our PopupWindow

Public class MainActivity extends Activity
    private PopupWindow popup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //inflate the popup content layout; we do not have access
        //to the parent view yet, so we pass null as the
        //container view parameter.

        View popupContent = getLayoutInflater().inflate(R.layout.popup, null);
        popup = new PopupWindow();

        //popup should wrap content view
        popup.setWindowLayoutMode(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT);
        popup.setHeight(250);
        popup.setWidth(350);

        //set content and background
        popup.setContentView(popupContent);
        popup.setBackgroundDrawable(getResources().getDrawable(R.drawable.popup_background));

        popupContent.findViewById(R.id.btnClose).setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                popup.dismiss();
            }
        });

        popup.setTouchInterceptor(this);
        popup.setFocusable(true);
        popup.setOutsideTouchable(true);
    }

    @Override
    protected void onPause() {
        super.onPause();
        popup.dismiss();
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        //Handle direct touch events passed to the PopupWindow
        return false;
    }

    public void onShowWindowClick(View v) {
        if (popup.isShowing()) {
            popup.dismiss();
        } else {
            //Show the PopupWindow anchored to the button we
            //pressed. It will be displayed below the button
            //if there's room, otherwise above.
            popup.showAsDropDown(v);
        }
    }
}
RamPrakash
  • 1,687
  • 3
  • 20
  • 25