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;           ...

Android: How to Generate Key Hash for Facebook Integration

Hello everyone, As this is a quite confusing for everyone to generate the key hash in your PC, so i decided to write about it. I have also tried many ways but every time i failed to generate the key hash. After trying for an hour i successfully generated the key. Just follow the below given Steps and you are done with this. Step I: Download Openssl from   here Step II: Download openssl-0.9.8e_X64.zip for 64 bit PC and openssl-0.9.8e_WIN32.zip for 32 bit PC Step III: once your download the zip folder. Go to C drive(where window is installed) and make a folder named openssl Step IV: extract all zip file in this folder. Step V: Now go to Java folder and copy path of jre. For example: C:\Program Files\Java\jre1.8.0_101\bin