Android popupWindow, cant get elements within popup
Asked Answered
P

2

5

I am having an issue with grabbing my Buttons and other elements from within my PopupWindow, using the debugger it just report back as null?

private void initiatePopupWindow() 
        { 
            try 
                { 
                    // We need to get the instance of the LayoutInflater 
                    LayoutInflater inflater = (LayoutInflater) LoginActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
                    View layout = inflater.inflate(R.layout.activity_register,(ViewGroup)

                    findViewById(R.id.popup_element)); 
                    pwindo = new PopupWindow(layout, 600, 900, true);
                    pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);

                    btnReg = (Button) findViewById(R.id.btnReg);
                    inputName = (EditText) findViewById(R.id.name);
                    inputDOB = (DatePicker) findViewById(R.id.dob);
                    String name = inputName.getText().toString();

                    btnReg.setOnClickListener(new View.OnClickListener()
                    {
                        @Override
                        public void onClick(View v)
                        {


                            if (  ( !inputName.getText().toString().equals("")) &&
                                    ( getAge(inputDOB.getDayOfMonth(), inputDOB.getMonth(), inputDOB.getYear()) > 15) )
                            {
                                //register user
                            }
                            else if ( ( inputName.getText().toString().equals("")) )
                            {
                                Toast.makeText(getApplicationContext(),
                                        "Please enter your name", Toast.LENGTH_SHORT).show();
                            }
                            else if (( getAge(inputDOB.getDayOfMonth(), inputDOB.getMonth(), inputDOB.getYear()) < 16) )
                            {
                                Toast.makeText(getApplicationContext(),
                                        "You must be at least 16 to use this app", Toast.LENGTH_SHORT).show();
                            }
                            else
                            {
                              Toast.makeText(getApplicationContext(), email, Toast.LENGTH_SHORT).show();



                            }

                        }
                    });
                }
            catch (Exception e)
            { 
                e.printStackTrace(); 
            } 
        }

Could anyone point me in the correct direction or explain why I can't find them using R.id?

Cheers

Update: Activity_Register.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
               android:id="@+id/popup_element" 
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent" 
                android:background="@drawable/rounded"
                android:orientation="vertical" 
                android:padding="10sp" >

    <EditText
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        android:hint="@string/name"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true">

        <requestFocus />
    </EditText>

    <DatePicker
        android:id="@+id/dobPicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:calendarViewShown="false"
        android:layout_below="@+id/dob"
        android:layout_centerHorizontal="true"/>

    <TextView
        android:id="@+id/dob"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/dob"
        android:layout_below="@+id/name"
        android:layout_alignParentStart="true"
        android:layout_marginTop="42dp"/>

    <Button
        android:id="@+id/btnReg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="38dp"
        android:text="@string/register"
        android:layout_below="@+id/dobPicker"
        android:layout_centerHorizontal="true"/>

</RelativeLayout>
Porbeagle answered 14/3, 2014 at 5:35 Comment(1)
post your activity_register.xml fileSmidgen
S
7

You can get like

View layout = inflater.inflate(R.layout.activity_register,
            (ViewGroup) youractivity.this.findViewById(R.id.popup_element));

btnReg = (Button) layout.findViewById(R.id.btnReg);
inputName = (EditText) layout.findViewById(R.id.name);
inputDOB = (DatePicker) layout.findViewById(R.id.dob);

It's becoz your all the elements coming from inflated layout view and you need to pass context to get your popup_element layout as ViewGroup.

Smidgen answered 14/3, 2014 at 5:38 Comment(1)
It works in mine as well now, I just thought when I had tried it that it didnt, I have problems with my onclick not firing now from within the popup, but I think this is because I have it wrapped in the initiatepopup sub.Porbeagle
E
0

use this layout.findViewById(R.id.popup_element)); instead of this findViewById(R.id.popup_element));

Exhaustive answered 14/3, 2014 at 5:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.