Thursday, 27 August 2015

Create your own layout using Java

Add a Large text from widgets in Design view of xml file. Let it be anywhere.
It will look like the figure
Now run the emulator. Its output will be as shown in figure.

Now go to MainActivity.java file.

Try to analyze the onCreate method. Here setContentView function displays the layout on the screen. Comment the like
And again run the app on emulator.
The output will not show the text because it was the part of that layout.
Now. Write the two lines on code in onCreate method.
Here we create a new object of class RelativeLayout named as MyLayout and then we display the layout on screen. If we run the application now then it won’t show that text because it was the part of RelativeLayout and now of MyLayout.
In future tutorials when we will add texts and buttons to MyLaout, you will better understand this concept.


Design of Android Studio- java file

The whole logic of the program is developed in the java file. Even we can do everything through java file i.e. a java programmer can create android application without writing single letter in .xml file. But for layout xml file is easier to use than java. So for time factor java is not suitable at least for layout. For logic you have no option other than java. We will study to create widgets using xml and java in future tutorials.

Currently see the figure.
The package name is CompanyName.ApplicationName
All required classes for predefined program are imported already by Android Studio.
In future if you have written one function for the class which is not imported yet, so the studio is showing error. Then just import the class at start of file otherwise use the shortcut of alt+Enter. This will import the class for you
The activity defined for the layout is the main programming class. In start we will deal only with onCreate function whenever this function is called the saved instance is generated. If we are opening the app after closing it then the app will start from the opening and of we have paused the app (pausing app means instead of back button just clicking on home button, so the process is still on in background) and then resuming it again. setContentView sets the layout on the screen. Which layout should be passed is decided by the passing parameter to the function.
The futher part will be covered in later tutorials.