Issues in referencing new elements in an existing Android application - android

I have an existing android app. I wanted to change its menu structure without disturbing the existing code. For this I added 2 new java files and relative xmls in the existing project and updated the manifest.xml with the new starter activity.
The issue which I am facing is in the java code. I am not able to reference the elements of the new xmls xxx.findViewById(R.id.xxx)
The error coming up: cannot be resolved or is not a valid field
Please suggest something, I am a newbee

When you build an application, Android will create a file called "R.java". This file basically is an index to all resources in your project (strings, layouts, drawables, etc.). If you add new resources by hand, you cannot refer to these resources because they have no ID in the R class.
According to this page you can use the aapt tool to (re?)generate the R file, but I have no experience with this myself.

Related

Can I design the UI in Xamarin.Forms by using XAML

I'm trying to create a cross platform app using Xamarin.Forms. As far as I know, the UI will be created from the code and the .axml file will be generated automatically.
Can I modify the .axml file to edit the UI? I tried editing but all that comes up is what is written in the code. ie: hello forms
UPDATE
public static Page GetMainPage ()
{
return new simplerow ();
}
In Xamarin.Forms you can create your pages from markup definitions that are shared across all platforms.
Typically you will write all your content pages using Xamarin.Forms, however you can mix-and-match native pages into an application should you so wish.
These shared common pages, written in Xamarin.Forms, will reside maybe in a PCL project, or a Shared Project so these can then be re-used in the platform-specific projects, each targeting a specific platform OS.
You can write these common pages, either in code, or in XAML. You can even choose to write some pages one way, and some the other if you so choose.
A Xamarin.Forms page is processed at runtime through the interpretation of the page composition that has been created.
Each control that is specified on a page, has its own platform specific renderer, behind the scenes, that will produce output that is targetted to that OS.
When writing Xamarin.Forms pages, for-the-most, you will start to learn a new way of creating pages that is abstracted from the intricacies of creating mobile applications on each different platform OS.
There is therefore no editable .axml that is generated etc as you will write your pages using Xamarin.Forms markup and controls, and even your own or other custom-controls to produce your own application pages.
The following link shows some examples of how to write XAML pages.
The following link shows some examples of how to write from code-behind pages.
Along with the previous answer re: .xaml instead of .axml, you need to remember to change the startup code in app.cs to use your new .xaml form. Replace the "new ContentPage {...};" with "new MyForm();" (where "MyForm" is the name of your shiny new XAML form).
EDIT: Downloaded the project from the dropbox link. Comments below...
I see several issues here. I think you may need to go through the walkthroughs and sample applications provided by Xamarin to get up to speed with the concepts behind XF apps.
First, you are trying to use an Activity as your application's page. In a Xamarin Forms app, it must be a View of some sort, not a platform-specific visual such as Activity.
Second, remove the "test.xml" file from your Android project's Resources/layout folder; while XAML files are indeed XML, they have an 1) have a file extension of .xaml and 2) belong in the shared project.
Here's what you need to do to get your project working: (I'm assuming you're using VS here, under Xamarin Studio, it's slightly different.)
Right-click your "testforms" shared project
Click Add from the context menu and select "New Item"
In the dialog that appears, select "Forms XAML Page" and in the Name area enter a name (such as "MyForm")
(If you're using XS, choose "New File" and "Forms ContentPage")
This will add two files to your project: a XAML file containing your layout (e.g.: MyForm.xaml), and a code-behind file (e.g.: MyForm.xaml.cs).
Open the XAML file, and modify the Label element so that the Text attribute is
Text = "Hello, World!"
Modify the body of GetMainPage in your App.cs to the following:
return new MyForm();
Run the app
Hope this helps!
You got it wrong. Forms are created either through code or XAML. No axml or anything persistent is generated at platform level, everything is done in runtime(XAML is sort of compiled at compile time).
So, modify either code or XAML if you wish to change something. Or, if you need something more demanding, than consider either subclassing an existing Renderer or create you own.

what R.java file actually does and how

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.

Cannot override library's xml resource with png resource in application?

I have an Android library MyLib containing everything I need for my app (targeting Android 2.2). This library has an XML resource:
drawable/main_background.xml
In my Application MyApp project I reference MyLib. Here I want to override specific resources (i.e. branding). So I added a background image in MyApp:
drawable/main_background.png
Eclipse keeps giving me this error:
[com.mycom.mylib.myapp] res\drawable\main_background.xml:0: error: Resource entry main_background is already defined.
[com.mycom.mylib.myapp] res\drawable\main_background.png:0: Originally defined here.
How can I override the resource in the library project?
You cannot simply override resource ID (it's the resource ID you are overriding, not the actual file) with a file with different extension in Android SDK. However, you can do the trick by putting in your project xml file with the same name (main_background.xml) and fill it in a proper way to display your new file (main_background.png), which you need to rename earlier. All syntax you need is descibed here:
http://developer.android.com/guide/topics/resources/drawable-resource.html
, in your case it could be simply (assuming you put this in your non-library project as main_background.xml, and you have your new png as main_background_new.png):
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/main_background_new" />
With above solution, you could refer to #drawable/main_background from your project and it should use your file included with that project, instead of a library one.
[com.mycom.mylib.myapp] res\drawable\main_background.xml:0: error: Resource entry main_background is already defined.
[com.mycom.mylib.myapp] res\drawable\main_background.png:0: Originally defined here.
I don't believe you can have the same file name even with different extensions. Try naming the png something else.
Now, i've not used overriding, So this seems odd as you'd expect this to be how you override the asset. However i think you've either got the two assets in your lib named the same. And that in your project it might be ok to have an asset with the same name. I would however check that its ok to have different types. XML is different than png, and if you access the asset from code you could get type errors.
Let me clarify the above point. I understand that a library project can have an item with the same Resource ID as an item in your application.
However the error above suggests that both main_background.png and main_background.xml are in the same project ([com.mycom.mylib.myapp]) which i don't believe is correct.
Further reading
This page describes the various types of project including the library project http://developer.android.com/tools/projects/index.html
Now i don't know where i got the impression from but having looked again it simply doesn't state anywhere that you can override a resource by using the same resource name. God knows why i thought that was a feature.
So no, the same rule applies as far as i can tell, that resources have to be named uniquely even across library projects, otherwise the generated resource ids will conflict. (The error your getting)
What is explained is how resource conflicts are managed.
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.
The system will use the resource with the highest priority, discarding everything else. Whats odd, is that you would think that a compile error wouldn't occur as the compiler should be discarding the resource. This makes me believe that the original poster had the similarly named assets in the same project, and not across the lib and project.
I haven't read anywhere that this is actually an intended feature. Got any links to say otherwise? (comment them)
So one 'solution' to this problem, which I do not consider to be an answer is the following:
Define an XML document in the library in question (we'll call it bunny.xml), and have it refer to another xml of a similar name (bunny_drawn.xml) with the actual content to be displayed.
Then, in the target project, override bunny.xml with another and use it to refer to an image with a different name instead - bunny_image.png
This does not however solve the problem, firstly because we aren't technically overriding a png with an xml (although the effect is somewhat close to that). Secondly because one of the key features of overriding resources is they are overridden, i.e. they are NOT compiled into the APK:
the tools ensure that the resource declared in the application gets
priority and that the resource in the library project is not compiled
into the application .apk
But the bunny_drawn.xml will still be compiled in! We can sort-of overcome the second point, by not only defining the image to be replaced in the target APP, but also replacing the old target bunny_drawn.xml with a blank xml. (or, as Fenix pointed out, you can have the contents of bunny_drawn.xml inside bunny.xml in the first case - the fact still remains that the resource ID can't be replaced...)
So my final conclusion is that this need to be submitted as a bug in the Developer Tools.

Android - Multiple .apk file from single code base

I had developed 3 applications in android where the major functionalities are the same but the UI looks different. Images and the background color of the screens are different.
NOw, i want to create a single code base from which i can generate multiple .apk files for the 3 apps.
I tried creating 3 different packages for src folder for the 3 apps. But i dont know how to set the res folder for these apps.
Need pointers on creating a single code base from which we can generate multiple .apk files which includes only the respective src and res folders.
Use an Android Library Project that contains all your common code.
Create separate Android projects that reference the Library Project (you will need to copy your Manifest into each of these and make sure all components are declared with their full Java package name).
Put any resources specific to each app (drawables, colors etc) into the individual project resource folders and they will override similarly named resources in the library project at build time.
i think the best option is to use ant, you'll need to add an ant target for each build and change the resource folder.
if you use the generated build.xml, the res folder is defined like this
<property name="resource.absolute.dir" location="res" /> so you'll want to override that
Can't you put all of your common code into a library project and then just reference that project from each of the 3 unique projects that each contain the relevant resources.
Update: This answer is now obsolete when using the Gradle build system.
Why don't you use a single application, that does three different things based on SharedPreferences values set by the user, or from context at install time. If you really want to separate, you can have three different activities, and you decide which one to launch from a silent main Activity that redirects to either of the different ones.
An alternative is to have a unique activity that inflates itself dynamically from 3 different layouts at onCreate time.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (...custom check for layout... equals(layout1)) {
setContentView(R.layout.main_layout1);
} else if (... equals(layout2)) {
setContentView(R.layout.main_layout2);
} else if (... equals(layout3)) {
setContentView(R.layout.main_layout3);
} else {
throw new IllegalStateException("Unknown layout!");
}
... your onCreate stuff....
}
It will make code maintenance easier (only one code source to modify, only one version-list and changeset to maintain)
Check here:
How to use SharedPreferences in Android to store, fetch and edit values
I would suggest using Gradle flavors.
It seems to explain all the basics really well. I just finished converting to Gradle today, and it works great. Custom app icons, names, and strings, etc.
As the website explains, part of the purpose behind this design was to make it more dynamic and more easily allow multiple APKs to be created with essentially the same code, which sounds similar what you're doing.
Also see a recent question I had, referring to your project structure and using custom code for each app.

Type R is already defined error

How do I fix this? I already tried removing the R.java and cleaning the project via eclipse, but it doesn't help.
FYI I am trying to get PhotoStream from here: http://code.google.com/p/apps-for-android/, but so far it has been very difficult to get things work.
Okay..... 5 mins later google tells me the correct answer...
http://www.fairtec.at/en/it-blog-mainmenu-16/168-the-type-r-is-already-defined
I just didnt search hard enough.
"The type R is already defined"
That's the message you get in Eclipse if you try to build the Funambol Android Sync Client.
Reason is that you have checked two Builders that try to generate the same class.
You just have to uncheck the Java-Builder from Project->Properties->Builders.
Then the application even works fine in the Emulator.
Delete the R.java from the src folder and rebuild the project. This file will be automatically rebuit during this process.
http://www.fairtec.at/en/it-blog-mainmenu-16/168-the-type-r-is-already-defined
click right to project click properties
Project->Properties->Builders.
unckeck java Builder
delete file R.java
You may want to change your package names. It looks like you are using a 'PhotoStream'.jar which has it's R.class defined at the same package structure as you.
Here is a link to the R.java from the project on Google Code. Notice you are using the same package:
http://code.google.com/p/apps-for-android/source/browse/trunk/Photostream/src/com/google/android/photostream/R.java?r=83
I had the same issue when I imported a project from work. Turning off the Java builder as suggested in the article you found fixed my problem, but when I made code updates they were not reflected in the running app. In my case there was an R.java in my source which I deleted and that fixed my problem.
In my case,
as i m not using any IDE for programming but using command line Android..
i had two xml files, one in layout and other in layout-land. i was using same id "XXX" for both but while declaring i made small mistake
android:id="#+id/XXX" (in layout xml)
android:id="#+id/XXX " (in layout-land xml)
please observe extra space in second id declaration, so while creating R.java they were different and in R.java i had
public static final int XXX=0x7f040046;
public static final int XXX =0x7f040045;
which are same, so please be aware of extra spaces. Thank you

Categories

Resources