Android Tutorial Intent


In Android a single screen is called activity. For example a chat application can have many activities like login, register etc.
In android to run another activity from an activity Intent is used. Intent performs many tasks but the most common use of Intent is the opening of another activity.
In this tutorial we will create two activities and will create a button in our first activity to go to the second activity and also a button in our second activity to go to the first activity.

Here is the code 

XML 

Activity 1:

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:app="http://schemas.android.com/apk/res-auto" 
 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" 
 app:layout_behavior="@string/appbar_scrolling_view_behavior" 
 tools:showIn="@layout/activity_main">


    <Button 
 style="?android:attr/buttonStyleSmall" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="Button" 
 android:id="@+id/button" 
 android:onClick="onButtonClick" 
 android:layout_marginTop="187dp" 
 android:layout_alignParentTop="true" 
 android:layout_centerHorizontal="true" />

    <TextView 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:textAppearance="?android:attr/textAppearanceLarge" 
 android:text="Activity 1" 
 android:id="@+id/textView" 
 android:layout_alignParentTop="true" 
 android:layout_centerHorizontal="true" 
 android:layout_marginTop="62dp" />


</RelativeLayout>
 
Activity 2: 
 
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:app="http://schemas.android.com/apk/res-auto" 
 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" 
 app:layout_behavior="@string/appbar_scrolling_view_behavior" 
 tools:context="com.example.saleem.myfirstandroidapp.Main2Activity" 
 tools:showIn="@layout/activity_main2">


    <Button 
 style="?android:attr/buttonStyleSmall" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="Button" 
 android:id="@+id/button2" 
 android:onClick="activityTwoButtonClick" 
 android:layout_marginTop="187dp" 
 android:layout_alignParentTop="true" 
 android:layout_centerHorizontal="true" />

    <TextView 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:textAppearance="?android:attr/textAppearanceLarge" 
 android:text="Activity 2" 
 android:id="@+id/textView2" 
 android:layout_alignParentTop="true" 
 android:layout_centerHorizontal="true" 
 android:layout_marginTop="62dp" />

</RelativeLayout>

Java Code of Activity 1:

public class MainActivity extends AppCompatActivity {



    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

public void activityOneButtonClick(View view){
    Intent intent = new Intent(MainActivity.this,Main2Activity.class);
    startActivity(intent);
}
}
 
Java Code for activity 2:
 
public class Main2Activity extends AppCompatActivity {

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);


    }
    public void activityTwoButtonClick(View view){
        Intent intent = new Intent(Main2Activity.this, MainActivity.class);
        startActivity(intent);
    }

}

Opening one activity from another activity
 
 
 

0 comments: