How can I display a popup with additional buttons?

0

I am programming an App that should be used to drive an RC car. It is working mostly, but I have problems with a popup.

When I click a Button from the activity_main.xml (popupButton) a popup with three buttons should appear and I want to use onTouchListeners on these buttons. I made the layout file popup.xml and designed the Buttons like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="250dp"
    android:layout_height="100dp"
    android:gravity="center"
    android:layout_marginTop="150dp"
    android:background="#FFFF00"
    android:screenOrientation="landscape"
    tools:context="com.example.studienarbeit.MainActivity">

    <Button
        android:id="@+id/functionBL"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#aaa"
        android:layout_marginTop="85dp"
        android:layout_marginStart="140dp"
        android:paddingRight="10dp"
        android:paddingLeft="10dp"
        android:text="Rückw. links" />

    <Button
        android:id="@+id/functionBS"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="85dp"
        android:background="#aaa"
        android:paddingRight="10dp"
        android:paddingLeft="10dp"
        android:text="Rückw. gerade" />

    <Button
        android:id="@+id/functionBR"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#aaa"
        android:layout_marginTop="85dp"
        android:layout_marginStart="382dp"
        android:paddingRight="10dp"
        android:paddingLeft="10dp"
        android:text="Rückw. rechts" />

</RelativeLayout>

The onClickListener to open the popup ist working and the Buttons are displayed:

popupButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // inflate the layout of the popup window
                LayoutInflater inflater = (LayoutInflater)
                        getSystemService (LAYOUT_INFLATER_SERVICE);
                assert inflater != null;
                View popupView = inflater.inflate (R.layout.popup, null);

                // create the popup window
                int width = 2500;
                int height = 1000;
                // boolean focusable = true; // lets taps outside the popup also dismiss it
                boolean isTouchable = true;
                final PopupWindow popupWindow = new PopupWindow (popupView, width, height/*, focusable*/, isTouchable);

                // show the popup window
                popupWindow.showAtLocation (v, Gravity.CENTER, 0, 130);

                // dismiss the popup window when touched
                popupView.setOnTouchListener (new View.OnTouchListener ( ) {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        popupWindow.dismiss ( );
                        return true;
                    }
                });

And I also wrote the onTouchListeners for the three Buttons:

bl.setOnTouchListener (new View.OnTouchListener ( ) {
                    @Override
                    public boolean onTouch(View arg0, MotionEvent event) {
                        if (event.getActionMasked ( ) == MotionEvent.ACTION_DOWN) {
                            handleCmd (L, IR);
                            bl.setBackgroundColor (0xAAAAFF00);
                        } else if (event.getActionMasked ( ) == MotionEvent.ACTION_UP) {
                            handleCmd (U, L);
                            bl.setBackgroundColor (0xFFFFFFFF);
                        } else if (event.getActionMasked ( ) == MotionEvent.ACTION_CANCEL) {
                            handleCmd (U, L);
                            bl.setBackgroundColor (0xFFFFFFFF);
                        }
                        return false;
                    }
                });

But when I uncomment the Listeners and want to open the popup I get an NullPointerException.

I want to avoid a second activity if possible and when I make getContentView(R.layout.popup) only the popup is shown. But I want to put it only over a part of the activity_main so that the rest of it is still visible.

I would be grateful for help!

java
android
xml
android-studio
asked on Stack Overflow May 21, 2020 by lrstck • edited May 21, 2020 by (unknown user)

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0