How to reuse EmptyView across fragments? - android

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"/>

Related

Add more than one view in a tab

I am trying to add content in tab view using "setContent(...)" method
TabHost.TabSpec spec = tabs.newTabSpec("tag1");
spec.setContent(R.id.radioGroup1);
spec.setContent(R.id.button1);
when i add more than one item in setContent(...) using different methods it only prefers the last one.
How can i add two view under a single tab, in this case- radioGroup1 as well as button1 ?
Thanks
you can not directly add more then one view but aleternative is you can add container view (e.g. linear layout) and inside container layout you can add as many views you want for sure
this is exactly what you want..nice example of tab
You have to group the two views into a single one - for example a LinearLayout.
It is typical in Android, that when you do stuff in a callback from the system (like e.g. onCreate or onButtonPressed) that the effect only come active after the user code returns and that the last setting 'overrides' previous ones in the same callback, as you have seen.

how to create view instance knowing its id?

I would like to create instances of all my views in application once and during execution pass proper view to Activity.setContentView() - by reference not by id.
So - how to properly create view knowing its id?
Additional question:
How is view instance created when I pass id into Activity.setContentView?
Every time I call setContentView new instance is created?
regards for all
Why do you want to do this? My disclaimer is that holding instances for ALL your views in one activity is excessive and unnecessary. You should probably split the functionality of your code into multiple activities and have one layout associated with each.
If you must use multiple views within the same activity, this type of behavior is probably better done within your layout file (putting multiple views in your layout, then showing/hiding them in your code).
That being said, what you'll need is the LayoutInflater class. Inflate the layout and do what you want with it. If you're looking for a specific view within this layout, use the findViewByID() method on that inflated layout.
LayoutInflater inflater = (LayoutInflater)context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
LinearLayout ll = inflater.inflate(R.layout.your_layout, null);
View v = ll.findViewByID(R.id.view_name);
//Do stuff with view or layout

Android Compound Controls - Inflate from XML

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?

Adding a view on runtime to a ViewFlippers

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);

Creating multiple objects of a view defined in the xml

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?

Categories

Resources