How to start other application from my activity on button click using Intent.
In this tutorial we would going to create an android application which would open any pre installed app in our android mobile phone. This type of functionality basically used where android developer needs to launch a specific android app from current activity. So here is the complete step by step tutorial for Launch Another Installed App From Your App in Android.
Also Read:
- How to find any application package name from Google Play Store.
- How to open any app from Your App directly into Google Play Store.
How to Launch Another Installed App From Your App in Android Programmatically.
Code for MainActivity.java file.
package com.android_examples.launchanotherapp_android_examplescom; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { Button buttonChrome, buttonYouTube; String GoogleChromePackageName = "com.android.chrome"; String YouTubePackageName = "com.google.android.youtube"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); buttonChrome = (Button)findViewById(R.id.button); buttonYouTube = (Button)findViewById(R.id.button2); buttonChrome.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // Passing Google Chrome Package Name Here. Intent intent = getPackageManager().getLaunchIntentForPackage(GoogleChromePackageName); startActivity(intent); } }); buttonYouTube.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // Passing YouTube Package Name Here. Intent intent = getPackageManager().getLaunchIntentForPackage(YouTubePackageName); startActivity(intent); } }); } }
Code for activity_main.xml layout file.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" 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="com.android_examples.launchanotherapp_android_examplescom.MainActivity"> <Button android:text="Lauch YouTube App" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/button" android:layout_centerHorizontal="true" android:id="@+id/button2" /> <Button android:text="Launch Google Chrome App" android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/button" android:layout_marginTop="199dp" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> </RelativeLayout>
Screenshots: