Android programming for a web developer or a quick start for the smallest. Part 1

Good day.

This hello world is aimed at an audience who is interested in this topic (Android), but the fear of java does not allow to take up development. It is possible to create Android applications on html5 (phonegap), or php (PFA), but a full-fledged application can only be written in Java.

About app


The application that will be described can do the following:

1. Launch from the “application menu” of your smartphone
2. Register using a remote server
3. Log in
4. Listing of data received from the server

Important


I will not show how to create a project in Eclipse and especially how to install. Creating a virtual device, or installing the necessary components, has been described an infinite number of times and is available on google by the request "we are writing our first Android application." It is assumed that you are familiar with the basic syntax of Java and xml, as well as familiar with OOP.

Analogy with web development and AndroidManifest.xml


When programming in php, your site has an entry point index.php, this is the first executable file (main page). In Android, the role of pages is played by Activity. The main Activity is specified in the AndroidManifest.xml file, it is located in the root of the project. Description of AndroidManifest.xml is available on the official website of developers.

You need to pay attention to the following lines of xml code:



Here comes the listing of your Activity. In order for Activity to become an entry point, you need to give it the following form (code from my project):



The MAIN and LAUNCHER properties allow you to be the main one and run from the smartphone menu. Here android: label = "@ string / app_name" is the include of the strings.xml resource from the project values ​​folder. This resource contains the search

Ctc

which will set the name of the application in MainActivity (as my main Activity is called).
An application needs access rights (uses-permission) to access the phone features. They are also installed in AndroidManifest.xml and, for example, look like this:


This code will allow our application to work with the network.

On the way to registration


In Android, the logic is separate from the view, the views are usually located in res / layout and have the xml extension. To admit layout design in xml is not a simple matter and has many pitfalls. I will not describe the creation of the interface, elementary ui can be created by dragging and dropping elements in Eclipse.

Your steps:
  • 1. Drag the button into the view file of your MainActivity and change the text in it to "Go to registration".


Now we will learn how to move from one Activity to another. In our case, from the entry point to the Registration Activity.
  • 2. Create an Activity to register, name RegistrationActivity.


Open your MainActivity. The first method that you see in the MainActivity class (class name == to the name of Activity) is onCreate , and we will work in it. This method starts automatically when the application starts.
It is assumed that you created a button in the view (step 1), we need to find it.

@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// не забудьте добавить import android.widget.Button; // находим нашу кнопку
		Button myButton = (Button)findViewById(R.id.айдиКнопки);
}


Next, we need to add a “reaction” to the click on the myButton button. Add the code, add a listener to your myButton button using setOnClickListener :

		myButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
                // из MainActivity в RegistrationActivity
				Intent intent = new Intent(MainActivity.this, RegistrationActivity.class);
		        startActivity(intent);
			}
		});


Now when you click on the button, RegistrationActivity will start.

Also popular now: