Skip to main content

Android: How to Create a Single Activity !!

Well, this is the most necessary and initial part in android development. One should have knowledge about creating an Activity. It represents a single screen of your application, for more details check this Article on Activity and Activity Life Cycle

Every activity in android consist two files. One is JAVA file and another is XML fie. JAVA file helps you implement the functionality of components that you defined in your XML file. To create an layout you should have an XML file. This is the file where you create an user interface for your activity.

There are two approaches to create an Activity:

1. Create both the files(JAVA & XML) separately and define it in Manifest file

a) In android studio, Select File Tab and click New












b) Select Java Class Option in the list and give name to you Java file





Enter any name and click OK.

c) To make it an Activity you need to extend Activity class in it.





public class FirstActivity extends Activity{

}




d) we need atleast one method to work with our Activity class. So, we will insert an override method onCreate() check the details in Activity Life Cycle  

Press Cntrl + O in android studio and Select onCreate() from list and click OK.



Now Lets create an XML layout file for our Activity.

first few steps are same. Click File tab and select new then select XML and inside XML select Layout XML File. 



Give a appropriate name to your layout file for example:first_layout.xml (Make sure there should not be any special character or Upper Case while declaring the name on underscore(_) is allowed.

now you have an layout for your activity.



add this line in you FirstActivity. java under its onCreate method below super.onCreate(savedInstanceState)

setContentView(R.layout.first_layout); // We are inserting User interface to our activity.



There is only one step left to make sure that our activity runs properly. You need to define this activity inside your manifest file.

Now you Activity is ready to run. 

2) The another way far simple than the first one. you need not to create both  files separately and you need not to bother about manifest changes as well.  

Lets take a look on another way of creating an Activity.

Just select File Tab then select New and then Activity option. In Activity option, select Empty Activiy 



It will show your an dialog box like screen where you need to enter your activity name.


Just Hit finish and you have an activity which is ready to work for you. This will automatically create Java file and XML file inside the Layout Folder of your project. And you will find your java file in src folder.

Thats all about Creating an Activity. 



Comments

  1. Thank you Author for Sharing Android Activity Tutorials.

    Visit for Free Android Studio Tutorials

    ReplyDelete

Post a Comment

Popular posts from this blog

Android: Login Screen using Fragments

In this tutorial, our focus is on making a User/Member login activity using fragments. With the help of Fragments we will use the same activity to show User login area and also Members Login area. Prerequisite for this tutorial: You should be know how to make an Activity And most importantly you should have prior Knowledge of Fragments. For practising basic Fragment implementation refer to Android Simple Fragment Example      We have used only three activities for this: MainActivity(which represents the Login Screen) Fragments for Members area Fragment for New Users

Simple Login/Register Example using SQLite database

MainActivity.java(login Screen) package com.AndroidDevelopmentGuru.database_new; import java.util.List; import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.os.Bundle; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity {                                 EditText user, pass;                 Button login, not_reg;                 DatabaseHandler db;                 Cursor cursor;                                 @Override                 protected void onCreate(Bundle savedInstanceState) {                                 super.onCreate(savedInstanceState);                                 setContentView(R.layout.activity_main);

Android: Current Location Using Fused APi on Google Maps

This tutorial gives us the simple implementation of "Fused API" to fetch the current location on google map in android. Fused API is latest among all techniques to get the location. It provides you very precise results and also uses less battery of your device. It chooses GPS or Network provider to get to your current location. And it helps your device remember about the last saved location. Let's implement the Fused API to fetch/get the current location. Step 1: Create a new project in Android studio. and select Maps Activity.