What is android activity? learn how to create a new activity in android studio?

In this tutorial, we will see what is activity and how to create a new activity in the android studio. For this, we will make a simple and that app will contain a simple button. When somebody clicks on the button a new activity will be launched. So be ready with your new android studio project.

Useful links

What is android activity?

You can consider the Android activity as a new screen. Every screen which is showing in the android device is an activity. For example, when you click on any button in the app, it moves to a new screen and that new screen is actually an activity.

Steps to create new android activity

  • Select main_activity.xml 




First of all, add a simple button widget in main_activity.xml. You can edit its properties to adjust its height, width. I have set the layout_gravity to centre to set the button in the centre of the screen. You can read my xml file below.

<Button
    android:id="@+id/button"
    android:onClick="click"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginBottom="8dp"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="8dp" />
  • create new android activity
    Now create a new activity by clicking on File->new->activity->basic->activity. You can give any name to this activity. Let us say we give name “SecondActivity”.A new activity will create two files. One is java file and other is xml file. You can do it manually also. I mean you can create java and xml file manually but then you have to do a little bit more work. After creating a new activity, android studio will automatically add some code in android_manifest file. Here you can change the home screen or launcher of app. Again if you are creating files individually then you have to add some code in manifest file manually.

what is android activity

 

  • Edit second_activity.xml

In this file drag and drop textview widget and change the text to “Second Activity”. This is a screen which will be showing up on button click. We don’t need to edit Second_Activity.java file. Leave it as it is.

  • Edit Main_Activity.java

In this file, we will write some important code. First of all, make instance of Button class and link it to it’s id. Then we will set the on click listener. There are two ways to create button click events. You can make a function also . We will the android studio what should happen on button click event. In this case we want to open new activity, right? So here Intent class will help us. Make the object of Intent class and its constructor will take two arguments. First is current class name and second is the name of the class which we want to open on button click.

Button button;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

// Get the view from activity_main.xml

setContentView(R.layout.activity_main);

// Locate the button in activity_main.xml

button = (Button) findViewById(R.id.MyButton);

// Capture button clicks

button.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {

// Start NewActivity.class

Intent myIntent = new Intent(MainActivity.this,

NewActivity.class);

startActivity(myIntent);

}

});

}

Note- Change NewActivity.class with your second activity name.



Summary

I hope now you understand what is android activity and how we can make a new android activity. If you really want to learn android then never just copy and paste the code. Firstly try to understand the code and it will be beneficial if you write code manually. See the output of this android activity project in the video.

Gaurav singla

Founder of Techyunk.com. I write blogs about android tutorials, blogging tips, programming etc.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: