Just something I have been thinking about. It is possible to create an android layout with just a TextView widget and no Layout code (e.g: inearLayout, ScrollLayout), but if I try to add anything else to the XML file all sorts of errors start popping up. Is it possible to create a Layout with just widgets?
Also, if it is, how?
I don't think it's possible inside of an XML layout to have a layout of purely widgets and no ViewGroups. You would for one be creating an invalid XML doc (multiple roots since a widget cannot contain other widgets). Also you need a ViewGroup to hold multiple Views. You can create a merge and then decide on the view group later, however that is just a substitute for a ViewGroup. However it is the closest thing to an XML layout with only widgets in it, surrounded by a merge element as the root xml tag.
Yes it is possible to create layout with a single View (e.g. TextView). But if multiple Views are required you neeed to wrap them with a ViewGroup (e.g. LinearLayout)
Related
I'm trying to make an error page for each of my activities where if there is a network error, it will show this error page. Some activities have two fragments, some have a Recycler view,some have a Toolbar, so on.
I'm trying to implement this showErrorPage() method in the BaseActivity class as a generic method for all activity that extend it.
I'm thinking I would get the root view using binding.getRoot() then I need some way to hide whatever that view is, and replace it with error_page.xml which the root view is layout as for all data binding and it contains a LinearLayout which includes TextView AND ImageView
I would definitely not try to do it the way you mentioned, since it would be unnecessarily complicated. In fact, you should never have to do this, since this isn't the way such a simple interface should be designed. Instead, I would probably create a Fragment which inflates error_page.xml as its root View.
Another approach you could try is surrounding your error_page.xml with <merge> tags and using <include> tags in each of the layouts of the activities/fragments you want to show your error layout (see https://developer.android.com/training/improving-layouts/reusing-layouts.html#Merge). Make sure that your <include> is at the bottom of each layout.
Then in every layout containing your <include> tag, I would set android:visibility="gone", android:layout_width="match_parent", android:layout_height="match_parent" within the body of the include and give it an id. In your method showErrorPage(), you could then set the visibility of the included layout, using its id, to View.VISIBLE.
I have two activities with almost identical layouts. Only the buttons at the bottom are different.
The empty space at the top is an empty EditText that is supposed to show the amount of the expense and is filled in at runtime. Same goes for the empty TextView for the date.
I know it is possible to leave the buttons out of the XML file and fill them in programmatically. But I like the ease of changing an XML file.
Hence my question: Is it possible to choose from two additional XML files at runtime in order to add the buttons?
I imagine that "placeholder views" exist, which can be inflated with a chosen XML layout.
A ViewStub would be a good candidate for this scenario. You can specify the XML which should be inflated at runtime, then inflate it.
ViewStub vs = (ViewStub) findViewById(R.id.my_viewstub);
vs.setLayoutResource(R.layout.my_layout);
vs.inflate();
You can create three buttons in the xml file. And Which you don't want to show, you can invisible it.
It may sound foolish, but I actually can't find anything on it.
Is it ok to add multiple views to the root view of activity in Android?
So for example I could go like this:
setContentView(R.layout.main);
setContentView(gLView);
setContentView(otherView);
Or simply retrieve it as a view
FrameLayout layout = (FrameLayout)this.getWindow().getDecorView().findViewById(android.R.id.content);
layout.addView(view1);
layout.addView(view2);
layout.addView(view3);
layout.addView(view4);
It all seems to work on devices I test, but is it guaranteed to work on all of them?
Or should I artificially create single FrameLayout to add to root view and add everything to this FrameLayout?
More experiments:
1) if I don't use setContentView and print:
Log.d("Test", this.getWindow().getDecorView().findViewById(android.R.id.content).getClass().getName());
I get: android.widget.FrameLayout
2) If I set content view to for example GLSurfaceView and print the same Log.d
It also prints android.widget.FrameLayout
3) And if I dont' use setContentView at all, and simply do
FrameLayout layout = (FrameLayout)this.getWindow().getDecorView().findViewById(android.R.id.content);
layout.addView(myView);
It attaches the view
So I assume that android.R.id.content returns not what you set by setContentView but the parent of what you set, or the actual root view in activity (which turns out is FrameLayout?)
And I am able to add multiple children to it this way, but question is:
Am I allowed to do that?
Yes, it's perfectly fine to add multiple content Views at the root level. The content Views are simply added to a FrameLayout container, and it is best practice to simply use it for your layout if a FrameLayout is all you require for it, instead of adding an additional container layer.
If you are adding a new content View, then you should use the addContentView() method instead of setContentView(), which would cause the existing content to be replaced instead.
Also, it is possible to add multiple Views to the content container in XML layouts as well by using the <merge> tag, which would just replace the base FrameLayout.
Calling setContentView several times will simply replace whatever view you set before. So what you want to do is create a root view that contains the multiple views you want to add and set the root view to your Activity with setContentView. Your second example with the FrameLayout is a good approach.
Your approach is valid at some extent. but its nice practice to call the setcontentView() once in activity.
this is because it will be very easy to maintain the Activity life cycle and reduce the app crash due to layout leak.
I personally call the setcontentView() once. I define all the layout in single XML file. Afterwords I call setVisibility(View.VISIBLE) and setVisibility(View.INVISIBLE) or setVisibility(View.GONE) to show or hide any particular layout containing my certain views.
Edit
Also its very easy to adjust the layout in XML because you can use simple drag and drop and If you are using relativeLayout then it will be very easy to place any view any where. but in Java code its some how difficult to place the views on your desired position
Hope this helps :)
i have to create a layouts dynamically depending on the some action for example depending on action i have created 5 layouts which is horizontal scroll and i want the control of each layout how to go about it please give me some suggestion
Your requirement is not entirely clear. If you want to load layouts from a xml resource fiel you can use LayoutInflater. This will also return a handle to the loaded layout that you can use for further manipulation.
You should use the layoutInflater to load the layout.xml files, later in List add the handlers in for loop.
unless you set the inflated layouts to some Activity to display it has no effect. Hence consider ViewStub and inflate the things inside the ViewStub.
If you are inflating and adding it as a part of some component ( like listView items), then you can get the references using a global ArrayList, to store all those handles.
I am unable to understand your question fully, can you please elaborate your problem?
I have a composite widget that consists of an ImageView and TextView object wrapped up inside a LinearLayout. Since this is used several times inside one of my activities I made it a separate layout and include it multiple times inside the main layout. I understand you can override the view id for the included layout from the tag in the main layout. My question is, is it possible to set things like the image source and textview string from the main layout in XML? Sure, I could do this programmatically but I was wondering if it was possible purely in XML...
From the documentation:
You can include any other layout
attributes in the <include> that are
supported by the root element in the
included layout and they will override
those defined in the root element.
It sounds like you can only override the layout_* attributes. (One might be tempted take a look at the source code to see if something else is secretly supported, but that would be a no-no in terms of forward compatibility.)