⬆️ ⬇️

Programming on Android for a web developer or a quick start for the little ones. Part 1

Good day.



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



About the application



The application that will be described can the following:

')

1. Run from the “application menu” of your smartphone

2. Registration using a remote server

3. Authorization

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 in google on request “we are writing our first application on Android”. It is assumed that you own the basic syntax of Java and xml, as well as are familiar with OOP.



Analogy with web development and AndroidManifest.xml



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



Pay attention to the following lines of xml code:



<activity> <intent-filter> <action /> <category /> <data /> </intent-filter> <meta-data /> </activity> 




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



 <activity android:name="com.dnt.ctc.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> 




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 resource strings.xml from the project's values ​​folder. This resource contains the desired



 <string name="app_name">Ctc</string> 


which will set the name of the application to MainActivity (this is the name of my main Activity).

The application needs access permissions to the phone functions (uses-permission). They are also installed in AndroidManifest.xml and have for example the following form:



 <uses-permission android:name="android.permission.INTERNET" /> 


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 extension xml. Admit design layout on xml is not easy and has many pitfalls. I will not describe the creation of the interface; you can create an elementary ui by dragging and dropping elements into Eclipse.



Your steps:



 <Button android:id="@+id/" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="  " /> 


Now we will learn to move from one Activity to another. In our case, from the entry point to the Activity registration.





Open your MainActivity. The first method that you see in the MainActivity class (class name == Activity name) is onCreate , which is where we will work. This method automatically starts when the application starts.

It is assumed that you have 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. Let's 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 be launched.

Source: https://habr.com/ru/post/165105/



All Articles