Deepak Rattan
Deepak Rattan

Reputation: 1299

Unable to click on the items of PopupWindow

I am implementing the following screen:

Screenshot

Here you can see i have a popup containing different icons like Gallery,Photos etc.

The layout for popup is :

    <?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:background="@color/while_color"
    android:orientation="vertical"
    android:padding="@dimen/padding10">

    <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="Video" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/margin10"
        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>

I am using the following code to attach the popup under toolbar:

 //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  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));


 @Override
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);
    }
}


private void initializePopUpWindow() {
    //Placing the popup window below the toolbar
    popupWindow.showAsDropDown(toolbar, 0, 0);
    LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(R.layout.popupwindow_attachment, null);
    TextView gallery = (TextView) v.findViewById(R.id.gallery);
    gallery.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getApplicationContext(), "Gallery Clicked", Toast.LENGTH_SHORT).show();
        }
    });
}

Here i am trying to toast the message on clicking the Gallery icon ,but it is not working for me.Please help to access the icons of a popup window.

Upvotes: 0

Views: 2335

Answers (2)

Niranj Patel
Niranj Patel

Reputation: 33238

You are inflating your popup view again in initializePopUpWindow() method and this view you are not using that's why you not getting click.

Declare your click event code where you wrote popup window code.

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  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));

        TextView gallery = (TextView)layout.findViewById(R.id.gallery);
        gallery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(), "Gallery Clicked", Toast.LENGTH_SHORT).show();
            }
        });

Upvotes: 4

jinkal
jinkal

Reputation: 1702

You can get popup window icon using popup view.

 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  popupWindow = new PopupWindow(layout, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);


 //you can access popup icon and click


TextView gallery = (TextView)layout.findViewById(R.id.gallery);


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

        }
    });

//or

 layout.findViewById(R.id.gallery).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

//or 

 gallery.setOnClickListener(this);


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

Upvotes: 2

Related Questions