Custom controls/inflating an XML onto an Android layout? - android

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.

Related

Best way to make a layout with many buttons on Android

First I have build a simple android application which had only five optios to select. For this purpose I used five Buttons on main Activity. Now I have more than twenty buttons in a ScrollView to select. What is the best way to represent this kind of application (using buttons in a ScrollView? using TabHost? or with some other widget?)
The app look like this now:
Grid View or List View or Recycler View
the Adapter automatically will add buttons with the names you want, something that I did for my upcoming app.
I made a java class called data which has 'data` for my app.
it has an array of images for my GridView.
SO:
Make a class called data
Add a public final static String[] myArray array of your names, or data
Now, whenever you want to access them, use data.myArray
If you want to access one item ,use data.myArray[itemIndex]
Don't forget, indexes are zero based, not 1
Put your button inside a viewHolder class
find the id of the button in the getView if convertView is null & set the holder as a tag
NOTE : after finding the ID of the button, just leave it don't do anything or edit the text, continue reading please.
Use that array with your custom adapter
as
gridView.setAdapter(new myCustomAdapter(parameter1, parameter2,data.myArray);
use this , I just made it yesterday, added array of buttons feature now. You can just learn it or use it or commit changes.
NOTE :
You can make an array of listeners just like any primitive data type, View.OnClickListener[] and name it, initialize it.
Use grid view. it will be easy to show multiple buttons on screen using grid view.

Android Dev: Generate buttons and text fields based on user input

I'm fooling around with Android and trying to make a simple application to split a bill between friends.
I'm wondering if there is a way to create buttons and textfields based on user input. For example, if the user says he needs to split the bill 5 ways, how can I generate 5 buttons and 5 textfields? Similarly, splitting 10 ways will create 10 buttons and 10 text fields.
Thanks,
Ben
Edit: I should mention I have implemented the input already. I simply need to use the input to generate the buttons and textfields.
What you need is to create a Linear layout that have only a button and textfield then you can dynamically add that layout into your activity main's layout programatically. You can follow this thread on how to add view dynamically to another view
I've had a similar problem. You need to make a custom layout and then use LayoutInflater to "put it into another layout".You can use the return of inflater.inflate() to get the parent layout. then you just use parent.getChildAt(i) in a for loop for all "fields" and getChildAt(whatever the index of the button or text field you want is) to get the buttons and access them programatically. You can also give every Button a unique ID with setId(generateViewId(yourButton)) to access them from other functions.

Which is the better method in Android for creating a dynamic list?

If you are creating a very dynamic list, say, where every row can have a different set of input types plus optional buttons, and the list length is based on another dynamic value, is it better to do this in a list adapter or creating a custom view in a scroll window?
After struggling with list adapters for quite a while now something finally occurred to me- this seems dumb. It seems like I am going through a lot of work keeping track of what spinner is set to what value, which row was clicked and so forth.
For example, say you are showing something like a contacts screen with various details that can be entered about a contact. Some rows will have text inputs (name, address etc), some will have spinners (ie. state, group), some will have checkboxes (like 'favorite' or something). Also, there is an 'add' button that allows you to add another field to edit. Is it worth making this in a list adapter or is it better to populate a custom view, and if the "add" button is clicked, we re-create the custom view, adding a view of the type they want to add?
I hope this is clear.
ListViews (and List Adapters) are meant for data that is to be displayed in mainly similar views. For your example, it is much easier and more natural to have a predefined layout file with the screen and use view visibility so select which views are to be shown. If you need to add views to the screen you can do this dynamically by using findViewById on the layout and then using it's addView method.
Let me know if you need more clarification or sample code...

AppWidget - list of elements without ListView

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.

Custom TextView and ListView versus a GridView suggestions

I have an application that is a task management application. The application uses tabs to separate tasks, calendar, and notes functionality. The primary view by the user is a Task view which is currently implemented as a List with a ListAdaptor class to put all the widgets together in a single row.
Each row contains the following items:
Spinner to allow use to select the priority for a task (i.e. A1, A2.. B1.. D4)
TextView to enter a description of the tasks
CheckBox for the user to indicate if the task is completed
TextView for an optional date field
Button to attach a note to the task
On a phone the above items take up too much room. For example, the spinner component takes up space to display the value as well as the drop down menu. Checkbox and button are also too big for the list view on a device like a phone.
Here are my questions:
Custom Spinner. What I really need in the application is a simple text field that shows the users priority for the task with the ability to click on the text and select from options similar to a spinner component. Should I extend a TextView component to do that or is it possible to modify the look of the Spinner to function like a spinner but display like a text field?
Shrinking components. Is it possible to shrink the default Android components like the spinner, checkbox, and button to better fit in a row with limited size? Do I have to extend components to do this or are their attributes that can do this easier?
Lastly, I have implemented this functionality using a ListView. Would it be easier to control the cells in each row using a GridView instead of a ListView? What are the benefits and disadvantages to using a GridView versus a ListView?
Thanks in advance for an advice on the above questions.
Brett
Why don't you add a click listener to your TextView and show for example a context menu with all your items when TextView is clicked.
You may limit the size of the components by setting their width/height to a desired value. Set text size, paddings and margins of the components to a minimum value as well. Note that components' background may also define paddings, so define your own background drawable with no paddings.
Not many differences. You will provide ListAdapter for both. I would say that GridView is likely used for galleries, ListView being used for tasks like your. ListView also gives you more control over its items.

Categories

Resources