What's happening
I am writing a PopupWindow containing two TextViews, where the second TextView should be centered vertically in the popup, and the first TextView should be directly above it.
The problem is that RelativeLayout seems to process the two TextViews as one element and vertically center their middle. I want the bottom TextView to be center centered and the top above it (hence android:layout_above="@id/first_name" ).
XML layout
Note the clearly unnecessary LinearLayout, because RelativeLayout refused to fill the screen vertically (PopupWindow uses ViewGroup.LayoutParams.MATCH_PARENT ).
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" > <TextView android:id="@+id/first_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:gravity="center" android:singleLine="true" android:text="Christopher" /> <TextView android:id="@+id/lbl_hello" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@id/first_name" android:gravity="center" android:singleLine="true" android:text="@string/hello" /> </RelativeLayout> </LinearLayout>
Java Activity
LayoutInflater inflater = LayoutInflater.from(this); final View popupView = inflater.inflate(R.layout.<fragment name>, <parent object>, false); final PopupWindow pwindow = new PopupWindow(popupView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true); pwindow.setTouchable(true); new Handler().postDelayed(new Runnable() { @Override public void run() { pwindow.showAtLocation(popupView, Gravity.CENTER, 0, 0); } }, 100L);
source share