can anyone tell me what is the main difference between main.xml and Activity_main.xml.
I am writing a sample pgm for a simple client server communication.In the tutorial that am following it says to add the following code in main.xml which the author says is inside layout folder. but in my package explorer main.xml is shown inside the menu folder and in my layout folder, i have only activity_main and fragment_main. this is the code that has been posted in the website.
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”>
<ListView android:id=”#+id/list”
android:layout_width=”fill_parent”
android:layout_height=”0dip”
android:layout_weight=”1″
android:transcriptMode=”alwaysScroll”
android:cacheColorHint=”#00000000″
android:listSelector=”#android:color/transparent”/>
<LinearLayout android:id=”#+id/footer”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:orientation=”horizontal”
android:gravity=”bottom”>
<EditText android:inputType=”textMultiLine|textNoSuggestions”
android:layout_width=”0dp”
android:layout_height=”40dp”
android:id=”#+id/editText”
android:layout_weight=”1″/>
<Button android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:id=”#+id/send_button”
android:layout_gravity=”center_vertical”
android:text=”send” />
</LinearLayout>
</LinearLayout>
my question is where exactly am i supposed to use this code? in main.xml in menu folder or activity_main.xml? plz help
Put the code in the activity_main.xml file.
activity_main.xml and main.xml described in your tutorial are referring the same thing (a layout file). You can find it under 'res/layout' in the package explorer in ADT.
(Do not put the layout code in the .xml file in the menu folder)
You could actually name the .xml file anything you want as long as it is properly declared in your Java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);//<-------your xml file is declared here
I believe it is just the newer version of ADT that names the file activity_main.xml by default.
Whatever your tutorial is saying in regards to main.xml use your activity_main.xml instead.
alternatively from within ADT navigate to 'res/layout' and right click on the activity_main.xml select refactor->rename and rename the file to main.xml so everything matches your tutorial
where exactly am i supposed to use this code
This code would go inside activity_main.xml because it is a layout file.
menus
If it was a menu it would look a lot different such as having menu items instead of Views. And it would start and end with the <menu> </menu> tags. A menu file can be inflated to be used with your menu buttons whether that be a hard menu button, from the ActionBar, or inflated to be used somewhere else.
Menus
layouts
layouts will contain your Views such as ViewGroups (LinearLayout, RelativeLayout, etc..) and child Views like TextViews, Buttons, etc... The layout files can be named whatever you like (assuming they follow the naming conventions for layout file names). Then you use that name in your Activity to set the layout using setContentView(R.layout.some_layout_name)
Layouts
The code you posted is layout code; it should go into layout/activity_main.xml. The menu/main.xml file describes a menu named 'main'.
You need to add the code in layout/activity_main.xml. The tutorial main.xml corresponds to your activity_main.xml. This is the xml that is inflated in your Launcher activity (normally using setContentView(R.layout.activity_main) api in onCreate() function). This xml name can be anything. While creating the application, you get the option to give name to this xml. The default name is activity_xml.
The main.xm which is inside menu folder is used for sub menus in your applicaion. If you click the menu button inside your application, sub menu will come up. The layout of this sub menus is controlled by main.xml in menu folder.
Related
I' new to Android development. I created a new project with automatically created 'MainActivity.class', 'activity_main.xml' and 'content_main.xml'. So at this step there was no problem.
Then I'd like to create another activity called 'MainActivity2' using basic template (the one with a fab). I expect to see two layout files: 'activity_main2.xml' and 'content_main2.xml'. But it ONLY created 'activity_main2.xml'. There is no 'content_main2.xml'.
Even worse, I tried to create MainActivity2 using other templates (beside basic and empty templates), but still I cannot get 'content_main2.xml'
Where went wrong?
It might be because of the layout type you've selected when creating an activity in Android Studio. Nevertheless, if you would like to include one layout inside another layout you could use <include> tag, for example:
<include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="#layout/yourlayout" />
create a new xml layout file called yourlayout.xml
I created two layout files.
Android XML Layout File which created this xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
and an Android XML File
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
From the looks of it they are both the same. When to use one over the other since they are both creating the same xml?
Yeah they are prograticaly the same, there isn't different type of xml. What matters, is the location of the xml (layout, values, ...).
In eclipse, there maybe is a small difference in the IDE view, i'm not sure that you have the Graphical Layout view if you create a Android XML file instead of a layout file (it's a detail because, you still can add this view ;) )
But results apk will be identical.
The reason why you would use an Android XML, is if you wanted to use the Layout Tool (the GUI layout editor). The Eclipse IDE will react to this file differently.
But, the code is the same, so the APK you create will also be the same - it doesn't matter in your final output.
I always use Android XML files, so they integrate into the ADT tools better.
the only difference as far as I can see is that in xml you can't use both "android:layout-width:" and "android:layout-height:" attributes which makes xml for example suitable for the use of menu and item bar and other uses.
i am customizing an action bar, and i am using inflater and styles.
i added a menu.xml to a menu folder in project which i inflated into the onOptionsMenu,
the menu.xml contains the action bar item's.
Everything worked fine, until i renamed menu.xml to actionBar_menu.xmlusing refractor.
Now i can't even add it to the Inflater. it's not even declared in R.java
How do i solve the problem any hint or solution?
Thank you
Edit:
Thank you guys for your answers.
i did what you asked so i changed my action bar to actionb_menu.xml, but it still not working.
otherwise it's now declared in R.java, but eclipse editor doesn't recognize it
(ctrl+space), and even if i ignore this and continue typing the name of the file in the inflater it it still underlined as an error.
any solution for that??
Your original problem was: You can only use lowercase letters, numbers and underscores in your layout XML file name. As stated by #midoalageb
This is the answer to your edit.
Because your id wasn't initially in your R.java when you tried to do
R.id.....
eclipse has imported android.R; into your class file.
You need to go up to the top of your file delete this import:
import android.R;
then attempt to do:
R.id.whatever
again. When you press ctrl+space after R. ensure you select com.your.package.R and not android.R
hey hosni habeby look do like this,
open your new xml file the one you re named it and go to
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.something. app.OldJava" />
and replace the context value to your new java file the one you renamed also.. like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.something. app.MyNewActivityJava" />
in eclipse when you changed java file's name and it xml.. eclipse will not touch the values in the xml so we have to change it our selves also eclipse will add an import line may not refer to your R file.. if so remove it and type one that refers to your R file of your project not of Android or any library
You can only use lowercase letters, numbers and underscores in your layout XML file name. So rename your file to actionbar_menu.xml
Right click on Project. Refresh it first--> then clean it --> then rebuild it -->if there are necessary code changes then do it--> it should work.
also rename it in all small case chars
Try cleaning the project. If it still says R cannot be resolved then there should be something wrong with the xml file. Check your xml file for some extra characters that you mistyped or something of that kind. Your R file will not be built if even one xml file has an error!
I`m wondering about one thing. If I decide to have a part of my View same in every Intent. For example 2 buttons at the bottom of screen and for example I have 3 diffrent views, List, Detail and a third one :) Do I need to put the buttons on every xml schem for each view or can I create other xml and attach it in each activity with all listener etc.
If I can attach in activity how can I do that ?
Create a XML buttons.xml with the common elements
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<ImageButton
android:id="#+id/myButton"
android:src="#drawable/ic_title_search"
android:onClick="myHandle" />
<ImageButton
android:id="#+id/myButton2"
android:src="#drawable/ic_title_search"
android:onClick="myHandle" />
</merge>
Include it in another xml:
<include layout="#layout/buttons"/>
where buttons is the name of the xml file to be included
You can use the <include /> tag in your XML files.
See Layout Tricks for an example.
Create a separate layout for your buttons then in the layout you wish to display them use the include tag.
This will allow you to reuse the same layout in multiple parent layouts.
The include works as stated above you could also consider using fragments
Fragments
You can use these in older versions of Android by including the compatibility library in your application. It's definitely more work than a simple include but if you need some reusable UI for more sophisticated features than simple buttons you might want to look at that as well
How to use compatibility API
I am creating a few layouts with the root element being merge. inside the merge element i have a ScrollView containing a TextView. Here is the file:
<?xml version="1.0" encoding="utf-8"?>
<merge
xmlns:android="http://schemas.android.com/apk/res/android">
<ScrollView
android:id="#+id/scroll"
android:padding="6dp"
android:layout_below="#id/headingLayout"
android:layout_above="#id/tabsLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/aboutTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</ScrollView>
</merge>
I am getting 2 errors: there is no resource to match the layout_below and layout_above names i have put in.
I have another layout xml file including very similar components, some of which also reference #id/headingLayout and #id/tabsLayout. Both of these XML layout files do not contain the component with name headingLayout or tabsLayout
Why is it that one layout file has no errors about these references and the other layout file does? What the crap am i doing wrong, the app will build and run like i expect, until i add this merge to another layout?
I have even tried copying the xml from the working layout file, to find that it gives the same errors, something must be wrong with my new layout file. I have tried cleaning, rebuilding, opening/closing eclipse..
Wow, this is a strange fix i have found for this. the layout i have given in my question above(the one giving errors) is named about.xml. the layout with no errors which also includes merge and references to other xml components is named home.xml. the xml file including the referenced components is named base_layout.xml.
in the eclipse Package Explorer under the layout folder, items are listed alphabetically. so the home.xml file came after base_layout.xml, and could therefore reference the components inside it. Since about.xml came before base_layout.xml, about.xml could not reference anything inside base_layout.xml.
so i just renamed base_layout.xml to aaa_base_layout.xml so it would be first, and all errors go away.