Kevin Murvie
Kevin Murvie

Reputation: 2652

Android - Programatically changing Popup Window's layout

I have a PopupWindow which finally decides to follow my command and show itself after null error(certainly not my mistake in forgetting to initialize some value).

Here's the xml file for the PopupWindow

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/popup_handphone_MainLayout"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/popup_handphone_Wrapper"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/popupkodehp">

        <TextView
            android:id="@+id/popup_handphone_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:gravity="center"
            android:textColor="@color/textColor"
            android:text="@string/popupPhoneMessage"/>

        <RelativeLayout
            android:id="@+id/popup_handphone_functionalities"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/popup_handphone_text"
            android:layout_marginTop="15dp">

            <EditText
                android:id="@+id/popup_handphone_phoneNumber"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Phone Number"
                android:textColorHint="@color/textColor" />

            <EditText
                android:id="@+id/popup_handphone_phoneNumberConfirm"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/popup_handphone_phoneNumber"
                android:layout_marginTop="10dp"
                android:hint="Retype Phone Number"
                android:textColorHint="@color/textColor"/>

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/popup_handphone_phoneNumberConfirm"
                android:layout_marginTop="20dp"
                android:minHeight="20dp"
                android:text="Send Code"
                android:textColor="@color/textColor"/>

        </RelativeLayout>

    </RelativeLayout>

</RelativeLayout>

Here's the code of the PopupWindow I made with the help of anonymous and amazing people all over Google

View.OnClickListener phoneReinputHandler = new View.OnClickListener() {

        public void onClick(View arg0) {
            /*Intent intent = new Intent(SignupStepTwoActivity.this, PopupHandphone.class);
            backDim = (RelativeLayout) findViewById(R.id.bac_dim_layout);
            //backDim.setVisibility(View.VISIBLE);
            startActivity(intent);*/

            mainLayout = (RelativeLayout)findViewById(R.id.activity_signup_step_two_mainLayout);

            int width = displayMetrics.widthPixels;
            int height = displayMetrics.heightPixels;
            inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
/*            View popupLayoutInflater = inflater.inflate(R.layout.popup_handphone, mainLayout);

            RelativeLayout popupFunctionalitiesWrapper = (RelativeLayout)popupLayoutInflater.findViewById(R.id.popup_handphone_functionalities);
            int popupFunctionalitiesWrapperHeight = layoutResize.height(70, displayMetrics);
            RelativeLayout.LayoutParams popupFunctionalitiesWrapperParams = (RelativeLayout.LayoutParams)popupFunctionalitiesWrapper.getLayoutParams();
            popupFunctionalitiesWrapperParams.height = popupFunctionalitiesWrapperHeight;
*/
            PopupWindow pw = new PopupWindow(
                    inflater.inflate(R.layout.popup_handphone, null, false),
                    (int)(width * .8),
                    (int)(height*.35),
                    true);
            pw.setBackgroundDrawable(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
            pw.setOutsideTouchable(true);

            pw.showAtLocation(mainLayout, Gravity.CENTER, 0, 0);

        }
    };

The second commented part is a failed attempt to set the layout element's height. It shows itself on the main layout too, as in, the whole PopupWindow layout lay on top of the class' layout.

How to programatically customize the PopupWindow layout I made from the class' layout I'm at?

Upvotes: 0

Views: 1402

Answers (2)

Kevin Murvie
Kevin Murvie

Reputation: 2652

I've found the answer on how to change the insides of the PopupWindow using popupWindow.getContentView()

As usual, instantiate the PopupWindow

PopupWindow pw = new PopupWindow(
                    inflater.inflate(R.layout.popup_handphone, null, false),
                    (int)(width * .8),
                    (int)(height*.35),
                    true);

THEN call the layout of the PopupWindow using..

RelativeLayout popupFunctionalitiesWrapper = (RelativeLayout)pw.getContentView().findViewById(R.id.popup_handphone_functionalities);

Notice that pw.getContentView() is there before findViewById.

And thru that popupFunctionalitiesWrapper, I can set stuffs like..

int popupFunctionalitiesWrapperWidth = layoutResize.width(70);
            RelativeLayout.LayoutParams popupFunctionalitiesWrapperParams = (RelativeLayout.LayoutParams)popupFunctionalitiesWrapper.getLayoutParams();
            popupFunctionalitiesWrapperParams.width = popupFunctionalitiesWrapperWidth;
            popupFunctionalitiesWrapperParams.addRule(Gravity.CENTER);
            popupFunctionalitiesWrapper.setLayoutParams(popupFunctionalitiesWrapperParams);

Yeah and that's that!

Upvotes: 0

Mustansar Saeed
Mustansar Saeed

Reputation: 2790

If you want to update the PopupWindow width and height while the popupwindow is showing then use the function PopupWindow.update.

Hope this helps.

Upvotes: 1

Related Questions