I'm currently writting an android application and I'd like it to be well written/designed.
Right now I have a set of multiples views (2 images views, 3 textviews) in a relative layout that I use pretty often. Is there any way to create a custom view that regroup them all?
I've took this screenshot to explain what I've done right now:
As you can see, right now I've just copied/pasted my framelayout which contain all my views... Is this the proper way to do it or there's a cleaner solution?
Thanks guys
No a better way would be to put all the content you putting in you layouts in a ArrayList of objects.
and then create an ArrayAdapter to populate a ListView from this objects the way you want.
The advantage of this technic is that you gain a Views recycle mechanism that will recycle the Views inside you ListView in order to spend less memory.
In Short you would have to:
1. Create an object that represents your data for a single row.
2. Create an ArrayList of those objects.
3. Create a layout that contains a ListView or add a ListView to you main layout using code.
4. Create a layout of a single row (you already have it).
5. Create a ViewHolder that will represent the visual aspect of you data row from the stand point of Views.
6. Create a custom ArrayAdapter that will populate the rows according to you needs.
7. Finally assign this ArrayAdapter to your ListView in onCreate.
You can get an Idea of how to implement this by reading this blog post I wrote:
Create a Custom ArrayAdapter
If you do it in code instead of the interface builder you can use LayoutInflator to create a view from your xml each time, and then add those views as subviews to your LinearLayout. This would also allow you to vary the number without having to copy paste each new view.
LayoutInflater layoutInflator = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout ll = (LinearLayout) findViewById(R.id.yourLayout);
for(int i=0; i<numOfSubViews; i++){
View view = layoutInflator.inflate(R.layout.yourFrameLayout, null);
ll.add(view);
}
Related
I was wondering if I want to create a simple list of buttons, a recycler view will be needed or if I can make do with a listview. Thank you
The simplest method would be to create the buttons and add them the view. I strongly recommend you to not do it the following example is for demonstratiom purpose:
onCreate...
LinearLayout root = findViewBy...
for (item: dataList) {
Button btn = newn Button(this);
btn.setText(item.text);
root.addView(btn);
}
In this example Im using a linear with vertical orientation, that should be inside a scrollview.
That is bad because every view is in memory at the same time. If you have just 2 or 3 buttons then there is no problem but if the number raise to hundreds then there will be memory usage problems.
This is why ListView got deprecated, because every row was rendered. Large data set made the UI slow. Instead RecyclerView literally recyle the views as the name implies. In memory there is only the view on the screen and a bit extra, so when a view leaves the window is available to be reused by the incoming row.
By the comments I can see you are also confused with views and viewgroups. A TextView is a View it can not have another View inside. If you only need to have a click, then TextViews can use a setOnClickListener, other is the case if you need the appearance of a button. Anyway, when you create an adapter you can add any layout you want.
A list of buttons can be achieved in both the ways. But ListView is outdated. So better use RecyclerView
Suppose I have a list of items like this:
List<User> mPersonList=getAllUser();
Then I want to display them in the activity, I have two ideas:
1 Use the ListView
I will create a Custom adapter extending from ArrayAdapter to bind the data to the view. This this the general idea.
But sometimes, the parent of a ListView may(must) be a ScrollView, so someone suggest me use the Linearlayout.
2 Use something like the LinearLayout.
Create one LinearLayout for each item, and add them to the parent view.
For example:
ViewGroup parent=(ViewGroup)findViewById(R.id.xx):
for(Person p : mPersonList){
LinearLayout ll=new LinearLayout(...) //created by constructor
// or ll=mlayerInflate.inflate(R.layout.xxx);
TextView tv=new TextView(..)
tv.setText(p.name);
ll.addView(tv);
parent.addView(ll);
}
I wonder if which is better when performance considered?
ListView will be much better for performance than implementing the same in a LinearLayout.
The ListView will only inflate as many views as will fit on the screen at any one time. Inflating views is much more expensive than changing the values of a view, so this is much better.
EDIT:
However, you are right that if you are putting the ListView inside a ScrollView it will not work correctly. in that case you should probably use a LinearLayout instead, or perhaps re-work your layout so that you don't use the ScrollView parent (such as using ListView header views etc)
I have one question for list view. At the time of creating list item in getView() method, which is a good option for list view. Creating views through coding or inflating view through xml. I am thinking about memory utilization & performance of list view.
Normally list item contain one product image with their name & 3 line description. Means one Image View & two text view.
Creating Views through code is usually not recommended and is justified only in cases when you can't deal without it. Using XML is always best practice so you should use this approach no matter where you're using your Views. Hope this helps.
You need to recycle views. Are you using a VieHolder?. My suggestion is use a view holder inflate using xml. ViewHolder will increase performance of ListView.
http://www.youtube.com/watch?v=wDBM6wVEO70. Check this link.
We are developing an Android project. In that project we need to create a list view with multiple objects.
Inside each list view item we need to show Name, Mobile, Checkbox1 and Checkbox2
We tried with various options and we are clue less how to get this done.
You can create an .xml file, which contains a Layout (LinearLayout, RelativeLayout etc) and inside that layout put some Views like TextViews for the Name and Mobile and 2 CheckBox items for your checkboxes. I recommend this tutorial for more on ListView issues.
You need to define your own row layout and also your own ListAdapter implementation. The adapter needs to override getView to return your custom row view. There's a good example here that extends an ArrayAdapter. There's another example here that extends a CursorAdapter. Search the web for android custom listview row to get lots of other examples.
I have to dynamically add a list of views (the views use RelativeLayout). Can I do this by specifying the view definition in xml and instantiate multiple objects off it? This gives the usual benefits of separating the view part from the code (for e.g., making it easy for the UI guys to alter things) or Is going the ArrayAdapter the suggested/only route?
are you saying that you want to do this?
View v1 = (View) findViewById(R.id.someView);
View v2 = (View) findViewById(R.id.someView);
If you do this, you will merely have 2 references to the same view; it does not create two separate View objects. However, if you want to make a vertical list of views, look into ListActivity. in this case you will make a layout xml that will be used for every item in the list. you will need to implement a ListAdapter, or use a SimpleArrayAdapter.
does that help?