0

I am trying to implement the following screen.Here on clicking attachment icon ,popup window is displayed :

Screenshot

I am trying to implement the screen using the below mentioned code :

1.popupwindow_attachment.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/popup_element"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="@dimen/padding10"
    android:background="@color/while_color">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/gallery"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawablePadding="@dimen/padding10"
            android:drawableTop="@drawable/gallery"
            android:gravity="center"
            android:text="Gallery" />

        <TextView
            android:id="@+id/photos"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawablePadding="@dimen/padding10"
            android:drawableTop="@drawable/photos"
            android:gravity="center"
            android:text="Photos" />

        <TextView
            android:id="@+id/videos"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawablePadding="@dimen/padding10"
            android:drawableTop="@drawable/videos"
            android:gravity="center"
            android:text="Gallery" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/audio"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawablePadding="@dimen/padding10"
            android:drawableTop="@drawable/audio"
            android:gravity="center"
            android:text="Audio" />

        <TextView
            android:id="@+id/location"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawablePadding="@dimen/padding10"
            android:drawableTop="@drawable/location"
            android:gravity="center"
            android:text="Location" />

        <TextView
            android:id="@+id/contacts"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawablePadding="@dimen/padding10"
            android:drawableTop="@drawable/contacts"
            android:gravity="center"
            android:text="Contacts" />
    </LinearLayout>

</LinearLayout>

2.Code implementing the Popup window

   private void initializePopUpWindow() {

        //inflate the popupwindow_attachment.xml
        LinearLayout viewGroup = (LinearLayout) SingleChatActivity.this.findViewById(R.id.popup_element);
        LayoutInflater inflater = (LayoutInflater) SingleChatActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.popupwindow_attachment, viewGroup);
        popupWindow = new PopupWindow(layout, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT,true);

        //Displaying the popup at a specific location
        popupWindow.showAtLocation(layout,Gravity.CENTER,0,0);

    }

I am getting the following screen after using this code:

Screenshot

As you can see here ,popup window is not below the toolbar .Please help me to fix the issue.

Edited Code:

  private void initializePopUpWindow() {

    //inflate the popupwindow_attachment.xml
    LinearLayout viewGroup = (LinearLayout) SingleChatActivity.this.findViewById(R.id.popup_element);
    LayoutInflater inflater = (LayoutInflater) SingleChatActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.popupwindow_attachment, viewGroup);
    popupWindow = new PopupWindow(layout, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, true);

    //Displaying the popup at a specific location
   // popupWindow.showAtLocation(layout, Gravity.TOP, 0, 150);
    popupWindow.showAsDropDown(toolbar,0,0);

    //Close the popup when touch outside
    popupWindow.setOutsideTouchable(true);
    popupWindow.setFocusable(true);
   // popupWindow.dismiss();

}

After using the above code ,popup window is below the toolbar which i want but it not closed after clicking outside it.No functionality is working in the screen.Screen is just hanged badly.

Edited Working Code :

1.onCreate() method

 protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_single_chat);

            //Toolbar
            toolbar = (Toolbar) findViewById(R.id.toolbarSingleChat);
            toolbar.setNavigationIcon(R.drawable.back); // Setting Navigation Icon in the Toolbar
            setSupportActionBar(toolbar);

     LinearLayout viewGroup = (LinearLayout) SingleChatActivity.this.findViewById(R.id.popup_element);
            LayoutInflater inflater = (LayoutInflater) SingleChatActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View layout = inflater.inflate(R.layout.popupwindow_attachment, viewGroup);
            popupWindow = new PopupWindow(layout, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);

            //Close the popup when touch outside
            popupWindow.setOutsideTouchable(true);
            popupWindow.setFocusable(true);
            popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }

2.onoptionsItemSelected()

 public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_viewContacts:
                return true;
            case R.id.action_media:
                return true;
            case R.id.action_search:
                return true;
            case R.id.action_block:
                return true;
            case R.id.action_email_chat:
                return true;
            case R.id.action_clear_chat:
                return true;
            case R.id.action_attach:
                initializePopUpWindow();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

3.initializePopUpWindow() method:

 private void initializePopUpWindow() {
      popupWindow.showAsDropDown(toolbar, 0, 0);
    }

Now it is working for me.

Deepak Rattan
  • 1,279
  • 7
  • 21
  • 47

2 Answers2

3

If you want your popup at top then you need to set Gravity.TOP not Gravity.CENTER

Replace

 popupWindow.showAtLocation(layout,Gravity.CENTER,0,0);

with

 popupWindow.showAtLocation(layout,Gravity.TOP,0,0);

Update

If you want the popup below toolbar then you should do something like

 popupWindow.showAtLocation(layout,Gravity.TOP,0,100);// where 100 is the height of toolbar
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
  • I have tried this .It is showing me the PopupWindow at the top of the screen ,But i want it to below toolbar. popupWindow.showAtLocation(layout,Gravity.TOP,0,150); It is working for me on my Yureka mbile.I need to check whether it will work on on devices or not . – Deepak Rattan Feb 09 '16 at 13:34
  • instead of layout pass there toolbar view in popupWindow.showAtLocation(layout,Gravity.TOP,0,0); – justDroid Feb 09 '16 at 13:36
  • @justDroid Can you please explain in detail with any example ? – Deepak Rattan Feb 09 '16 at 13:40
2

You can do somthing like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textView = (TextView)findViewById(R.id.text);
    textView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showpopUp();
        }
    });

}

public void showpopUp()
{
    LayoutInflater layoutInflater
            = (LayoutInflater)getBaseContext()
            .getSystemService(LAYOUT_INFLATER_SERVICE);
    View popupView = layoutInflater.inflate(R.layout.popup, null);
    final PopupWindow popupWindow = new PopupWindow(
            popupView,
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);

 popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    popupWindow.setOutsideTouchable(true);
    popupWindow.setFocusable(true);

    popupWindow.showAsDropDown(textView, 0, 0);
}
justDroid
  • 343
  • 2
  • 9
  • But it will display the pop-up window below the view clicked. I want pop-up window below a specific view. In my case I want it below Toolbar which contains attachment action. – Deepak Rattan Feb 09 '16 at 14:52
  • exactly so you can pass here toolbar view so it will be displayed below toolbar. what else you want. – justDroid Feb 10 '16 at 04:55
  • :Please check my edited code .Popup window is successfully added below toolbar .But it is not removed after clicking outside it.Screen is just hanged.No functionality is working . – Deepak Rattan Feb 10 '16 at 05:05
  • Thanks for the help dear.Please check my edited code.Its working for me now. – Deepak Rattan Feb 10 '16 at 06:23