This is also useful for you
- How to Create Image Slider using ViewPager in android studio
- How to create Swipeable Videos Like TikTok Using ViewPager2 in andriod studio
- how to get all audio files in android programmatically
- How to use SearchView in andriod studio
- How to check internet Connection in androd studio
- How to Create Bottom Sheet in android studio
- How to Refresh a list Using RecyclerView in Android Studio
- How to create Welcome Splash Screen with animation in android studio
- How to create menu and sub menu in android studio
- How to create a list item with RecyclerView in Android Studio
- BottomNavigationView android example
- How to create Text to Speech app in android studio
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/whatsApp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="80dp"
android:text="open WhatsApp" />
<Button
android:id="@+id/business_Whats"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:text="open WA Business" />
<Button
android:id="@+id/facebook"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:text="open facebook" />
<Button
android:id="@+id/instagram"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:text="open instagram" />
</LinearLayout>
Here you are seeing four buttons on which we will use OnClickListener so that we will be able to open different aap. Such as whatsapp, facebook, instagram and whatsapp business.After creating activity_main, now in MainActivity.java we will use OnClickListener on these buttons so that we will be able to open a different app. import androidx.appcompat.app.AppCompatActivity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Button wa,ba,fa,in;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wa=findViewById(R.id.whatsApp);
ba=findViewById(R.id.business_Whats);
fa=findViewById(R.id.facebook);
in=findViewById(R.id.instagram);
wa.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String whatsAppPackageName="com.whatsapp";
if (checkInstall(whatsAppPackageName)){
openApp(whatsAppPackageName);
}
else {
Toast.makeText(MainActivity.this, "WhatsApp Not Install", Toast.LENGTH_SHORT).show();
}
}
});
ba.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String whatsAppPackageName="com.whatsapp.w4b";
if (checkInstall(whatsAppPackageName)){
openApp(whatsAppPackageName);
}
else
{
Toast.makeText(MainActivity.this, "WhatsApp Business Not Install", Toast.LENGTH_SHORT).show();
}
}
});
fa.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String fa="com.facebook.katana";
if (checkInstall(fa)){
openApp(fa);
}
else
{
Toast.makeText(MainActivity.this, "Facebook Not Install", Toast.LENGTH_SHORT).show();
}
}
});
in.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String insta="com.instagram.android";
if (checkInstall(insta))
{
openApp(insta);
}
else {
Toast.makeText(MainActivity.this, "Instagram not Not Install", Toast.LENGTH_SHORT).show();
}
}
});
}
private boolean checkInstall(String whatsAppPackageName) {
PackageManager pm = getPackageManager();
boolean app_installed = false;
try {
pm.getPackageInfo(whatsAppPackageName, PackageManager.GET_ACTIVITIES);
app_installed = true;
} catch (PackageManager.NameNotFoundException e) {
app_installed = false;
}
return app_installed;
}
private void openApp(String whatsAppPackageName) {
Intent intent = getPackageManager().getLaunchIntentForPackage(whatsAppPackageName);
startActivity(intent);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sharelistproject">
<queries>
<package android:name="com.whatsapp" />
<package android:name="com.whatsapp.w4b" />
<package android:name="com.instagram.android" />
<package android:name="com.facebook.katana" />
</queries>
<queries>
<intent>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https" />
</intent>
<intent>
<action android:name="android.support.customtabs.action.CustomTabsService" />
</intent>
</queries>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.ShareListProject">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
if app not open then use package name in manifest inside queries.
Here we are using four buttons and calling the method to open the app using OnClickListener on all the buttons.In every OnclickListener, first of all we will assign the package name of the app to the string we want to open in that variable.After that in the if condition we will call the checkInstall method which will take a string type parameter (package name of app) which will check every time that the app user's device is installed or not if the app is installed inside the body of the if condition If the app is not installed, then another method will run, in which a toast will be printed that the app is not found or that you want to print.Similarly, by assigning the package name of all the app to a string variable, checkinstall in if else and call the openApp method and give it app package name.If you do not want to assign the app package name to a string, then instead of the app string, write the package name directly under "write package name".Below is a screenshot video of it, so that you will know how the app will work.I hope this article will be very useful for you.Thanks You
0 Comments
Please do not enter any spam link in the comment box
Emoji