Friday, 4 September 2015

8.Giving very first layout

Giving layouts with XML files is pretty easy (as some of you may know) If you don't have any idea about XML (as I was not having in my time) then don't worry about it let's get started here.

(This is my old post and some things are changed so you must have a look at this firstly. I am writing this line after 6 months since last edit of this post.)

Firstly navigate to path app->src->main->res->layout->activity_main.java


So the folder named "layout" in the heirarchy contains all the layout XML files of different Activities. We will add layout of other Activities here only in later posts.

Open the file to edit

You are going to see how your layout is going to look like in phone.
As you add things you will have the things in phone.

So when you open an XML file which is intended to give various kind of layouts (in layout folder) you are going to see phone.

Look at the options below the section in which phone is displayed.
In this section you are going to see two section 

"DESIGN" and "TEXT"    

The DESIGN section is selected by default that's why you are having phone on your screen.

Click on the "Text" section where you will find the hard code behind the layout present till now on the mobile screen.

Now we are going to talk about the things which are there in the layout file of our Activity.
 
In this post we will learn about how to use some very common things(views).

You can see a term "RelativeLayout" out there with opening and closing angle brackets 

First note that some other layouts other than "Relative Layout" out there.

So commonly used Layouts are

1.LinearLayout
2.RelativeLayout
3.TableLayout
4.FrameLayout
5.DrawerLayout

We make selection of Widgets(Layouts) according to our use.

These Layouts are the widgets which are responsible for arranging the Views within it. For example, if we are displaying simple text then we have to add it in this type of widgets or Layout According to our use(it will be more clear).

We will use each one of them

For this time replace the term "Relative" by "Linear"(we will be using Linear Layout here).

The two lines after the name of root layout
1.xmlns:android="http://schemas.android.com/apk/res/android"
2.xmlns:tools="http://schemas.android.com/tools"

These lines declare namespaces and you have to add these two lines to the the root widget of your layout for more information about these two you can refer to this link (or you can just take it as just syntex and focus on android).

Now, here some terms come which provide attributes like width, height, orientation of layouts, background color of layout etc. which are very important, you have to fill necessary ones and you can add some additional attributes for specific layout. Here we will consider some attributes which are really very important and you will use them most of the time in most of the views or Layouts(views will be covered).


android:layout_width="match_parent"android:layout_height="match_parent"

We have to add "android:" attribute most of the time.
As you can see these both are used to give Layout's  width and height.
 

"Cover up attributes" :These attributes decide the total cover up of particular view or Layout on screen


There are two type of cover up attributes 
1.match_parent
2.wrap_content


Here The Linear Layout is our root or parent widget and having match_parent attribute our widget is going to cover the entire screen and if same attribute(match_parent) is given to views within it, will also cover the entire screen as well or we can say it will have same width and height as its parent do.
 




While in case of wrap_content attribute(android:layout_height="wrap_content") our layout will only cover the part of screen in which its contents are adjusted.

Now, come to the term TextView, 

This is a simple view which is used to display some text on screen with some necessary attributes and/or addition attributes.

That's all for now we will have explanation on some other attributes and proper elaboration of TextView in very next post.





7.Dealing with XML files (Layouts)

As already mentioned XML files are categorized as assets.

Mainly XML files are used "in terms of layouts" in Android.

Here another explanation comes for "in terms of layouts" :

Basically some types of XML file are there, XMLs which are mainly used in our package are :

1.Layout XML : "These"(Each activity will have its own Layout (XML)file however you can use same file for one or many Activities when necessary) XML files are used to define the actual User interface of our application.
They hold up all the elements or the tools that we want to use in your application. Like Text Views, buttons and other UI elements (I will explain various may be each UI element which you can add in your layout in later posts).


2.Manifest XML :"This"(Each application is having one Manifest.xml within its package) XML file is used to define all the components of our application.

It includes the names of our packages, our classes(Activities), services, receivers and the permissions that your application needs(Related terms will be covered in another post sooner).

3.Strings XML : "This" XML file is used to replace the Hard coded strings with a single string, like you can replace"Welcome to my application" with "Welcome" string name and add the string with the word welcome in this XML file, you can refer to that string with Welcome throughout your application.

4.Styles XML :These files are mainly present in single quantity but you can increase  quantity by one or more to give different Styles to different versions of android(when necessary).

5.Drawable XML :These are the xml files that are used to provide various graphics to the elements of application. for more info about these right now then take a look at this however I will use it during the later tour.

6.Elaborating very first method onCreate(Bundle SavedInstanceState)

You must read just previous post before proceeding with this one.

As you know this method builds Activity.


The very first line super.onCreate(savedInstanceState);
Does the job of feeding the Prior data during rebuild or Build of Activity.

If Activity is being build very first time then Bundle will not have any data and will not return anything, if it has some data then Activity continues with its last state.

Proceeding next line, (Very Important line of code)

setContentView(R.layout.activity_main);

You may have some idea with its name, It is just setting the layout of your Activity.

It is just presenting the layout (XML file) in which you had given layout of your Activity.

the name of file which is having all the elements of user interface of our Activity is "activity_main".

Here it is deciding the layout of our Activity.

By this end you may have a clear plan in your mind about some mechanism of Android.
(We will proceed with XML files in next post).

5.What are Activities in Android ?

As I told that in Android we are just dealing with Activities. Every Screen having content or not is an Activity.


Whether it is the home screen of your device or First or Last Screen of your Application is an Activity.(Its just introduction, Things will be more clear Don't worry about that).

Now back to flow, in our first application, 
The class MainActiity.java is representing an Activity as you can see it is extending a class AppCompatActivity.java (as sytex says class MainActivity extends Activity).

Now in previous versions of android we were dealing with Activity class instead of AppCompatActivity.
As time passes new things are added so android has many resources to be added so they have made new class AppComatActivity which contains so many features which the previous class did not.

Now we are Overriding (see "@Override" its syntex to override methods) three methods, I mentioned in previous post.

Now understanding all three methods
1. onCreate(Bundle savedInstanceState)
We can say that this method builds the Activity when Activity starts for the very first time and also rebuilds Activity when Application is out of memory. Take this example, If you are working on application, and a moment comes when you have to check out other applications something like that then, If application gets out of the memory then the Activity on which you were working has to be rebuilt and also when orientation changes and the prior data(Last data like last user edits) must remains so the Activity continues with its last state.

The (Bundle SavedIntanceState) stores or roles the prior Datas and fills into Activity again when Activity gets rebuild.

It will be too sooner to think about next twos (This is what I think so I will proceed with that two methods later).

So we are dealing with the first method onCreate only we would use other methods very later sections.

Wednesday, 2 September 2015

4. Dealing with Java file (But where is main() method ?)

So till now I don't think you are having any kind of trouble. If you do have then try to send me issue or just google it may be Stack Overflow is having the answer.

So let's get into the programming section.

After Selecting the new project till now you might have a screen which is divided into certain sections 

Click on the all of the titles on very left or right side of screen written vertically 

Project 
Structure
Captures
etc all of them 

This will close all the distracting sections.

Now click on the "Project" section (very left side)

Click on the first Drop-Down menu (Android is witten you can say android is selected by default) and select "project"

Now you can see there are folders as I had mention in previous post that assets are placed in particular folders. I will explain these folders letter.

navigate to the path app->src->main->java->com.....->MainActivity.java

Double click on this java file.

As you can see MainActivity.java file is open in the center section of our software.

In this file you are having class MainActivity which is having three functions

1. onCreate(Bundle savedInstanceState) 
2. onOptionsItemSelected 
3. onCreateOptionsMenu(Menu menu)


Well in my time at this stage I had think where is the main() method.

So in simple words every application has to be started by main method as far as we know till now and yeah it is true.

In android we are not dealing with application we are dealing with Activities.

So the root classes which are responsible for running applications which are application itself  and running before execution of our application and are dealing with main method already.

So our application is just an package of assets and classes which are representing an Activity (an complete screen of the application). 

You will have description in detail in next post about what Activity is and everything about it and about all three methods I mentioned above.

Keep learning and I hope you are getting all stuffs clearly.

3. Knowing Development Environment(Android Studio)

I liked eclipse very much than Android Studio to get started with android but since Google is supporting Android Studio and sooner eclipse will not be supported by google (may be). So that's why better start with Android Studio.

Now after starting Android Studio few seconds letter a window will pop-up asking various things like this.

First Screen: 

Select Start new project.

Second Screen :

 

After filling application name and some entries in this screen you will be asked for minimum sdk version which means on the minimum android version you want your app to run on, so select any one of them.

Third Screen :

 

Well getting much older version our application will not support the new functionalities which are being added as time passes with new ones(versions). Try to select which can cover most of the devices and can give most of the latest functionalities (Depends upon time). Here you might want to know about term used after jelly bean "api' 16 something.

Api just represents the functionalities which are added up with that version of android which means, in api level of kitkat some functionalities are there which are not supported by jelly bean means jelly bean will not support the app which uses kitkat api leveled functionalities. Well well well some important features of higher version of android application api can be added by importing support libraries but not most of them.

Fourth Screen :

Select Blank Activity from this screen.

Fifth Screen :


Don't edit any of the text fields. I have covered what details are these in the next posts.

Sixth Screen :(The Final screen)
 

Now, In eclipse there was an option of minimum ,maximum and compile with sdk version but in Android Studio you can only select minimum level of sdk version. So application will be compiled with latest version of android api available. So you must have latest android SDK platform installed within android studio. You can check and download the latest one by :-

1. Select the tools option from the very first bar of the application and then Android->SDK Manager.
2. If latest platform is not checked then check it then download will happen and then installation also.
3.After these all the stuffs you have made your Android Studio ready for Programming.

In next post we will deal with Java programs and XMLs files.

Tuesday, 1 September 2015

2. Android basic understanding

In this post I am giving you an idea what basically you are going to do to deal with android programming. In Android we have a lot of ways to get our stuffs done or we can say we can do one thing in many ways so its just depends upon your level what path you prefer. I will use very easy (as much as I can do) way to get the stuffs done.

As android is based on Java , the programs of Java will use XML files, medias(such as images, tones etc.) as assets of the application. All the assets are place at defined locations (folders) of our application package that is .apk (Android Application Package). When application programming gets finished the java programs are compiled with assets in the form of package which is our final .apk file.

You may find my posts very short but I would like to make clear that I will give you all the information which is necessary to know as a good android programmer to make his/her way of developing and learning future features of android easily

Now, to program within android guidelines we must use an Android Integrated Development Environment officially which is Android Studio you can get it from here. Before you must have Java Development Kit pre-installed in your system (as you may know) with Environment Variable defined.

Till now I think you may have done all the stuffs to get started with android application development. So congratulations ! You will have better understanding with tutorial as we move ahead. Lets get into the programming stuffs in sequential steps ahead.