• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Creating a Widget Using a Listview

 
Ranch Hand
Posts: 70
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't find any decent tutorials out there on this.

I have been able to make a widget with a button that opens the app.  But if I put a listview into this widget, how can I then populate it with information?

Thanks.
 
author
Posts: 13
Android Google Web Toolkit Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Andy,

A ListView in Android backs onto an Adapter. If you're familiar with Swing, it's a mix of a ListModel and ListCellRenderer. Android provides some default Adapater implementations for you, check out ArrayAdapter for simple static lists, and CursorAdapter for ones that back onto a database.

If you have a static list of options you can link them as resources in your layout XML file, like this:

Then create a values resource named "arrays" (so the file will be in res/values/arrays.xml), and write the options array into it like this:

That will create a really simple ListView, each item will be rendered as a "simple list item" which corresponds to a standard Android layout resource.

For more info about how to build ListView's and other Adapter backed classes, check out the official docs about Building Layouts with Adapters: https://developer.android.com/guide/topics/ui/declaring-layout.html#AdapterViews
 
Andy Crowther
Ranch Hand
Posts: 70
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, thanks for the reply.  To be clear though, I'm fine with ListViews, I use them regularly with custom looks, but how do I put one into a Widget?
 
Jason Brent Morris
author
Posts: 13
Android Google Web Toolkit Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aha, sorry about that. So your question is: how do you put a ListView into a Button or another non-layout widget?

Short answer: you don't

Long answer: you don't, but you can make a ViewGroup (like a FrameLayout or LinearLayout) look like a Button by changing it's style.



Then simply hook an OnClickListener onto the ListView and you're sorted. I would just like to add: before doing something like this, think very very carefully about why you're doing it. Does it make your user interface more intuitive to your users?

If you're not worried about the styling though, you can set an OnClickListener on just about any Android View and it'll work fine.
 
Andy Crowther
Ranch Hand
Posts: 70
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply.  I don't think that is my question.  I think (and stand to be corrected) my question is as the title suggests, how do I create a widget and put a ListView into it    Adding touch responses would be next, but first of all I would just like to know how I set the ListView into a widget.
 
Jason Brent Morris
author
Posts: 13
Android Google Web Toolkit Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aha, Sorry I didn't realise you were talking about App Widgets (when someone says "widget" to me I think of classes in the "android.widget" package). The official documentation is again probably the best place to look: https://developer.android.com/guide/topics/appwidgets/index.html#implementing_collections

They implement the example with a StackView, but the same concept will apply to a ListView. The WeatherListWidget example uses a ListView in an App Widget: https://android.googlesource.com/platform/development/+/master/samples/WeatherListWidget
 
Andy Crowther
Ranch Hand
Posts: 70
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.  Often I can't keep up with the official documentation which is why I was hoping for some other tutorials, but I'll give it another go later. Thanks.

Stackview is quite a good looking widget actually, so maybe I'll try that!
 
When all four tires fall off your canoe, how many tiny ads does it take to build a doghouse?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic