In this tutorial you will learn how to create an activity.How to start an Activity and in the end i’ll give introduction about Android Manifest.

Basically you will create a new activity and next you will create new layout for your newly created activity.Next you will start new activity via Intent.

Let’s start with some introduction of Layouts and different type of layouts.

Layout

Layout is visual structure for user on which you can use different UI components like button for user interaction.Layout basically created for an activity because when ever you press any button it will show Toast because you have coded that Toast in your Activity.

You can create layout in two ways

  • Declare a UI component in XML
  • Instantiate layout elements at runtime

for more detail you can visit Android Official Website.

you always remember one thing that every UI component in layout including layout have width and height.Like

activity,start new activity

Different type of Layout

  • Relative Layout

    One of the basic layout to design UI in android.In this layout you can display elements or components with relative position which means where ever you drop your UI component it be show on that particular position whether you drop one component on another component.

This image show you how Relative Layout will look like.

activity,start new activity

Now you can see i droped UI components on different positions and they are displaying on that particular position.

  • Linear Layout

    In this layout every component or element display in two direction either in Vertical or Horizontal.When ever you drop any UI component it will display in sequence.This image might illustrate something more about this layout.

Vertical Linear Layout

activity,start new activity

Horizontal Linear Layout

activity,start new activity

  • Table Layout

    In this layout every UI element or component display in a row or in a column by adding tableRow element in Table Layout.This image will help you to understand what exactly Table Layout is.

activity,start new activity

  • Absolute Layout

    In this layout UI elements or components displays at absolute position which means you can give x and y position to that particular component.It is not recommended for UI design because whenever you want to add new component you have to change every single component.It will be just like below imageactivity,start new activity

  • Frame Layout

This Layout basically can hold only one child view except first UI component every component will display on              top of below UI components you can say overlapping of components just like this.In this Image Toggle Button              and Button are overlapping you can see.

activity,start new activity

  • List View

List View will display all the items in a scroll able list.List View always need parent layout like Linear Layout                or other Layout.List View will look like this.

activity,start new activity

  • Grid View

Grid View will display all the items in two dimensions like row and column.It is also scroll able.It will look like             this.

activity,start new activity

 

Now let’s start with coding and creating new layout and new activity.

Create new android project.I named it layoutActivity.

activity,start new activity

whenever you create new android project it’s default files will be in android project which might be named as (activity_main.xml) and (MainActivity.java).

so what you have to do next just drag and drop one button on your Graphical XML layout because we will start new activity by clicking that button.Then reference that button id and apply button click event on your button in your (MainActivity) just like this

public class MainActivity extends Activity {
	Button start;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        start = (Button) findViewById(R.id.activitystart);
        start.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
			}
		});
    }
}

Activity

Now next you will create new class by going into your android project > src > your package name like below image

activity,start new activity

next right click on that package New > Class click on class.

activity,start new activity

Next new window will popup at your front.Name that class which you want create and then hit Finish.Just like

activity,start new activity

After creating new class it will look like this.

activity,start new activity

 

You have created new class with name “AndroidCodec”.Now you will declare that class in (AndroidManifest.xml) if you will declare that activity your application will not be able to use that class.Also it might be crashed because your previous need that class to go on.First i’ll give a little introduction about (AndroidManifest.xml)
then we will move forward.

Android Manifest

Android Manifest is an essential component of an Android Application.You can say it’s back bone of Android Application.It stores all information about the application like activities,services etc.For Basic it contains three most important parameters.

  • Manifest : – <manifest> Manifest is the root element which contains application,activities and can be declared just once.
  • Application : –  <application> this is used to declare application whatever within the application.Can be declared once.It can also be used to declare other components like services,broadcast receiver etc.
  • Activity : – <activity> Used to declare  classes.

and also within root element <manifest> it contains sdk version which application target min-max.You can also add permission into that like camer permission,contacts permission to read etc

This might help you better. For more Detail.

activity,start new activity

So now go to android project and in (AndroidManifest.xml) and declare your newly created activity within the <application> tag don’t forget.Using this line of code.

activity,start new activity

Next we will create a layout for newly created class.Go to your android project find “res” directory and within that directory find “layout” directory.Right click on “layout” New > Android XML File like this

activity,start new activity

When click on Android XML File.It will display a window in front of you to name that layout and also for root element by default you can use Linear Layout just like below image.Now hit finish button.

activity,start new activity

After clicking Finish your Graphical Layout will be at front like this.Now i’ll drop TextView to display on that Layout.

activity,start new activity

Now next step is to extends our AndroidCodec (.java) class and import it if underline or shows error.Then right click on Activity and select Source > Override/Implement Methods then a window of implemented methods will display just like this.

activity,start new activity

now type from keyboard and find onCreate(Bundle bundle) method check that and click ok then your class will look like this.

public class AndroidCodec extends Activity{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

}

onCreate is a method which is called when your activity started.Now you have to set Graphical Layout for that particular activity using setContentView.SetContentView is used to set all the contents of an activity to an explicit view which parameters.In the below code ” R.layout.codec ” is pointing your graphical layout from Resource(R) ” codec ” name of graphical layout.After setting content View your code will look like this.

public class AndroidCodec extends Activity{
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.codec);
	}
}

Next go to “MainActivity.java” class file and within onClick sub method create an intent pass two parameters first pointer to current activity which will be (getApplicationContext) or (this) and second paramter class name followd by .class.Then pass that intent in startActivity() method to start new activity.

public class MainActivity extends Activity {
	Button start;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        start = (Button) findViewById(R.id.activitystart);
        start.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent n = new Intent(getApplicationContext(),AndroidCodec.class);
				startActivity(n);
			}
		});
    }

   
}

Final out will look like this.

activity,start new activity

when you click on button your new activity will start just like this image.

activity,how to create new activity,start new activity

that’s it.

I hope this information might help you alot thanks.