I'm trying to implement a .java splash screen in my Monodroid app in Visual Studio, anyway I want the splash screen to get it's content view from a resouces layout. I'm trying to get it like this:
setContentView(R.layout.AppSplash);
Also tried with:
setContentView(Resource.layout.AppSplash);
And also:
setContentView("#layout/AppSplash");
And I get error messages like this:
package R does not exist
Where R changes for Resources or:
cannot find symbol
symbol : method setContentView(java.lang.String)
location: class SwimmerTimesCalc.SplashActivity
setContentView("#layout/AppSplash");
When I try the #layout/AppSplash option
How can I access the Monodroid resources to set the layout of my Splash Screen?
This should help you get started.
If you were wanting to use a layout specifically for creating your own splash screen and then using that resource to display it in your activity then you can use something like this.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/SplashScreenLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#+id/SplashDefault"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:scaleType="centerCrop" />
</RelativeLayout>
Then in your activity you can just set the content view.
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.SplashLayout);
}
I figured out how to access the resources, since the debugger couldnt access the resources I went to the generated resources file that is in the project folder under \obj[This can be debug or release]\android\src and it's called R. While browsing that file I found the layout I was trying to use like this:
public static final int appsplash1=0x7f030002;
I took the resource value from there that is 0x7f030002 and used it like this:
setContentView(0x7f030002);
Anyway as the resources file is autogenerated, adding another layout that goes alphabetically before this may require to do this proccess again.
Having the same problem here. One can find the id of the resource in runtime by it's name and folder like this:
int iconResourceId = context.getResources().getIdentifier("icon", "drawable", context.getPackageName());
So, in the Java files one can use such lookups instead of R.drawable.icon and it will work.
This however is slower (because the lookup is implemented inefficiently on Android) and one would still hard-code the names of resources as strings. If one moves or renames resources in the Mono project, the Java files won't know about this.
Also, one has to type all resource names in lower case, cuz Mono converts the name from .NET-style like "Icon.png" to Android style "icon.png".
Related
I have an activity that is displaying to user. On the activity I have a button. Every time when user pushes the button, the app will load an xml file from a remote server. The xml file is actually a layout designed for the application. Right after the new xml file is loaded and stored in the newLayout variable, I would like to set the xml file to replace the current layout that is showing to user.
String newLayoutStr = "";
onClickButton() {
newLayoutStr = loadNewLayoutFromRemoteServer();
}
// set the layout contained in newLayoutStr as new layout of the current activity
Any comments?
This will not be possible with what your asking. Android relies on Resource identifiers using a class call R which is automatically generated. The file contains public static final int references for various class values in your application. The classes referenced here can be found at http://developer.android.com/reference/android/R.html
This class is than used to reference the various resources in your application, one of which is layout. So the system expects all layouts defined in xml to be included in this generated R class.
So in one sentence: you will not be able to accomplish this the way you want.
What i would do is have a json response and convert it to dynamically generated layout.
for XML you download test.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<TextView android:id="#+id/textViewTest"
android:layout_centerInParent="true"
android:gravity="center"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
</RelativeLayout>
If you want to have this layout created in your app create a json response:
{
"layout_parent":"RelativeLayout",
"layout_height": "match_parent",
"layout_width": "match_parent",
"layout_children": [
{
"layout_child":"TextView",
"layout_child_id":"textViewTest",
"layout_height":"wrap_content",
"layout_width":"match_parent",
"layout_gravity":"center",
"layout_centerInParent":"true"
}
]
}
i would then parse this to a model object and use that model object to build your layouts dynamically. In order to add the fields though you will need to have some layout you defined in xml to be able to add more fields to in your app.
As far as I know its currently not possible to load Layout dynamically at runtime that were not available at compile time. This is because the LayoutInflater needs a resource ID(from the R class) and this is determined at compile time. An alternative will be to implement a custom LayoutInflater(by borrowing some ideas from the LayoutInflater class) that can do this though this will be very slow as a lot of optimization is done at compile time
I'm not sure this can be done at runtime from memory. You may be able to marshall the XML and save it to the SD card, then load it as a resource using the AssetManager
From there you can inflate the XML using getLayoutInflater().inflate(YourResourceID, TheRootView)
I have been working on a simple android tutorial and while browsing through the project folders I found this R.java file in gen folder...
When I opened it seemed to me as a mess...
first R itself is a class.
it had multiple Inner classes defined within eg drawable,id,layout,etc.
and that inner classes had lots of variables declared as below which were assigned with hex values
public static final int addr=0x7f080003;
...
...
and much more
R is auto generated and acts as some pointer for other files
Questions for R.java
what it is basically for
how it works
why
values are in hex
what role did it performs while the actual application is running
"Acts as some pointer to other files" is actually absolutely correct, now the question is which files it points to how it is done.
What does it contain?
R file contains IDs for all the resources in the res folder of your project and also some additional IDs that you define on your own (in the layouts, for example). The IDs are needed for the Android resource management system to retrieve the files from the APK. Each ID is basically a number which corresponds to some resource in the resource management system.
The file itself is needed so you can access or reference the resource from code by giving the ID of the resource to the resource manager. Say, if you want to set the view in the activity, you call
setContentView(R.layout.main);
main in the R file contains the number which is understood by the Android resource management system as the layout file which is called main.
Why is it better than just plain file names?
It's harder to make a mistake with the generated fields. If you write the field name incorrectly, your program won't compile and you will know that there's an error immediately. If you write an incorrect string, however, the application won't fail until it is launched.
If you want to read more on this topic, you should check the Android documentation, especially the Accessing Resources part.
This holds your resource ids. So when you do something like
TextView tv = (TextView) findViewById(R.id.mytextview);
it looks up your id here for that View, layout, etc... This way the app has an easy way to look up your ids while you can use easy to remember names. Anytime you create a resource it automatically creates an id for it and stores it here. That's why you never want to try and edit this file yourself.
One way to think about how valuable R.java is, imagine a world without it. Its amazing how android brings the xml and java world together to help avoid coding the UI manually completely. With legacy java building UI using the java language was a pain. Invaluable.
With Android you can not only build your UI using only xml, but also see it while you build it. Invaluable.
Every element in the xml can be referenced in the java code WITHOUT writing a single line of code to parse the xml :). Just R.id.nameOfElement. Invaluable.
Rapid development is beautifully done in android. Imagine if iPhone would have 5000 screens to fit that one piece of code, they would crumble on their XCode. Google has done a wonderful job with just R.java. Invaluable.
The library project looks fine, but as soon as I import it to my main project, it shows me errors on each line which is referencing a resource:
id cannot be resolved or is not a field
The main project shows no errors.
Of cause I ask myself where android knows where to import the resources from e.g. in lines like that:
RelativeLayout menuLayout = (RelativeLayout) this.findViewById(R.id.menu_layout);
But this works neither:
RelativeLayout menuLayout = (RelativeLayout) this.findViewById(net.bla.library.R.id.menu_layout);
Any Ideas?
EDIT: what I found out is:
As soon as I include the library project, Eclipse duplicates the gen/net.mylibrary.R from the library into the main app (so there are 2 packages in the gen folder now: the one from the app, and the copied one from the library). strange thing is: R.id is not found in the copy. There are some other differences too, like the copy uses an additional "final" for its definitions.
I really have no clue why this might happen. Someone?
Eclipse sometimes likes to import android.R, and this causes errors similar with you are experiencing.
Look for the import at the top of the file, and remove it.
As it's stated on "Using Eclipse | Android Open Source":
Note: Eclipse sometimes likes to add an import android.R statement at the top of your files that use resources, especially when you ask eclipse to sort or otherwise manage imports. This will cause your make to break. Look out for these erroneous import statements and delete them.
I had a similar problem and I managed to resolve it.
Here is what I did:
When I call the main activity of my library project, the activity's onCreate method calls setContentView method and uses R.layout.main as a parameter.
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.main_button1);
btn.setOnClickListener(this);
findViewById returned null.
It seems that main.xml of the library project is being overriden by the main project's main.xml.
So I simply created a new xml layout main_libname.xml with the same content as library's main.xml and used this layout as a parameter for setContentView.
It did work!
Check out if the resources are inside the res folder. All the resources must be inside.
0 Proyect
0.1 src
0.2 res
0.2.1 layout
0.2.1.1 main.xml
0.2.1.2 menu_layout.xml
0.2.2 drawables
.....
Maybe you are putting the data in project folder.
I was also having the "cannot find symbol variable ..." error on a R.id item defined in a library project. Notably, only R.id symbols were not resolvable in the main project even though the other R fields (eg. R.layout) were clearly visible.
Fortunately, I was able to stumble upon on a solution. It turned out that in my case, I had a resource (a layout file) defined in my main project which had exactly the same name as that in the library project.
This was what this looked like filesystem-wise:
top_level_project
main_android_proj
src
main
res
layout
activity_main.xml
android_library_subproject
src
main
res
layout
activity_main.xml
My sub-project's activity_main.xml had an id like so:
<RelativeLayout....>
<TextView android:id="#+id/special_text" ... />
</RelativeLayout>
And this is how I try to reference the id in my main project's java code:
MainActivity.java:
public void onCreate() {
setContentView(R.layout.activity_main);
TextView tv = (TextView) findViewById(R.id.special_text);
tv.setText("Hello Android");
}
To fix this issue, I renamed the offending file in my main project to something else.
It seems that you can't have duplicate layout names in your resources.
Indeed, the sdk docs do point out that you should be careful with resource name conflicts when using library projects:
Resource conflicts
Since the tools merge the resources of a library
project with those of a dependent application project, a given
resource ID might be defined in both projects. In this case, the tools
select the resource from the application, or the library with highest
priority, and discard the other resource. As you develop your
applications, be aware that common resource IDs are likely to be
defined in more than one project and will be merged, with the resource
from the application or highest-priority library taking precedence.
I'm new... just finished my first eclipse/adt tutorial.
I don't see see anything in the manifest that points to res.layout.main.xml or res.values.strings.xml.
QUESTION: how does android find these xml's?
thanks,
Shannon
All the xml files get sent to the R.java class. They're assigned specific integer IDs that can be referenced in your java code.
when you say setContentView(R.layout.main) you're getting the integer ID and passing it to the content view. This R class then redirects it to your xml.
You can get to these files using the R class. For example when you want to set your contentview to main.xml you do it like this setContentView(R.layout.main);
if you want to reach a String from a XML you can do it like this getResources().getString(R.string.appname);
Also take a look at the Application Resources section of the Dev Guide http://developer.android.com/guide/topics/resources/index.html
In some code we're trying out, coming from various tutorials, we end up missing R.id, which should be generated in R.java, obviously. We can add it in (by analogy with other "first" Android projects we've done), but as this file is auto-generated, anything we do like that just gets overwritten ultimately.
public static final class id
{
public static final int Button01=0x7f060004;
.
.
.
}
Was there a construct to put into strings.xml, main.xml, etc. that causes this to be generated?
(Yeah, we're total noobs. Sorry.)
Thanks for any help you can give,
Russ Bateman
Say I have an XML file with the following content:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
>
<ListView android:id="#+id/ListView01"
/>
</RelativeLayout>
I save it in res/layout/ .
Then R.id.ListView01 gets automatically created.
You might want to look at the Notebook Sample application and how it's organised. At least that's how I got familiar with androids organisation.
R.java is auto generated by Eclipse. If it is not built, this means you have probably an error somewhere in your xml files, or a resource with a name that is not allowed.
Sometimes it is just Eclipse doing strange things, in this case, you can try :
Project > Clean > all project
Then let Eclipse work.
Sometimes it solves the issue. If not, it's highly probable that you have an error somewhere in your resources.
To create this file, Eclipse gathers the ids you declared in your xml files with #+id,but also the layout names, images names, string names, ...
It appears that project/res/layout/main.xml contains the constructs that lead to the generation of id in R.java. (I'm not limiting the source for these only to that XML file.)
Look specifically for android:id in various widgets (I think they're called) such as TableLayout, TextView, EditText, etc. There will be corresponding
public static final class id
{
public static final int x=0x7f05006;
public static final int y=0x7f05000;
public static final int z=0x7f0500c;
.
.
.
}
where x, y, z correspond to the (TableLayout, TextView, EditText, etc.) identifier coded immediately after the "#+id/" construct in the XML file (and the initialized value in R.java is magically generated--you don't have to worry about that).
Thanks to all who contributed.