How to add a scrollbar to a listview??

Put your problem here if it does not fit any of the other categories.

How to add a scrollbar to a listview??

Postby xayide » Mon Mar 03, 2008 8:31 pm

Hello!

I tried to add a scrollbar to a listview this way:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <ListView android:id= "@id/android:list"
  2.  
  3.              android:layout_width="wrap_content"
  4.  
  5.              android:layout_height="0dip"
  6.  
  7.              android:layout_weight="1"
  8.  
  9.              android:scrollbars="vertical"/>
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


but it doesn´t do anything.

If i put a global scrollbar to all the layout, it tells me that "ListView can´t have UNESPECIFIED size".

Does anybody have the same problem? how can i solve it?

thank you!
xayide
Junior Developer
Junior Developer
 
Posts: 20
Joined: Tue Feb 05, 2008 1:23 pm

Top

Postby plusminus » Tue Mar 04, 2008 1:38 pm

Hello xayide,

you mean sth. like this :darrow: is not working:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <ScrollView android:layout_width="fill_parent" android:layout_height="wrap_content">
  2.  
  3.       <ListView android:id= "@id/android:list"
  4.  
  5.              android:layout_width="wrap_content"
  6.  
  7.              android:layout_height="fill_parent"  />
  8.  
  9. </ScrollView>
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby xayide » Wed Mar 05, 2008 11:23 am

Yes, exactly that is not working. I´ve tried to put the scrollview inside the layout, outside the layout, before the listview... i think i´ve tried all the possible combinations!!

the problem is something with the listview´s size.
xayide
Junior Developer
Junior Developer
 
Posts: 20
Joined: Tue Feb 05, 2008 1:23 pm

Postby plusminus » Wed Mar 05, 2008 11:44 am

Hello xayide,

usually AdapterViews (like ListView and GridView) create the Scroll-Functionality on their own, like here:

[align=center](there are much more flags when scrolling down.)
Image[/align]

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby xayide » Thu Mar 13, 2008 7:18 pm

The problem is that my listview grows dynamically. I have a chat represented as a list. When a user writes a message, this message is inserted in the list and using a adapter this list is shown in the screen. When the screen is full of messages i have two options: put a scroll or deleting old messages.
The first of them is the one i´m asking here, but puting

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  2.  
  3.    android:layout_width="fill_parent"
  4.  
  5.    android:layout_height="wrap_content"
  6.  
  7.    android:scrollbars="none">
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


gives me that famous error: listview can´t have unespecifiez size

If anyone knows how to do any of the two options will be great!!

thank you very much!
xayide
Junior Developer
Junior Developer
 
Posts: 20
Joined: Tue Feb 05, 2008 1:23 pm

Scrollbar problem

Postby cadlg » Thu Mar 13, 2008 11:36 pm

Hi xayide.
I think that the 'UNSPECIFIED size' problem has currently no solution (please somebody correct me if I'm wrong)

But I was wondering why do you have to delete old messages when the screen is full if you cannot put a scroll.

As plusminus said, ListViews create the Scroll Functionality on their own, so the user can navigate to the items that have been left out of the screen whenever they want (although not by means of a permanent scroll bar), and imo this is better than deleting old messages.

Of course there will be necessary to delete too old messages at some point, but not when they have just got out of the screen (which I guess can be done deleting the items from de source that provides the data to the ListAdapter (i.e. database, array, etc..)).

Regards,

cadlg
cadlg
Experienced Developer
Experienced Developer
 
Posts: 84
Joined: Wed Feb 20, 2008 12:33 am
Location: Guatemala

Top

Postby xayide » Fri Mar 14, 2008 9:55 am

What i mean by "deleting old messages" is that when the screen is full i want old messages not to appear in the screen and only news appear.

The scrollbar appears, but everytime a new message is send, the listview goes to the begining of the list and the user has to use the scrollbar to go to the last message.

I need to know if there is anyway to show always the last message and use the scrollbar just to go up in the screen, to see old messages. Like if it was a chat.

Regards!
xayide
Junior Developer
Junior Developer
 
Posts: 20
Joined: Tue Feb 05, 2008 1:23 pm

Postby cadlg » Fri Mar 14, 2008 4:06 pm

You just need to know what is the total number of items in the list (i.e. incrementing a counter each time you add a new message), and then use setSelection(itemsCount int) to select the last one.

Hope this helps.

cadlg
cadlg
Experienced Developer
Experienced Developer
 
Posts: 84
Joined: Wed Feb 20, 2008 12:33 am
Location: Guatemala

Postby xayide » Fri Mar 14, 2008 4:29 pm

Thank you very much!
setSelection method worked!

But a strange thing happens: the list just fills half screen, it doesn´t go till the end if i don´t click in any of the items... i have read somewhere that it might be a bug of the sdk...

Do you know something about this?

thanks!!

regards!
xayide
Junior Developer
Junior Developer
 
Posts: 20
Joined: Tue Feb 05, 2008 1:23 pm

Workaround

Postby cadlg » Fri Mar 14, 2008 5:26 pm

I really don't know if there is a solution, but I tried injecting a key event and it worked... :roll:

I took this code from a plusminus tutorial. (Tutorial) (just replaced kEvent.isDown() with kEvent.getAction()==kEvent.ACTION_DOWN, because the isDown method is now deprecated)

First, you need to get the window manager service
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     final IWindowManager windowManager = IWindowManager.Stub
  2.  
  3.     .asInterface(ServiceManager.getService("window"));
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


After the setSelection call, you start a thread, to inject the key event:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. new Thread(new Runnable() {
  2.  
  3.             public void run() {
  4.  
  5.                  /* Simulate a KeyStroke to the down key. */
  6.  
  7.                  simulateKeystroke(KeyEvent.KEYCODE_DPAD_DOWN);
  8.  
  9.             }
  10.  
  11.        }).start();
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.        
  2.  
  3.     private void simulateKeystroke(int KeyCode) {
  4.  
  5.         doInjectKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyCode));
  6.  
  7.         doInjectKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyCode));
  8.  
  9.    }
  10.  
  11.  
  12.  
  13.    /** This function actually handles the KeyStroke-Injection. */
  14.  
  15.    private void doInjectKeyEvent(KeyEvent kEvent) {
  16.  
  17.         try {
  18.  
  19.              /* Inject the KeyEvent to the Window-Manager. */
  20.  
  21.              windowManager.injectKeyEvent(kEvent.getAction()==kEvent.ACTION_DOWN, kEvent.getKeyCode(),
  22.  
  23.                        kEvent.getRepeatCount(), kEvent.getDownTime(), kEvent
  24.  
  25.                                  .getEventTime(), true);
  26.  
  27.         } catch (DeadObjectException e) {
  28.  
  29.              e.printStackTrace();
  30.  
  31.         }
  32.  
  33.    }
  34.  
  35.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


It must be done in a thread because if you try to inject the key event in the UI thread, the system sees your activity as non-responding...


Regards,

cadlg
cadlg
Experienced Developer
Experienced Developer
 
Posts: 84
Joined: Wed Feb 20, 2008 12:33 am
Location: Guatemala

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 27 guests