Android :: Instantiating A Layout From An Xml File / Resource Id
Jan 28, 2009
I'm interested in instantiating a LinearLayout object from an xml file that has a LinearLayout file configuration. I want to do this within a method of my Activity. I tried findViewById() but that returned null. I've made sure that the LinearLayout id is the one I'm passing. Note that this layout isn't part of my screen, it's stand-alone, so I want to build a LinearLayout object from the xml.
I have a ListView that I want to populate with objects whose layout is defined via XML. Is there a way to instantiate a layout at runtime, editing stuff like the Android layout ID, etc., and then load the new layout into the list?
I have an XML resource file: <resources> <section> <category value="1" resourceId="@xml/categoryData1" /> <category value="2" resourceId="@xml/categoryData2" /> <category value="3" resourceId="@xml/categoryData3" /> </section> </resources> Using XmlPullParser, on the START_TAG, I can use: int value = parser.getAttributeIntValue(null, "value", 0); to get values 1, 2, 3...however: int resourceId = parser.getAttributeIntValue(null, "resourceId", 0); doesn't work...it just yields the default value 0, or whatever I change the default value (3rd parameter) to be. Does anyone know what I am doing wrong or if this is possible?
This seems like a simple problem to me, but I can't figure it out. I have a fullscreen layout that displays differently if the screen is taller (854 instead of 800 pixels). I've tried putting the xml into layout-notlong-hdpiand layout-long-dpi respectively, but both my WVGA and FWVGA emulators go for the "long" version (as described in the doc).
Is there any way to distinguish between the two besides using the deprecated -800x480/-854x480 qualifiers?
How would I reference this to use as the layout_height in a layout xml file?
@string/config_somePadding is actually the only one I've found that doesn't throw an error in Eclipse (even though there isn't a config_somePadding in values/strings.xml), but it appears to just put an empty string.
In the sdk, they use an integer for animation duration. They reference it like this: android:duration="@android:integer/config_longAnimTime". Is there a way to use values that aren't integers in the layout_height attribute?
I need to display a list of categories from a ListActivity. My data source is a ArrayList of type Category which is implementing the android Parcelable interface. stucture is given at the bottom. it has an id and a title
I need title to be displayed as list text and on clicking the list item, i need to get the id for further processing
It is possible to create a new array of 'title's by iterating the category ArrayList and use
But bu using this method i will get only the title of the category upon click and not the id.
Is it possible to directly use the ArrayList of objects as data source?
Is it possible to build a GridView object in XML with 3 columns and 4 rows of Image buttons? It doesn't seem to have similar containment relationship like LinearLayout or RelativeLayout viewgroups.
I want to do this entirely in an xml layout file. When I put ImageButton xml tags inside a GridView xml body, The layout panel in eclipse is throwing an exception: UnsupportedOperationException:addView(View, LayoutParams) is not supported in AdapterView.
All of the android examples for XmlPullParser pull from a local resource file, and all of the SAX examples pull the XML from a URL. I've been told SAX is faster, so I'm trying to use that to pull data from a local resource file (res/xml/thefile.xml)
The example code I'm working off of is here. So in that example, the code I want to change is:
URL url = new URL("http://example.com/example.xml"); ... xr.parse(new InputSource(url.openStream()));
Instead of using URL, I want to use getXml(R.xml.thefile) Is that possible, or does SAX need to get data from a URL?
I am trying to read a file "words.txt" from a resource. It is a very simple, but large (2 MB), text file that I want to read line by line. I have put the file into /res/raw/words.txt, and try to open it with the following code:
However, I get a java.io.IOException. This is not a "resource not found" exception, so the resource is opened correctly, but the readLine() produces the error.
I tried using the InputStream itself, with the result that read() produces -1, which stands for EOF, as if the file was empty.
I have a text file added as a raw resource. The text file contains text like:
a) IF APPLICABLE LAW REQUIRES ANY WARRANTIES WITH RESPECT TO THE SOFTWARE, ALL SUCH WARRANTIES ARE LIMITED IN DURATION TO NINETY (90) DAYS FROM THE DATE OF DELIVERY.
b) NO ORAL OR WRITTEN INFORMATION OR ADVICE GIVEN BY VIRTUAL ORIENTEERING, ITS DEALERS, DISTRIBUTORS, AGENTS OR EMPLOYEES SHALL CREATE A WARRANTY OR IN ANY WAY INCREASE THE SCOPE OF ANY WARRANTY PROVIDED HEREIN.
c) (USA only) SOME STATES DO NOT ALLOW THE EXCLUSION OF IMPLIED WARRANTIES, SO THE ABOVE EXCLUSION MAY NOT APPLY TO YOU. THIS WARRANTY GIVES YOU SPECIFIC LEGAL RIGHTS AND YOU MAY ALSO HAVE OTHER LEGAL RIGHTS THAT VARY FROM STATE TO STATE.
On my screen I have a layout like this:
The code to read the raw resource is:
The text get's showed but after each line I get a strange character  How can I remove that character ? I think it's New Line.
I am trying to get a video to pop up and play. I can get it to work when I use the first uri (that is commented out in the below code), but when I try to use the second uri (from the resource), I get the following error:
From researching the error, it looks like I might have to declare an activity in the manifest but I'm not sure if that applies here?? Can someone point me in the right direction?
I use this method couples of occasion to load text file to display as help file. But I don't know why the following code didn't work. It seems to hang and logcat says "OutOfMemoryError"? All I did was break this out as an separate activity.
I'm trying to unbundle a binary file (sound1.ogg) and store it on the sdcard.So it starts out as a raw resource (R.raw.sound1) and should end up as a copy named /sdcard/ mydir/sound1.ogg I wrote a method called something like copyResourceToFile() and did a copy via file descriptors / streams. But the target file just isn't right.If I do a buffered read, the target file ends up being WAY bigger - like 10x bigger.If I just do a byte-by-byte read for the size of the original file, the target file is the right size but is not the same file. Is there a simpler (well, correct/working) way to do this thing?
Is is possible to specify data in xml resource file and initialize ArrayList from it? I am looking for a way to use the ArrayList with a SimpleAdapter to bind to a ListView. I am wondering if there is a way to specify my data in xml resource file and init ArrayList from it.
My 1.6 app works fine in both portrait and landscape modes on the default HVGA device. I'm now trying to support it on QVGA devices and am encountering build-time errors I don't understand. In my res/drawable-ldpi directory I have:
startstopin.png startstopout.png and a selector file startstopbuttonimageselector.xml which contains: <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable-ldpi/startstopin" /> <!-- pressed --> <item android:drawable="@drawable-ldpi/startstopout" /> <!-- default --> </selector>
I've 'fixed project properties' and done a 'clean' build, to no avail. The error I get is: Error: No resource found that matches the given name (at 'drawable' with value '@drawable-ldpi/startstopin'). Error: No resource found that matches the given name (at 'drawable' with value '@drawable-ldpi/startstopout'). I've tried adding a layout file in res/layout-small that explicitly references this selector file, but this triggers a similar build error and fails to address the original problem: Error: No resource found that matches the given name (at 'src' with value '@drawable-ldpi/startstopbuttonimageselector').
I generated a new android project in eclipse. I then copied all the files from chapter 3 in my book over to the project. After doing so the R.anything is not available and the resource file is missing. I try to clean the project and generate it but it isn't working. What I can do to fix this error?
Just getting started with developing for Android, so apologies if there's an obvious answer. I'm working on an application in which it's necessary to pro grammatically change the entire structure of the UI. What I would like to be able to do is specify a subtree of the UI in xml, and then instantiate that multiple times as required throughout the tree. For instance, if I need the user to set a numerical value, I'd like to use xml to define all the widgets involved, increment and decrement buttons for instance, and then reference this same file whenever I need a UI of that sort.
I have an application with a multi-instance activity. I would like each instance of the activity to appear in a new task and process. Any suggestions on how to do this? By manipulating the manifest I can cause all instances of the activity to appear in a specific task and process, but I haven't found any way to cause each instance to appear in a different task and process.(The requirement to have different tasks is that we want each instance of the activity to appear separately in the task list; the requirement for different processes is that each instance of the activity is backed by a chunk of native code with global state. We can't change either of these.)
Do Android have any way to instantiate objects without calling any of its constructors? In Java, Sun have sun.reflect.ReflectionFactory.getReflectionFactory().newConstructorForSerialization(), in .Net we have System.Runtime.Serialization.FormatterServices.GetUninitializedObject() but I was not able to find anything like that in the Android platform.