How to move from One Screen to Another in Android Studio.

Moving from one Activity to another Activity or  Screen to another screen is very easy, just follow the simple steps to do. Create your Android project, click on start new project, name your project, click next, select the version, click next select Empty Activity click finish.

Now in your Project, In Activity.xml take a TextView text it with MainActivity_Screen it shows that we are on the main screen. Take a Button whenever you click on Button it will go to the second screen, we can achieve this by calling the onClick method. The code for Activity.xml is given Below.


Activity.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:orientation="vertical"
  7. android:layout_height="match_parent"
  8. tools:context="com.example.user.application4.MainActivity">
  9. <TextView
  10. android:text="MainActivity_Screen"
  11. android:textSize="40dp"
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content" >
  14. </TextView>
  15. <Button
  16. android:id="@+id/button"
  17. android:onClick="click"
  18. android:layout_width="match_parent"
  19. android:layout_height="wrap_content"
  20. android:text="Send">
  21. </Button>
  22. </LinearLayout>


In MainActivity.java, where we have an onClick method here we define Intent, An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed. The code for MainActivity is given below.

MainActivity.java
  1. import android.content.Intent;
  2. import android.support.v7.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. public class MainActivity extends AppCompatActivity {
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_main);
  10. }
  11. public void click(View view) {
  12. Intent in = new Intent(MainActivity.this,SecondActivity.class);
  13. startActivity(in);
  14. }
  15. }

Before writing code MainActivity.java, we need to create second Activity for that go to File-new-Activity-Empty, now name your Activity click finish. There is no code we need to write in Second Activity. But in SecondActivity.xml, take a TextView to give text as ScecondActivity_Screen. The code is given below.

SecondActivity.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:background="#F7B2C5"
  8. tools:context="com.example.user.application4.SecondActivity">
  9. <TextView
  10. android:text="SecondActivity_Screen"
  11. android:textSize="30dp"
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content" />
  14. </LinearLayout>
SecondActivity.java
  1. import android.support.v7.app.AppCompatActivity;
  2. import android.os.Bundle;
  3. public class SecondActivity extends AppCompatActivity {
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_second);
  8. }
  9. }

Below is For more detail, check out.

Hope this post is helpful,for more useful post like and share with your friends, we also have Youtube channel for more details check out the video below, do like share and subscribe to our channel it will boost our energy and help to improve our work, Have Nice Day.
How to move from One Screen to Another in Android Studio. How to move from One Screen to Another in Android Studio. Reviewed by gcrt on March 19, 2018 Rating: 5

No comments:

'; (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();
Powered by Blogger.