1. Creating a new Android Project
Select "Start a new Android Project", the following screen appear:
Input your Application Name and Company Domain, edit your app package (if you want), click Next:
By this screen, you select minimum SDK for your app, in this case and all other post, I set min-sdk is 14 or 15. Normally, we wrote app running in Android Phone and Tablet, so please check "Phone and Tablet" check box.
In this next screen, you select your Activity type for app. In order to prevent Android Studio auto generating much complicate codes, choose "Empty Activity" and click Next.
Naming your Activity here, check "Generate Layout File" option and click Finish. Waiting for building project completed, our Project interface in Android Studio will be like this:
The right bigger panel is showing the currently selected file content, you are coding here. The left panel show the project structure:
In Android Studio, project include 1 or many modules, by default, the runnable module name is app. If you add any libary to use in your project, it will available as a module. Now, we are listed some important directories of each module:
- Java code files is located in src/main/java/[your_package_name] folder, all programmatically code will be written here.
- Layouts resource is in res/layout folder, drawables resource you can put at res/mipmap or res/drawable folder. By recommended, your images (PNG, JPG) file should put in drawable folders (the mipmap folders use for putting the app icons (ic_launcher)).
- Other resources still similar in Eclipse, located in res/values.
- The most important file is build.gradle: each module has it's one, containing the basic information of that module (min-sdk, target-sdk, buildToolsversion,...). It's also contains dependencies which this module references in code.
apply plugin: 'com.android.application' android { compileSdkVersion 23 buildToolsVersion "23.0.1" defaultConfig { applicationId "info.devexchanges.helloworld" minSdkVersion 15 targetSdkVersion 23 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.0' }Note: the line make this app module is runnable is "apply plugin: 'com.android.application'".
2. Creating Emulator and Execute app
Choose "Create a Virtual Device" if you are not having any one. By some simple step, you can create a suitable emulator. By the next times, the created device is available when this Dialog displayed. Select device to run by click the blue triangles icon, after a moment, your virtual device will appear:
Note: You can use some virtual machine softwares like Genymotion or Blue Stack instead of the default SDK emulator. They're compatible very well with Android Studio (In some posts, I use Genymotion virtual device!).
Back to Android Studio, click Run (the green triangles) icon, app module will be invoked and we have this result:
3. Adding some codes to project
Firstly, changing the layout a little. By default, in only includes a TextView, now, adding a Button in res/layout/activity_hello.xml:
In Activity programmatically code, we will locate all views (TextView and Button) and handle Button event (change TextView text when it's clicked). So, open HelloActivity.java file and paste this code:
package devexchanges.info.helloworld; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class HelloActivity extends AppCompatActivity { private Button button; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_hello); button = (Button)findViewById(R.id.button); textView = (TextView)findViewById(R.id.text); button.setOnClickListener(onButtonClick()); } private View.OnClickListener onButtonClick() { return new View.OnClickListener() { @Override public void onClick(View v) { textView.setText("Button clicked!"); } }; } }Let learn a bit about activity code:
- Make it extends from Activity class or it's subclass like AppCompatActivity.
- Declare variables in class.
- onCreate() will call when Activity init. Locate all widgets by findViewById() here.
- Handling Button click event by call setOnClickListener() method.
- The params of setOnClickListener() is a View.OnClickListener method. So, create a onButtonClick() with this type and attach it to the Button.
After click the Button:
4. Conclusions
Every thing about developing an Android app has been done here, see next tutorials to be a great Android developer!