Skip to content

Instantly share code, notes, and snippets.

@libinbensin
Last active December 9, 2023 14:44
Show Gist options
  • Save libinbensin/67fcc43a7344758390c3 to your computer and use it in GitHub Desktop.
Save libinbensin/67fcc43a7344758390c3 to your computer and use it in GitHub Desktop.
Facebook Popup using PopupWindow
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<ImageView
android:src="@drawable/ic_launcher"
android:layout_width="30dp"
android:layout_height="30dp"/>
<TextView
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@android:style/TextAppearance.Small"
android:gravity="center_vertical"
android:textColor="@color/black"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/white"/>
<corners android:radius="5dp"/>
</shape>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/headerLayout"
android:layout_width="match_parent"
android:layout_height="30dp"
android:orientation="horizontal"
android:layout_alignParentTop="true"
android:gravity="center">
<TextView
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some One and 20 Others Like this"
android:textColor="@color/black"
android:textStyle="bold"
android:layout_margin="5dp"/>
</LinearLayout>
<ListView
android:id="@+id/commentsListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:layout_below="@id/headerLayout"
android:paddingBottom="50dp"
android:layout_marginBottom="0dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="1dp"
android:src="@color/Gray"/>
<EditText
android:id="@+id/writeComment"
android:hint="Write a Comment"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="12dp"
android:textColor="@color/black"
android:background="#00000000"/>
</LinearLayout>
</RelativeLayout>
public class MainActivity extends
private PopupWindow popWindow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
// call this method when required to show popup
public void onShowPopup(View v){
LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// inflate the custom popup layout
final View inflatedView = layoutInflater.inflate(R.layout.fb_popup_layout, null,false);
// find the ListView in the popup layout
ListView listView = (ListView)inflatedView.findViewById(R.id.commentsListView);
// get device size
Display display = getWindowManager().getDefaultDisplay();
final Point size = new Point();
display.getSize(size);
mDeviceHeight = size.y;
// fill the data to the list items
setSimpleList(listView);
// set height depends on the device size
popWindow = new PopupWindow(inflatedView, size.x - 50,size.y - 400, true );
// set a background drawable with rounders corners
popWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.fb_popup_bg));
// make it focusable to show the keyboard to enter in `EditText`
popWindow.setFocusable(true);
// make it outside touchable to dismiss the popup window
popWindow.setOutsideTouchable(true);
// show the popup at bottom of the screen and set some margin at bottom ie,
popWindow.showAtLocation(v, Gravity.BOTTOM, 0,100);
}
void setSimpleList(ListView listView){
ArrayList<String> contactsList = new ArrayList<String>();
for (int index = 0; index < 10; index++) {
contactsList.add("I am @ index " + index + " today " + Calendar.getInstance().getTime().toString());
}
listView.setAdapter(new ArrayAdapter<String>(TryMeActivity.this,
R.layout.fb_comments_list_item, android.R.id.text1,contactsList));
}
}
@johanrovala
Copy link

Hi! I was linked to this example from a brilliant answer to a question on SO. However I'm a little confused as to how the sizing dependant on Device screen size works. Namely in curious about this variable 'mDeviceHeight'. When and how should it be declared and what does it do?

Kind regards, Johan

@seshuvinay
Copy link

Any idea how to close it when it's dragged down like facebook comments?

@Inn0vat0r
Copy link

how to get outside window with transparent color for showing popup ?Any idea.

@Osmandiyaka
Copy link

any idea how to dismis by dragging?

@ibmshaikh
Copy link

edittext is not showing while typing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment