Complete layout programmatically by choosing from different XML files - android

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.

Related

Condtional Statement in XML Layout Files

I am making an Android application in which every member has up to 5 children, thus I need to show children dynamically. However, I can't apply conditions on XML layouts.
Any suggestions would be appreciated.
On your xml code you can make your layouts
android:visibility="gone"
to have them not showing and in you mainActivity (java code) you make them show up using
LayoutView.setVisibility(View.VISIBLE);
// or
LayoutView.setVisibility(View.GONE); //depending on the case
I normally make my layouts to have other layouts horizontally or vertically with buttons textviews etc. (child fields) show or disappear at a button click event.
That is what java code is for. You create xml layouts for activities and widgets and then you manipulate them in Java.

creating layouts dynamically and getting control on each layout

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?

Hiding and showing android layouts - How do you mark active layout?

I'm just writing my first android app and in one layout xml file I have four buttons that will display a different layout when pressed. Instead of creating four seperate screens, I've just put the four layouts in the same layout as the buttons with android:visibility="GONE", and then when a button is pressed, I set it's corresponding layout to being visible.
My question is, is there a best practice or suggested method for keeping track of the active layout so that when a button is pressed you can set the active layout back to visibility="GONE" before making the new one visible. I thought I could just set a string value with the ID of the active layout, but then findViewById wont take the string to get a hold of the layout. Any help or suggestions would be great. Thanks!
Why do you use a string to save the ID of the layout? Can you not just use an int since the ID's are such? Or set the visibility of the layouts from the onClickListener you attached to each button?

Is it possible to make an Android Layout, without just widgets?

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)

How do I view a different flipper in Layout view of an xml?

Hey all - I am using a flipper in my main xml file to view multiple layouts in my app.
For instance, if something = 1, the user is shown one version of the flipper, where if something = 2 the user sees a different version.
Is there a way I can view the layout of the xml file for the other flippers I have rather than the main one? Ie, is there a way to switch views of each state of the flipper in eclipse?
Thanks!
I would suggest including all flippers in your xml file. Use android:visibility="gone" for the flippers that won't show by default, or all of them. Then in your code you can toggle them back on with flipper.setVisibility(View.VISIBLE). The others should View.GONE.
For a cleaner XML file (especially if you have a lot of flippers), you can use <include> statements to each flipper source.

Categories

Resources