Activity: Activity is a java class that we use in Android Development. It takes care of creating a new window for developers in which you can place your UI(user interface). Well, activities can be of any type, it can be a full-screen window, a floating window or even embedded inside of another activity.
Here, we are going to create a new Activity by clicking on Button.
1) First create a new Project and name it "MyActivity"
2) Now create a new Blank Activity.
Go to app->java and right click on it.
Go to New->Activity->BlankActivity
Now give "NewActivity" as Activity name.
Here, we are going to create a new Activity by clicking on Button.
1) First create a new Project and name it "MyActivity"
2) Now create a new Blank Activity.
Go to app->java and right click on it.
Go to New->Activity->BlankActivity
Now give "NewActivity" as Activity name.
3) In app->res->layout->activity_main.xml, file write this code
<Button
android:id="@+id/press_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/press"
android:layout_centerInParent="true"/>
your activity_main.xml file should look like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<Button
android:id="@+id/press_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/press"
android:layout_centerInParent="true"/>
</RelativeLayout>
<string name="press">Press</string>
5) Now add the below code in onCreate function of your MainActivity.java fileButton Press = (Button)findViewById(R.id.press_button);
Press.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent myActivity = new Intent(getBaseContext(),NewActivity.class);
startActivity(myActivity);
}
});
finally, your onCreate function will look like:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button Press = (Button)findViewById(R.id.press_button);
Press.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent myActivity = new Intent(getBaseContext(),NewActivity.class);
startActivity(myActivity);
}
});
}
Note: import Intent library ,
import android.content.Intent;
import android.view.View;
import android.widget.Button;
6) Now go to app->res->layout-activity_new.xml and add this code
<TextView
android:text="@string/button_pressed"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
finally, activity_new.xml should look like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.company.entertainmentbucket.myactivity.NewActivity">
<TextView
android:text="@string/button_pressed"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
7) Go to app->res->values->strings.xml and add the below line
<string name="button_pressed">Button is Pressed</string>
That's it....

No comments:
Post a Comment