2

I am using ListPopupWindow with custom list item layout. But list item TextView isn't being multi-line. How can I do that?

final ListPopupWindow showRoomListPopupWindow = new ListPopupWindow(
                getActivity());
        showRoomListPopupWindow.setAdapter(new ArrayAdapter(
                getActivity(),
                R.layout.movie_detail_spinner_item, movieShowRoomARList));
        showRoomListPopupWindow.setModal(true);
//        showRoomListPopupWindow.setWidth(ListPopupWindow.MATCH_PARENT);


        showRoomListPopupWindow.setAnchorView(tvProgramSeansClick_TXT);

XML layout:

<?xml version="1.0" encoding="utf-8"?>
<TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:padding="10dp"
        android:textStyle="bold"
        android:minLines="3"
        android:singleLine="false"
        />
joao2fast4u
  • 6,868
  • 5
  • 28
  • 42
skhizein
  • 437
  • 1
  • 9
  • 15
  • What happens to TextView? Is it single line? Try to change android:layout_height value to wrap_content. – joao2fast4u Apr 12 '14 at 17:10
  • yes I want to multi-line text because texts are too long. I tried android:layout_height =wrap_content but it didnt work – skhizein Apr 12 '14 at 17:53
  • What is your AnchorView? The `ListPopupWindow` adjusts its width to the anchorView width when you set its width to WRAP_CONTENT. – joao2fast4u Apr 12 '14 at 18:42
  • I used your code and my anchor view is a Button with layout_height set to 300dp. My Items are multi-line and are ok. I used showRoomListPopupWindow.setWidth(ListPopupWindow.WRAP_CONTENT); I can show you the result if you want. – joao2fast4u Apr 12 '14 at 18:56
  • Anchorview is a button like this I tried this but not working. Can you share the code sample? It would be great. – skhizein Apr 12 '14 at 22:04
  • Why are you setting your Button visibility to gone? – joao2fast4u Apr 12 '14 at 22:46

1 Answers1

0

I tried to recreate your case, and I've managed to get your code working, just doing the following:

My AnchorView is a Button, inside a RelativeLayout, like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
  <Button
    android:id="@+id/popup"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:background="@null"
    android:visibility="invisible"
    android:text="Popup"
    android:textAppearance="@android:attr/textAppearanceSmall" />

Then, I have the following code inside my Activity onCreate():

Button popupButton = (Button) findViewById(R.id.popup);

    ArrayList<String> array = new ArrayList<String>();
    array.add("spendisse vel libero lacinia neque hendrerit posuere nec ac sem. Aliquam laoreet ullamcorper tortor, tincidunt suscipit eros pulvinar dictum. Cras eleifend ante at laoreet facilisis. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridic lorem ipsum");
    array.add("Odio. Quisque at rutrum tellus. Nullam hendrerit nisl non ligula condimentum varius aliquet eget turpis. Cras cursus arcu ornare elit dictum, et aliquet sem condimentum. Praesent mauris mi, malesuada volutpat dictum a, porta non mauris. Cras non sce");
    array.add("Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci veli");
    array.add("ltricies interdum sit amet nec ante. Suspendisse vel dolor dolor. Donec eget mattis lorem. Donec eget quam porta, hendrerit sapien a, interdum arcu. Integer rutrum, ante sed posuere adipiscing, nisl mi malesuada lorem, quis egestas sem augue fauci");

    final ListPopupWindow showRoomListPopupWindow = new ListPopupWindow(SecondActivity.this);
    showRoomListPopupWindow.setAdapter(new ArrayAdapter(SecondActivity.this,
            R.layout.movie_detail_spinner_item, array));
    showRoomListPopupWindow.setModal(true);

    showRoomListPopupWindow.setAnchorView(popupButton);

    showRoomListPopupWindow.setWidth(ListPopupWindow.WRAP_CONTENT);

    showRoomListPopupWindow.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(SecondActivity.this, "Clicked item " + position, Toast.LENGTH_SHORT).show();
        }
    });

    popupButton.post(new Runnable() {
        public void run() {
            showRoomListPopupWindow.show();
        }
    });

Note that all my strings are very long, and my Button has its visibility set to INVISIBLE, not GONE.

And this is what I get:

enter image description here

As you can see, the text has a minimum 3 lines and the width of the items is ok.

I hope to have helped you.

Let me know if it worked for you.

joao2fast4u
  • 6,868
  • 5
  • 28
  • 42
  • How to remove the divider line from the list popup window please suggest solution. – RAHULRSANNIDHI May 23 '14 at 05:28
  • 1
    @ RAHULRSANNIDHI, just use this after calling `show():` `showRoomListPopupWindow.getListView().setDivider(null);` – joao2fast4u May 23 '14 at 09:43
  • will you please tell me the value of @android:attr/textAppearanceSmall ,because without this i get same result as previous – Shruti Oct 29 '15 at 10:56
  • @Shruti That is an android default value. Check this: http://stackoverflow.com/questions/11590538/dpi-value-of-default-large-medium-and-small-text-views-android – joao2fast4u Oct 29 '15 at 11:37