2

I have a popupwindow and I have added scrollview as its parent layout. But its not working. If I change the scrollview to a diff layout, its working.. Basically I have multiple layouts ( mix of relative and linear ) and I want it to be scroll-able. I have removed the complex tree structured layout, so make the code look simple..

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/ScrollView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fillViewport="true" >

            <LinearLayout
                android:id="@+id/LinearLayout2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/chatheadpop"
                android:orientation="vertical" >

                <TableRow
                    android:id="@+id/headerRow"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="10dp" >

                    <ImageView
                        android:id="@+id/profilePic"
                        android:layout_width="0dp"
                        android:layout_height="110dp"
                        android:layout_marginRight="10dp"
                        android:layout_weight="0.40"
                        android:background="#ccc" />

                    <RelativeLayout
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="0.60" >

                        <TextView
                            android:id="@+id/phoneIcon"
                            android:layout_width="30dp"
                            android:layout_height="wrap_content"
                            android:layout_marginBottom="10dp"
                            android:textSize="18sp"
                            android:color="#ccc" />

                        <TextView
                            android:id="@+id/phoneField"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignBaseline="@+id/phoneIcon"
                            android:layout_toRightOf="@+id/phoneIcon"
                            android:maxLines="1"
                            android:singleLine="true"
                            android:textSize="18sp"
                            android:color="#aaa" />

                        <TextView
                            android:id="@+id/nameIcon"
                            android:layout_width="30dp"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/phoneIcon"
                            android:layout_marginBottom="10dp"
                            android:textSize="18sp"
                            android:color="#ccc" />

                        <TextView
                            android:id="@+id/nameField"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignBaseline="@+id/nameIcon"
                            android:layout_toRightOf="@+id/nameIcon"
                            android:maxLines="1"
                            android:singleLine="true"
                            android:textSize="18sp"
                            android:color="#aaa" />

                        <TextView
                            android:id="@+id/locationIcon"
                            android:layout_width="30dp"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/nameIcon"
                            android:layout_marginBottom="10dp"
                            android:textSize="18sp"
                            android:visibility="invisible"
                            android:color="#ccc" />

                        <TextView
                            android:id="@+id/locationField"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignBaseline="@+id/locationIcon"
                            android:layout_toRightOf="@+id/locationIcon"
                            android:maxLines="1"
                            android:singleLine="true"
                            android:textSize="16sp"
                            android:visibility="invisible"
                            android:color="#aaa" />

                        <TextView
                            android:id="@+id/jobIcon"
                            android:layout_width="30dp"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/locationIcon"
                            android:layout_marginBottom="10dp"
                            android:textSize="18sp"
                            android:visibility="invisible"
                            android:color="#ccc" />

                        <TextView
                            android:id="@+id/jobField"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignBaseline="@+id/jobIcon"
                            android:layout_toRightOf="@+id/jobIcon"
                            android:maxLines="1"
                            android:singleLine="true"
                            android:textSize="16sp"
                            android:visibility="invisible"
                            android:color="#aaa" />
                    </RelativeLayout>
                </TableRow>
    </LinearLayout>
    </ScrollView>

code to create the popupwindow

private void initiatePopupWindow(View anchor, final int pos) {
            try {

                popupWindow.dismiss();

            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }

            try {
                //Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

                LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);  
                popupView = layoutInflater.inflate(R.layout.chat_popup, null);  
                //popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
                popupWindow = new PopupWindow(popupView, width_px, LayoutParams.WRAP_CONTENT); 
                //popupWindow.setBackgroundDrawable(new ColorDrawable());
                //popupWindow.setFocusable(true);
                //popupWindow.setOutsideTouchable(true);

                profilePic = (ImageView) popupView.findViewById(R.id.profilePic);
                phoneIcon = (TextView) popupView.findViewById(R.id.phoneIcon);

......
......
......
}

}

Govind
  • 2,482
  • 1
  • 28
  • 40
  • Have you tried to chage android:layout_height="fill_parent" to android:layout_height="wrap_content" in your Scroll View? – domi Feb 03 '15 at 17:26
  • tried.. not working :( – Govind Feb 03 '15 at 17:27
  • Can you paste the code where you create the popout window please? – domi Feb 03 '15 at 17:28
  • Sir,its not working as it is not scrolling? `If I change the scrollview to a diff layout, its working` its working like how, it scrolls? or what – Elltz Feb 03 '15 at 17:30
  • If I change the scrollview to lets say linear layout, the popup window gets created ( but it wont scroll ).. But when i add scroll view, the popup window is not getting displayed.. – Govind Feb 03 '15 at 17:33
  • Sir,i assume you call `showAsDropdown` and `showAtLocation` with respective api level on your popupwindow.. then why don't you do what it wants create a linearlayout as parent and put your scrollview in it, sounds legit. – Elltz Feb 03 '15 at 18:37
  • @Elltz I have tried that.. keeping a linear layout as parent and scrollview as child.. it will throw a warning stating that "scroll view or its parent linear layout is useless", since linear layout wont have any other child view except scroll view.. – Govind Feb 04 '15 at 05:33
  • to be honest do not mind it if it works..those one child elements warnings are not important to me because there are soo many reasons why one would do that.. – Elltz Feb 04 '15 at 05:41
  • I would be happy if it works.. I doesn't work and when I checked, all I found was a warning.. I'm clueless.. The same structure works with "dialog" but not in popupwindow.. – Govind Feb 04 '15 at 05:59
  • SIr, i do not know if you come up with a solution, but i have and just copy paste the edit 1 and make a lil changes, and your problem will be solved, let me know though – Elltz Feb 06 '15 at 00:33
  • @Elltz I am facing the same problem and havent find a solution. Can you please let me know how you did it? – Srujan Barai Nov 12 '15 at 11:41

2 Answers2

1

okay also here hint you might need to show it

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
    popupWindow.showAsDropDown(context.getCurrentFocus(), 0, 0,Gravity.CENTER_HORIZONTAL); 
}else{
   popupWindow.showAtLocation(context.getCurrentFocus(), Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL, 10, 10);
        }

EDIT 1

TableRow is suppose to be use in a TableLayout, but there's no restriction on it, if you use it in any other layout as child then it acts as horizontal linearLayout.. so since you had all sort of problems which, i am conjuring your problems originated from your java code poping up with a NPE but you never printed the error so you didn't really actually see it or maybe the framework creating your xml widgets to memory.. so this is your solution, it will work,

i change some part in xml, but you can keep your version, but my version does what you want in terms of laying out, but with a different viewgroup

XML i called it popmenuitem.xml

  <?xml version="1.0" encoding="utf-8"?>

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

    <LinearLayout
        android:id="@+id/headerRow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:weightSum="10" >

        <ImageView
            android:id="@+id/profilePic"
            android:layout_width="0dp"
            android:layout_height="110dp"
            android:layout_marginRight="10dp"
            android:layout_weight="4"
            android:background="#ccc" />

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="6" >

            <TextView
                android:id="@+id/phoneIcon"
                android:layout_width="30dp"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:textSize="18sp"
                android:color="#ccc" />

            <TextView
                android:id="@+id/phoneField"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBaseline="@+id/phoneIcon"
                android:layout_toRightOf="@+id/phoneIcon"
                android:maxLines="1"
                android:singleLine="true"
                android:textSize="18sp"
                android:color="#aaa" />

            <TextView
                android:id="@+id/nameIcon"
                android:layout_width="30dp"
                android:layout_height="wrap_content"
                android:layout_below="@+id/phoneIcon"
                android:layout_marginBottom="10dp"
                android:textSize="18sp"
                android:color="#ccc" />

            <TextView
                android:id="@+id/nameField"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBaseline="@+id/nameIcon"
                android:layout_toRightOf="@+id/nameIcon"
                android:maxLines="1"
                android:singleLine="true"
                android:textSize="18sp"
                android:color="#aaa" />

            <TextView
                android:id="@+id/locationIcon"
                android:layout_width="30dp"
                android:layout_height="wrap_content"
                android:layout_below="@+id/nameIcon"
                android:layout_marginBottom="10dp"
                android:textSize="18sp"
                android:visibility="invisible"
                android:color="#ccc" />

            <TextView
                android:id="@+id/locationField"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBaseline="@+id/locationIcon"
                android:layout_toRightOf="@+id/locationIcon"
                android:maxLines="1"
                android:singleLine="true"
                android:textSize="16sp"
                android:visibility="invisible"
                android:color="#aaa" />

            <TextView
                android:id="@+id/jobIcon"
                android:layout_width="30dp"
                android:layout_height="wrap_content"
                android:layout_below="@+id/locationIcon"
                android:layout_marginBottom="10dp"
                android:textSize="18sp"
                android:visibility="invisible"
                android:color="#ccc" />

            <TextView
                android:id="@+id/jobField"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBaseline="@+id/jobIcon"
                android:layout_toRightOf="@+id/jobIcon"
                android:maxLines="1"
                android:singleLine="true"
                android:textSize="16sp"
                android:visibility="invisible"
                android:color="#aaa" />
        </RelativeLayout>
    </LinearLayout>

  </LinearLayout>

</ScrollView>

then in your activity, note i tested it in an awkward way..but will work with little changes that you can do.

Activity code

  //called this in my oncreate
  new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            initiatePopupWindow(getWindow().getDecorView(), 0, MainActivity.this);// i left pos because i didnt use it
        }
    }, 5000); // waited 5 seconds to notice it

  // I AM OUT OF MY ONCREATE METHOD AFTER THIS LINE 

  @SuppressLint("NewApi") 
private void initiatePopupWindow(View anchor, final int pos,Activity a) {

    PopupWindow popupWindow = null; // i know the initialisation is outside this method but since 
    // i am just throwing an example i put it here..
    View popupView;

    if(popupWindow != null){ // the unreachable error being thrown here is the tautology code i just did
        // but if you remove the initialisation from this method and place it outside the error will be gone
        popupWindow.dismiss(); //
    } // with this you get to remove the exception catch clause, and make your code look like you know how it works

    //
    try {
        //Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        popupView = a.getLayoutInflater().inflate(R.layout.popmenuitem, null);  
        //popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
        popupWindow = new PopupWindow(a); 
        popupWindow.setWidth(FrameLayout.LayoutParams.MATCH_PARENT);
        popupWindow.setHeight(FrameLayout.LayoutParams.WRAP_CONTENT);
        // i used FrameLayout.LayoutParams because of the scrollView, this was some of the reasons i think might be the reason of the error, might
        // not be though, but when incorporated everything seems cool..ScrollView extends FrameLayout
        popupWindow.setContentView(popupView);
        //popupWindow.setBackgroundDrawable(new ColorDrawable());
        //popupWindow.setFocusable(true);
        //popupWindow.setOutsideTouchable(true);
        //profilePic = (ImageView) popupView.findViewById(R.id.profilePic);
        //phoneIcon = (TextView) popupView.findViewById(R.id.phoneIcon);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
            popupWindow.showAsDropDown(anchor, 0, 0,Gravity.CENTER_HORIZONTAL); 
        }else{
            popupWindow.showAtLocation(anchor, Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL, 10, 10);
        }
    }catch(NullPointerException e){
        e.printStackTrace();
    }
}

Happy Coding Sir..

Elltz
  • 10,730
  • 4
  • 31
  • 59
0

have you tried removing scrollview

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/LinearLayout2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/chatheadpop"
            android:orientation="vertical" >

            <TableRow
                android:id="@+id/headerRow"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="10dp" >

                <ImageView
                    android:id="@+id/profilePic"
                    android:layout_width="0dp"
                    android:layout_height="110dp"
                    android:layout_marginRight="10dp"
                    android:layout_weight="0.40"
                    android:background="#ccc" />

                <RelativeLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.60" >

                    <TextView
                        android:id="@+id/phoneIcon"
                        android:layout_width="30dp"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="10dp"
                        android:textSize="18sp"
                        android:color="#ccc" />

                    <TextView
                        android:id="@+id/phoneField"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignBaseline="@+id/phoneIcon"
                        android:layout_toRightOf="@+id/phoneIcon"
                        android:maxLines="1"
                        android:singleLine="true"
                        android:textSize="18sp"
                        android:color="#aaa" />

                    <TextView
                        android:id="@+id/nameIcon"
                        android:layout_width="30dp"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/phoneIcon"
                        android:layout_marginBottom="10dp"
                        android:textSize="18sp"
                        android:color="#ccc" />

                    <TextView
                        android:id="@+id/nameField"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignBaseline="@+id/nameIcon"
                        android:layout_toRightOf="@+id/nameIcon"
                        android:maxLines="1"
                        android:singleLine="true"
                        android:textSize="18sp"
                        android:color="#aaa" />

                    <TextView
                        android:id="@+id/locationIcon"
                        android:layout_width="30dp"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/nameIcon"
                        android:layout_marginBottom="10dp"
                        android:textSize="18sp"
                        android:visibility="invisible"
                        android:color="#ccc" />

                    <TextView
                        android:id="@+id/locationField"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignBaseline="@+id/locationIcon"
                        android:layout_toRightOf="@+id/locationIcon"
                        android:maxLines="1"
                        android:singleLine="true"
                        android:textSize="16sp"
                        android:visibility="invisible"
                        android:color="#aaa" />

                    <TextView
                        android:id="@+id/jobIcon"
                        android:layout_width="30dp"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/locationIcon"
                        android:layout_marginBottom="10dp"
                        android:textSize="18sp"
                        android:visibility="invisible"
                        android:color="#ccc" />

                    <TextView
                        android:id="@+id/jobField"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignBaseline="@+id/jobIcon"
                        android:layout_toRightOf="@+id/jobIcon"
                        android:maxLines="1"
                        android:singleLine="true"
                        android:textSize="16sp"
                        android:visibility="invisible"
                        android:color="#aaa" />
                </RelativeLayout>
            </TableRow>
</LinearLayout>

Edit:

made a lot of changes to your code to get it working. finally I used

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffff"
android:isScrollContainer="true"
android:orientation="horizontal"
android:fillViewport="true" >

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

    <ImageView
        android:id="@+id/profilePic"
        android:layout_width="0dp"
        android:layout_height="110dp"
        android:layout_marginRight="10dp"
        android:background="#ccc"
        android:src="@drawable/ic_launcher"
        android:layout_weight="0.4" />

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.6" >

        <TextView
            android:id="@+id/phoneIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:text="textsssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss0"
            android:textSize="18sp"
            android:color="#ccc" />

        <TextView
            android:id="@+id/phoneField"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/phoneIcon"
            android:layout_toRightOf="@+id/phoneIcon"
            android:maxLines="1"
            android:singleLine="true"
            android:text="textsssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss1"
            android:textSize="18sp"
            android:color="#aaa" />

        <TextView
            android:id="@+id/nameIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/phoneIcon"
            android:layout_marginBottom="10dp"
            android:text="textsssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss2"
            android:textSize="18sp"
            android:color="#ccc" />

        <TextView
            android:id="@+id/nameField"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/nameIcon"
            android:layout_toRightOf="@+id/nameIcon"
            android:maxLines="1"
            android:singleLine="true"
            android:text="textsssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss3"
            android:textSize="18sp"
            android:color="#aaa" />

        <TextView
            android:id="@+id/locationIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/nameIcon"
            android:layout_marginBottom="10dp"
            android:text="textsssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss4"
            android:textSize="18sp"
            android:color="#ccc" />

        <TextView
            android:id="@+id/locationField"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/locationIcon"
            android:layout_toRightOf="@+id/locationIcon"
            android:maxLines="1"
            android:singleLine="true"
            android:text="textsssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss5"
            android:textSize="16sp"
            android:color="#aaa" />

        <TextView
            android:id="@+id/jobIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/locationIcon"
            android:layout_marginBottom="10dp"
            android:text="textsssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss6"
            android:textSize="18sp"
            android:color="#ccc" />

        <TextView
            android:id="@+id/jobField"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/jobIcon"
            android:layout_toRightOf="@+id/jobIcon"
            android:maxLines="1"
            android:singleLine="true"
            android:text="textsssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss7"
            android:textSize="16sp"
            android:color="#aaa" />
    </RelativeLayout>
</LinearLayout>

ps: I tried the same thing instead of HorizontalScrollView use ScrollView and it did not work

Omid Aminiva
  • 667
  • 5
  • 14