How to create dynamic xml file in res in android - android

I want solution of following:
I have create xml file in data/data/com.example.file/files/abc.xml so during running of app how I get this abc.xml in res/layout and should display on screen.
Thanks.

In general, the XML vocabulary for declaring UI elements closely
follows the structure and naming of the classes and methods, where
element names correspond to class names and attribute names correspond
to methods.
Courtesy goes to #Mike M : You cant create this .XML files used for layouts are pre-compiled .

I'm not sure I have followed your question- are you trying to attach a child view to the RelativeLayout? If so you want to do something along the lines of:
RelativeLayout item = (RelativeLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child, null);
item.addView(child);
the RelativeLayout item will be the container for your child view.

Your question needs to be a bit more specific, but here's a general answer:
If you are using fragments then you should re-create your fragment and then use:
inflater.inflate(R.layout.abc, container, false);
If you are using activities then you should use:
setOnContentView(abc);

Related

Can I refere to any other xml to style my dynamic layouts

using 'for' loop I am creating some dynmic layouts but I don't want to code them. I want to take reference from any other layout xml file. Is it possible?
You can do something along the lines of following:
for (some loop) {
View view = LayoutInflater.from(getActivity()).inflate(R.layout.some_layout, null);
containerView.addView(view);
}

Duplicate layout IDs returning as -1 after view replacement

Short Story:
I have a layout "layout.xml", which gets replaced by another layout "success.xml" after a successful web request. Both layouts have an ImageView that provides the backgrounds to the layouts. These 2 backgrounds both need to be the same, and both are dependent on a user preference.
Longer Story: This all happens in a Fragmnet with an AsyncTask replacing the contentView with "success.xml" in onPostExecute after the web request. This happens as follows:
View view = getView();
view = null;
view = View.inflate(context, R.layout.success, null);
What I tried to do is give both ImageViews the following android:id="#+id/background_image" and then call
ImageView background = (ImageView)view.findViewById(R.id.background_image);
background.setImageResource(R.drawable.bg1);
This background-setting works for the initial view (layout.xml), but on trying to change to "success.xml", I get a NullPointException because background is null.
I've checked and the View's id is set to -1 while the original view's background_image id is set to something sensible and valid.
I've also tried setting the second view's background id like this: android:id="#id/background_image", i.e. without the '+', but still no luck.
The added complication is that it's not just 2 layouts, but about 5 that I need to do this for, so it would be really handy to recycle view id's.
Any help would be greatly appreciated.
Your code for replacing the fragment's view will not do what you want, the original view will remain the same as you change only a reference to that view and not the actual object.
To replace the view of the fragment with the new layout you could have another ViewGroup(for example a FrameLayout) in the basic layout (layout.xml) wrapping your current content(don't forget to give it an id) of layouts.xml(as I understand this is the basic layout). Then, when it's time to replace the layout you could simply do:
// remove the current content
((ViewGroup) getView().findViewById(R.id.yourWrapperLayout)).removeAllViews();
// add the new content
View.inflate(context, R.layout.success, ((ViewGroup) getView().findViewById(R.id.yourWrapperLayout)));
You could avoid adding an extra layout if, by any chance, all your five layouts have the same type for the root view(like a LinearLayout etc). In this case you would use the same code as above but you'll modify the other layouts file to use a merge tag. Also, you'll be looking for the id of the root in the layout.xml layout into which you'll add the content of the other files.
Then you could have the same ids, but you'll have to reinitialize any reference to the views(meaning that you'll have to search for the view again if you store a reference to the view(like a Button field in the fragment class)).

addFooterView for a ListView that is defined in the xml

I've got a ListView, which is fully defined in the xml file. Now I want to add a footer to thi s list so that the last element of the list became a button.
So firstly I do this: View v = getLayoutInflater().inflate(R.layout.add_new, null);, where add_new is the layout for my footer
Now I know that I need to do smth like this:
listViewInstance.addFooterView(v);
But i dont really have that instance as my ListView is defined in an xml file. So what do I write instead of listViewInstance? Is there a way to get its instance somehow from an xml?
Thanks in advance xxx
Use ListActivity.getListView() if you are using a ListActivity or Activity.findViewById() to get the reference to the list view otherwise.

User Interface Design in Android

I want to design the layouts programatically that means without the xml file as per project requirement.
But the terms used programatically is completely different from xml file.Is their any useful tutorial to learn programatically that means without xml file. guide me!
you can create any view you want
a linear layout
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
A Text View
final TextView tv = new TextView(this);
tv.setBackgroundColor(0xFFFF00FF);
tv.setTextColor(0xFF000000);
tv.setTypeface(null, Typeface.BOLD);
tv.setText("Where is Fred?");
tv.setGravity(Gravity.CENTER_HORIZONTAL);
and anything else.
Source
I advise you to spend some time learning about the View class and its popular subclasses such as LinearLayout, RelativeLayout, and so on. (Also, spend a bit of time looking at Drawables.)
When you're creating a layout using XML, you're using XML to define a hierarchy of View objects which, at runtime, are "inflated" into a real hierarchy of View objects that the XML layout file describes. For example, your first XML layout file might be a simple LinearLayout that contains a TextView (note I'm simplifying it for brevity):
<LinearLayout ... >
<TextView ... />
</LinearLayout>
In your Activity you would use this layout using setContentView().
All that XML file is doing is providing a specification, if you like, about the View structure that the system needs to build (or inflate) for you. The end result is that there will be a real LinearLayout object (which is a subclass of View) that has a reference a child TextView (again a subclass of View) together with suitable layout parameters.
To do the above programmatically (i.e. by creating instances of objects and using their methods, rather than inflating from XML) you might do something like (again simplified):
LinearLayout container = new LinearLayout(this);
TextView tv = new TextView(this);
tv.setText("hello");
container.addChild(tv); // Simple example - usually you'd specify layout parameters
setContentView(container);
The basic point I'm making is that, in really simple terms, a layout XML file can be thought of as a kind of 'script' that you can use to tell the system how to create a hierarchy of Views. You can create exactly the same result by programmatically creating instances of View objects and calling appropriate methods on them. Whichever route you take, the end result is the same: a bunch of View objects in memory that represent a View hierarchy.
What you will find is that XML layout attribute names aren't necessarily the same as the corresponding method names, but you can use the relevant API documentation to see what corresponding XML attribute strings are for given methods. For instance, the API documentation for LinearLayout details all of the methods as well as the XML attributes.

Android, Can we have repeated ids in different xml file for views?

Assume that we have 3 different activities. Each activity has its own XML file for drawing UI.
In each XML file we have a TextView (but style of each one is different with the others).
I want to know what will happen if i assign same id to each of them for example android:id = "#+id/textView" for all of them.
When you use setContentView(R.layout.yourlayout);
the findViewById() will get the textview from that particular layout only...so you can assign same ids in different activities..

Categories

Resources