简体   繁体   中英

What is the relationship between XML and Java in Android?

In android, whats the difference between these 2? I started trying to make apps a few days ago and i can seem to wrap my head around it?

From what i have heard from the tutorial i am following, MaiActivity.java uses Java and Activity_main uses xml language?

Also is activity_main used to code the look of the app and MainActivity is used to code what the things do?

And what are ID's for? Is it just to reference certain buttons between the 2 files?

So basically from what i understand if what i have said above is correct, activity_main codes how the buttons look and gives them ID's, and MainActivity code what the buttons do and use the ID's to code the right button.

IS this correct?

From what i have heard from the tutorial i am following, MaiActivity.java uses Java and Activity_main uses xml language?

Also is activity_main used to code the look of the app and MainActivity is used to code what the things do?

Yes. Android uses xml to declare layouts and java to provide logic.

Note that while both activity_main and MainActivity follow common naming conventions, there is no need for them to be called this way.

And what are ID's for? Is it just to reference certain buttons between the 2 files?

IDs are used to identify views in all situations. The most common use case is in the respective java class.

When you create a android project 2 files get generated MainActivity(java) and activity_main(xml) , the xml file is used to create the views which you will be setting in the java file in the setContentView . The android build system created R.java file which contains your xml ids and other xml declaration . the java file can access the views in the xml by referring to R.id,R.string etc . basically its like a address of the xml view which you can refer from java . However I would recommend you to go through the android developer site - http://developer.android.com/guide/index.html

XML, it's an intermediate language between all programming languages and databases, used to pass values from language to another. All tags are user-defined as well as the properties inside such tags. The user can determine the name of the tag, and determine the properties in it, then the name of the tag and its properties with same names will be used in both languages, the first one sets the values to the properties while the other gets them. And so, it works as an intermediate language. To be specific on how it works, for example, let's assume that we want to pass values from database to java class. There will be three files as follow: - Java file (.class). - XML file (.xml). - Database file (.sql) for example. In the XML file there is a tag:

        <Student>
            <name>the name of the student</name>
            <age>number</age>
            <collage>name</collage>
        </Student>

Now each student's data will be in such tag, set from the database file (by an algorithm that writes inside a file when facing a specific text which is the property name), and the java file will get the values (by an algorithm that reads from a file when facing a specific text which is the property name). In this way the values are transformed from language to another. In Android, the XML file contains all the elements of the activity such as buttons, text views, menus and so on. Each element has an XML tag with its name like Button tag, and each tag has properties. The java file will go to the XML file and look for element tag (Button tag) by the ID of that element (tag), and then the java file (class) takes the values of the properties and sets them to the variables (attributes) of the Button class, and then the Button class draws the Button in the activity. Furthermore, Android studio provides virtual mobile phone screen and displays on it the elements to tell the developer the primary appearance of the activity, in addition, to inform the developer what is the appropriate position, dimensions, or the color of the element, this will generate the XML code to make it easier while coding (it's called visual programming), but in fact the java file did not read the XML file yet, until the Gradle is building the APK (execution phase).

In Android basically we use two languages JAVA and XML .

  • XML For layout, how your screen looks? What are the elements(Textview, Buttons, Listview, etc) on screen? What are the attributes of these elements (eg What is the textcolour, background colour, visibility, font, width, height and much more?)?

The answer of all above question is inside layout subdirectory of res directory ie a xml file.

  • Manifest.xml You will find this xml in app directory of your project. As in novel or any other book we have content/index page, which gives us the information about all chapters/topic included in that book. In a similar way APK have Manifest.xml which includes all information about Activities, User permissions, receiver, App name, App icon etc.

With the help of xml you can create animation (eg how textview or any other element will be animated? fade in, fade out, zoom in zoom out etc). Also you can create shapes like circle(oval), rectangle etc and use them as a background or as a icon.

You can string.xml, color.xml etc

  • JAVA Used for coding . This page control all the elements of xml with time. You can give default attributes values for different elements in xml, which will be used(for that particular element) in Activity(app) until you change that attribute in corresponding JAVA file for that particular element. To change attributes one must first define an id to the element and use that id in JAVA file to change its attributes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM