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?
Related
In my app I have multiple fragments that I want to add an EmptyView if there is no data available. Instead of creating one View per fragment, how can I create one and reuse across them?
About the app, each fragment hits different REST APIs to populate a RecyclerView.
You can create a single view, inflate it and add it to your parent view. Something like
View emptyView = getLayoutInflater().inflate(R.layout.empty_view, null);
parent.addView(child);
Also set the layout parameters as well
If you want it statically added, use
<include layout="#layout/empty_view"/>
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);
}
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.
I have created a compound control that I am reusing in multiple activities. This control contains a bunch of TextViews and Buttons, and most importantly a ListView. I define the XML in a layout file and in the constructor to this compound control, I inflate the XML as such:
String service = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(service);
inflater.inflate(R.layout.map_menu, this, true);
The layout XML contains a ListView, and also in the constructor this compound control will handle setting up the adapter (my adapter extends ArrayAdapter) and array for it, like so:
ListView tableOfContentsList = (ListView) findViewById(R.id.tocListView);
_layerAdapter = new LayerAdapter(context, R.layout.toc_layer_item, _layers);
tableOfContentsList.setAdapter(_layerAdapter);
This compound control is used in two activites - one of these activities calls another. No relation between the two activities is intended.
Here is my problem. When the compound control is created in the initial activity, the above code is called to set the adapter of this control. Then, when the second activity is created and navigated to, the constructor is called again on this second instance of the control. This seems to have a side effect on the first control located in the initial activity. The second control seems to overwrite parts of the adapter from the first control - because basically the first adapter will not be functional once the constructor to the second control is called.
It is my guess that since I am referencing the resource ID of the ListView in both controls, Android is removing the adapter from the first ListView when the second ListView is created - because it sees both ListViews as having the same resource ID? Is this possible?
I have had trouble before in this exact same case - where multiple compound controls are used in different activities (and multiple times in a single activity) - and the problem was due to inflating from XML layout. My solution to that prior problem was to get rid of the inflating from layout, and instead creating the objects through code. This was acceptable because those compound controls were much simpler and contained only two views - however I feel in the above ListView case, where my compound control has at least ten views in it, it is not an acceptable solution to define each view in code. I need the layout XML.
Has anyone ever experienced this sort of clashing behavior when using custom compound controls that are inflated from XML, and re-used in multiple instances?
From my understanding Android should create a new instance of the widgets each time you inflate the xml. Do you have any static members in you compound widget class?
I have defined two views ExampleView1, ExampleView2, ExampleView3 and ExampleView4 in resources.
In my Activity I have an empty ViewFlipper. Based on doing some logic I want to add either ExampleView1 and ExampleView2 to the ViewFlipper and show the view.
Later I want to add based on internal logic either ExampleView3 and ExampleView4.
How do I do this? Is there some tutorial or can someone help me with example code?
Just use the addView method, which ViewFlipper inherits from ViewGroup. If your views are custom ones, you will have something like this:
flipper.addView(new ExampleView1());
On the other hand, if the views are defined inside an XML layout, you will have to inflate them first:
View view = LayoutInflater.from(context).inflate(R.layout.your_view, null);
flipper.addView(view);