activity_main.xml and activity_main.xml(v14) - android

I've created a duplicate file of my activity_main.xml. Why? Because I was trying to solve a problem regarding the minimum API required. So there's that red bulb notification in android studio, I clicked that, and applied the possible fix. What it did was it created that second xml file. Now, it's no use since I solved the problem in another why. This time, I'm editing the original xml file. However, when I run the program, it will display the layout of the duplicate xml file. What should I do?

There are two ways to resolve your problem,
if your are using the activity_main.xml file for your layout then delete your duplicate file activity_main.xml. Go to your MainActivity.java file and
under onCreate() method put this line setContentView(R.layout.activity_main);
this will link it to your activity_main.xml file in which your layout is defined.
if your trying to resolve the minimum API problem then right click on your Project-> Properties->Android, under Project Build Target choose a lower API then Apply and Ok to save the changes.

In your MainActivity.java file there's a line that says:
setContentView(R.layout.activity_main);
you can set any other xml file (with any name) there as a layout. Change it to the new layout's name.

Related

main.xml file not found

I am a beginner in android and I am learning it by watching tutorials on YouTube. I completed all the initial steps for building up an android project but I am unable to find out any main.xml file or activity_main.xml file under res/layout folder. In fact, the layout folder is not even showing any drop down menu. I googled my doubts but I am unable to find any satisfactory answer. Can someone please tell me what exactly went wrong ?
Add your own activity_main.xml file to the layout folder. Right click layout -> New -> Android XML File -> Name the file activity_main and click Finish.
At least if you have the MainActivy.class file, (the launcher activity), the ide, wheater android studio or eclipse should show you if the you layout activity exist by underlining the parameters for the 'setcontentlayout()' function if it doesnt exist. The 'setoptionsmenu()' function also do the same. if they are there, your MainActivity.class should be free from errors otherwise create them and insert them.

R.layout shows only Android default template proposals.

I have a couple of xml files in my layout folder but when I use them in my class the template proposals doesn't show my layout files but shows android default proposals. There seem to be no error in any of my files at all. I tried deleting the R.java file and rebuilding and restarting eclipse but nothing seems to be working. PLease help. I need to get this done asap :(
Thanks,
Sowmya.
This may be because, when you try to use your xml layouts, the default android R file is imported.
import com.android.R
Check that. If it is there, delete that import and import the correct R file which starts with your package name.
First look are you setting xml name fine like
e.g
you have your.xml
Then in onCreate
super.onCreate(savedInstanceState);
setContentView(R.layout.your);
Then clean your project.
if you have design multiple xml with same name then you must have to place them in appropriate folder using hdpi etc
Further details available here
or you can write
import.R.layout
It will work for sure

Shortcut to open Android XML layout in Eclipse

For example, if I had something like
setContentView(R.layout.activity_main);
Would there be a shortcut to open activity_main.xml? If not, is there a way to create one?
Its there. Just hold Ctrl and move the cursor to the word activity_main which is the line setContentView(R.layout.activity_main); then, it will be give two option that one is for opening xml layout and another one is opening the declaration in R.java file. This is the shortcut to open the XML layout easily.
Cheers!
This is not exactly a shortcut, but you can highlight the activity_main part on that setContentView(R.layout.activity_main); line and then press Ctrl+Shift+R.
The Open Resource dialog that shows up should have activity_main.xml (along with other files with activity_main in their names) at the top of the list. Just press Enter to open the one you want.
Actually its an id. When you create an XML file in android automatically that file has given an id by Android and you can find it into gen folder and the class name is R.java. So if you want to see that file you have to open res folder -> layout folder -> there you can see that xml file.
But you can do it as #joe or #Spk has suggested you. And Spk's solution is far better. So cheers.
if you have the class file opened instead of the java file it probably wont work. But in the java file press control on windows or command on mac and hover over the element for options to navigate.

setContentView problem

I am beginner in android so some beginer problems is occured. I create second activiy and second layout xml file named getNearest.xml. The code section to show this file is
setContentView(R.layout.getNearest)
But eclipse cannot be find getNearest. It only shows .main. So what is the problem with this?
You need to build at least once to have the R.layout values updated when you create a new layout. Also make sure you saved the layout file and that it exists under the res\layout folder.

Unable to add Preferences.xml (Android Preferences in XML)

Hopefully, this is a newbie question with a quick answer...
I am attempting to add a simple preferences file in a new Android project (New -> Android XML File), but it doesn't appear to be working correctly.
There is no root element to choose from when I select the Preference type layout. If I press Finish, it doesn't do anything. See screenshot below.
Thanks,
wTs
Just create a normal xml file to the project /res/xml/ folder and add your content to the file as usual. Should work just great. Eclipse detects it as a preference file once you have added the preference-xml and saved it.
You don't need to have a root element in the selection screen. Just replace whatever eclipse generates with what you want.
try refreshing the folder that the file is supposed to be in (in your example res/xml).

Categories

Resources