I'm working on an AppWidget that shows a list of elements from my db. I see that there are problems (compatibility mostly) with listview in appwidget, so I am looking for alternative solutions.
My idea was to have a series of TextViews in my layout (i.e. 10 TextView) and fill them from db (the first 10 elements in example). Is it a good solution? Moreover I need to change the visibility of TextViews to hide some of them if elements are less than TextView, but I didn't find how to change visibility of views in AppWidget, is it possibile?
Alternative idea is to add only TextView I need from code, but is this possible in TextView? How can I add TextView?
Thanks.
Related
I am developing an activity with a ListView in which I need to change the current row by another layout by clicking on the row, and I'm not finding any way to do as much as I look (I take hours searching for possible solutions and I have not seen any reference to this problem). I do not know if this can be done in Android, but if anyone has an idea of how to do this would be appreciated.
Thanks in advance.
PS: The ListView control is normal and just want to replace a layout with a different layout. I'm using the API 15.
Use a ViewSwitcher
http://developer.android.com/reference/android/widget/ViewSwitcher.html
A ViewSwitcher is -
ViewAnimator that switches between two views, and has a factory from
which these views are created. You can either use the factory to
create the views, or add them yourself. A ViewSwitcher can only have
two child views, of which only one is shown at a time.
I suggest merging the two layouts in a single one and hide the second one. In your adapter data you should have a flag or something to indicate which layout to display. When you click a row, toggle that flag for the selected item and notifyDataSetChanged() on the adapter. This will make sure the changed layout remains even if you scroll up and down and the row goes off screen.
A more optimized solution is to have different item types in the adapter.
I have a standard layout and i have to populate it at runtime with a number of controls/views i.e. TextView / EditText depending on the number of products that come back from a REST service.
Of course the control I wish to add to the layout at runtime needs to contain a number of views (textview, edittext) etc. I was thinking a custom control to bring all the controls I need I am unsure.
The other idea I had was to inflate and existing XML into my layout but I am unsure if this is possible or if it was or would I control the ID names - inserting more than 1 would cause duplicate id's?
I will try and explain in detail what I am trying to do, we can wrap it in a for loop for test which would count form 1 to 5 hence 5 controls would get populated on my layout.
The custom controls would have a TextView which describes the product. The Edit text where the user can enter freely the amount in numbers using the virtual keyboard and a spinner control to the right of the EditText which would allow the increasing of the EdtiText value.
So all pretty simple eh ? :-) but of could I class all these controls as 1 specific view and I need to a number of them on my layout hence if there were 5 products there would be 5 custom controls, each custom control contain controls i.e. TextView, EditText and Spinner.
How can I accomplish this?
The examples I have seen have been inheriting from VIEW but I need my VIEW (CUSTOM CONTROL) to be a container for a number of other controls and then later be able to dynamically add this new custom control onto my Activity Layout.
What about using a ListView with custom adapter...
check http://www.ezzylearning.com/tutorial.aspx?tid=1763429
http://www.framentos.com/en/android-tutorial/2012/07/16/listview-in-android-using-custom-listadapter-and-viewcache/
You may want to use a ListView with a custom Adapter, and update the adapter with the information from the service.
Things are simple, I want to make a widget with 4 lines and 4 rows in it, on each cell there would be a click-able image and an action set by the user via the Settings page.
The layout is like this:
What layout element is recommended to be used for this scenario ? Should I use a GridView, TableLayout, more Linearlayouts ? Keep in mind that the spacing between items must be the same. I want to make it as light as possible. So, what layout ?
If I decide to use a GridView do you have any simple tutorial about
this? I can't manage to find a way of accessing the GridView from
AppWidgetProvider and set it's Adapter. Thank you.
LE: It seems that GridView is supported starting from Android 3.0.. please correct me if I'm wrong. In this case the only remaining thing to do is add 16 images and for each image add a onClickListener ? Brrr...
If you use a GridView then half of your work is done for you - The only layout and formatting elements you need to consider are on a Global (GridView) and Item level.
Using a GridView will also give your Scrolling functionality and the ability to change your row/columns count based on your device (4x4 on tablet, perhaps 2x8 on a phone).
Creating an extension of BaseAdapter to attach the Grid's children will also give you the flexibility to check items, multi select and will allow you to quickly modify the implementation in future by adding and removing items at will.
If this is simply a 4x4 grid which will always ALWAYS remain the same independent of device and each "Item" will always be the same, Use a RelativeLayout as it will be the most lightweight and efficient ViewGroup.
There is a requirement to have not-so-trivial dynamic list, each record of which consists of several columns (texts, buttons). It should look something like:
Text11 Text12 Button1 Button2
Text21 Text22 Button1 Button2
...
At first obvious way to accomplish that seemed to be TableLayout. I was expecting to have layout/styling data specified in res/layout/*.xml and to populate it with some dataset from java code (as with ListView, for which its possible to specify TextView of item in *.xml and bind it to some array using ArrayAdapter). But after playing for a while, all I found to be possible is fully populating TableLayout programatically. Still, creating TableRow by TableRow and setting layout attributes directly in java code doesn't seem elegant enough.
So the question is: am I at the right path? Is TableLayout really best View to accomplish that? Maybe it's more appropriate to extend ListView or something else to meet such requirements?
Using ListView and ArrayAdapter you can do more complicated layouts than just a TextView. You could specify a LinearLayout with 2 TextViews and 2 Buttons for each row in the List.
here's a similar question
Android: ListView elements with multiple clickable buttons
IMHO it depends on the amount of your data you need to render.
Build layout dinamically via inflate/addView is a quite simple task but is
also more slow than using a custom adapter. with a custom adapter you can
reuse the convertView parameter and then set the values more efficiently
I have a listview with custom rows and that extends SimpleAdapter.
Each row consist of two linear layouts : 1st having two textviews of which one is hidden in horizontal orientation, second having two textviews in horizontal orientation.
Now depending on the value in hidden textview , I want to setcolor for the remaining items for the row.
To put it as simple:
each listview item has some custom colors the value of which comes from the hidden field.
I have done this by overriding getview() for the simpleadapter and returning view for each, but this makes list very slow to render (and that I think is obvious as so much of work for each view before showing it).
Can I do this in some more efficient way ? like making views and then add up to list instead of using xml layout maybe one solution OR any other ? Any help ? Thanks.
If you use convertView in your adapter, I would not expect you to have any particular speed issues. Creating and garbage collecting rows is expensive -- setting some colors on a set of TextViews is not. So, make sure you are using the convertView parameter to recycle your rows.
Here is a free excerpt from one of my books that covers row recycling.