0

Heyy all, I'm starting to use Android Studio and as far as my understanding, you can create objects both in XML code and in Java code. Im not too sure what the difference is. like using TextView, the syntax is a bit different.

Am I creating a java object with both?

Also is the java textview for easier change in text and functionality while xml does the main layout/view of an application?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Tiger
  • 29
  • 2
  • https://stackoverflow.com/questions/13878053/android-xml-vs-java-layouts-performance you can read about performance here – EtherPaul Jun 17 '17 at 06:39

2 Answers2

1

For most Apps the view hierarchy is quite static. The contents of the views change, but not the views themself. In this case it is more convenient to define the view hierarchy in XML.

You can do it in Java as well. This is useful, if the views change depending on some data or if there are many views that are all alike and arranged very systematically so that a description in XML would just be too lengthy or repetitive.

Henry
  • 42,982
  • 7
  • 68
  • 84
0

Am I creating a java object with both?

Technically, yes. Almost everything you see on the screen is represented by an object. The XML file itself does not really "create" objects directly though. When your activity starts, another piece of Java code will read the XML file and create objects according to that. You usually have a call to setContentView in onCreate, right?

setContentView(R.layout.blah_blah_blah);

This is where you tell the Android SDK to read your XML file and create all the views.

Also is the java textview for easier change in text and functionality while xml does the main layout/view of an application?

Usually, yes, this is the case. When you want to dynamically add views depending on user interaction (e.g. pressing a button), you add the views in Java. If you have some views that never change, do it in XML.

However, this does not mean that you can't create static views in Java. You can. It's just that it's too much code to write so few people do it.

You can also create an XML file, and inflate the view inside it using Java code.

Sweeper
  • 213,210
  • 22
  • 193
  • 313