Android : One Activity To Another Activity
(One Screen To Another Screen).
In this tutorial, We Will See How to Interact With Activity, When a Button is Clicked, Navigate from current screen (current activity) to another screen (another activity).
Create New Android Project. I Named It screen2screen.
Now Go To Your XML Layout. In Your activity_main.xml Add One Large TextView And One Button.
Your activity_main.xml Will Look Like Below.
activity_main.xml
Then Select Android.
Then Click Finish.
New Activity Will Be Added To Your Project.
Now Go Res-->Layout-->activity_second.xml
Add One TextView To It as This is second activity
Your activity_second.xml will Look Like Below.
Now Run Your Program And Check Output on Emulaator.
My Output Screen's Are as Below.
After Clicking On Button.
All The Best :)
(One Screen To Another Screen).
In this tutorial, We Will See How to Interact With Activity, When a Button is Clicked, Navigate from current screen (current activity) to another screen (another activity).
Create New Android Project. I Named It screen2screen.
Now Go To Your XML Layout. In Your activity_main.xml Add One Large TextView And One Button.
Your activity_main.xml Will Look Like Below.
activity_main.xml
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="28dp"
android:layout_marginTop="87dp"
android:text="This Is Main Activity"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Click Here To Go Second Activity" />
</RelativeLayout>
Now Create New Activity Or Second Activity.
First Right Clock On Your Project Or On Your Activity Then Click On "New " Option Then Click On "Other".
Then Below Window Will Present On Your Screen.
Then Select Android.
Now Select Android Activity And Click To Next.
Then Name It To The New Or Second Activity Or Screen Whatever U Want , I Named It Second.
Then Click Finish.
New Activity Will Be Added To Your Project.
Now Go Res-->Layout-->activity_second.xml
Add One TextView To It as This is second activity
Your activity_second.xml will Look Like Below.
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Second" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="174dp"
android:text="This Is The Second Activity"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Now You Have To Add Code To Main Activities Button To Go To New Activity.
For Switching Between Two Activites Below Code Will Be Useful.
Intent intent = new Intent(context,Second.class);
startActivity(intent);
startActivity(intent);
Add This Code To Your MainActivity.java file.
The File Will Look Like Below.
package
arshad.screen2screen;
import android.os.Bundle;
import
android.app.Activity;
import
android.content.Context;
import
android.content.Intent;
import android.view.Menu;
import android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
public class MainActivity extends Activity {
Button second;
final Context context = this;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
second = (Button)
findViewById(R.id.button1);
second.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent
intent = new Intent(context,Second.class);
startActivity(intent);
}
});
}
@Override
public boolean
onCreateOptionsMenu(Menu menu) {
// Inflate the
menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Now Run Your Program And Check Output on Emulaator.
My Output Screen's Are as Below.
After Clicking On Button.
All The Best :)
No comments:
Post a Comment