Using If and else to change to change layout file # runtime - android

I am currently working on a project that i am trying to add an icon making competition and the winner will receive a special app plugin that only the contest winner will get and i would like my app to check if the package(special app plugin) exists on the users device and if it does i would like the app to display an alternate display on run-time.Would i use "if"
and "else" statements to achieve this and if so how would i go about this and Thanks in advanced.
Note:I have successfully made the app load a different layout depending on the android version so i have a little bit of an idea but need some help.

Check this to know app is installed or not..
public class Example extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Put the package name here...
boolean installed = appInstalledOrNot("com.Ch.Example.pack");
if(installed)
{
//This intent will help you to launch if the package is already installed
Intent LaunchIntent = getPackageManager()
.getLaunchIntentForPackage("com.Ch.Example.pack");
startActivity(LaunchIntent);
System.out.println("App already installed om your phone");
}
else
{
System.out.println("App is not installed om your phone");
}
}
private boolean appInstalledOrNot(String uri)
{
PackageManager pm = getPackageManager();
boolean app_installed = false;
try
{
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
app_installed = true;
}
catch (PackageManager.NameNotFoundException e)
{
app_installed = false;
}
return app_installed ;
}
}

Well if your plugin has a unique name(i am sure it should) you can check it.
Please read the post linked below for checking whether the plugin exists or not
Link : https://stackoverflow.com/a/6758962/1542720
Hope this helps !

you can change your xml file according to your requirement in onCreate()..
as i did in my code, for the different density i used different xml file.
if (metrics.densityDpi == DisplayMetrics.DENSITY_MEDIUM) {
setContentView(R.layout.activity_main);
} else if (metrics.densityDpi == DisplayMetrics.DENSITY_LOW) {
setContentView(R.layout.activity_main_small);
} else {
setContentView(R.layout.activity_main_large);
}

Related

How to check if zoom.us and Slack apps are installed on Android?

I'm trying to create an app in Android where a user can install zoom.us and Slack apps and run them but I need to check before installation if the app is already installed or not. The problem is I don't know the names of the packages so I can check against them, What would be the name of packages for zoom.us and slack and How would I run them by click of zoom and slack buttons?
public class MainActivity extends AppCompatActivity {
ImageButton zoom, slack;
Button installZoom, installSlack;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Install zoom
installZoom = (Button) findViewById(R.id.inst_zoom);
if (isZoomClientInstalled(getApplicationContext())) {
installZoom.setEnabled(false);
} else {
installZoom.setEnabled(true);
installZoom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=us.zoom.videomeetings"));
startActivity(intent);
}
});
}
// Run zoom
zoom = (ImageButton) findViewById(R.id.app_zoom);
zoom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "About to run zoom", Toast.LENGTH_SHORT).show();
}
});
// Install Slack
installSlack = (Button) findViewById(R.id.inst_slack);
if (isSlckClientInstalled(getApplicationContext())) {
installSlack.setEnabled(false);
} else {
installSlack.setEnabled(true);
installSlack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse("https://slack.com/downloads/android"));
startActivity(intent);
}
});
}
// Run Slack
slack = (ImageButton) findViewById(R.id.app_slack);
slack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "About to run Slack", Toast.LENGTH_SHORT).show();
}
});
}// End of Create();
// Determine whether the zoom for Android client is installed on this device.
public boolean isZoomClientInstalled(Context myContext) {
PackageManager myPackageMgr = myContext.getPackageManager();
try {
myPackageMgr.getPackageInfo("???.???.??", PackageManager.GET_ACTIVITIES);
} catch (PackageManager.NameNotFoundException e) {
return (false);
}
return (true);
}
// Determine whether the Slack for Android client is installed on this device.
public boolean isSlackClientInstalled(Context myContext) {
PackageManager myPackageMgr = myContext.getPackageManager();
try {
myPackageMgr.getPackageInfo("???.???.??", PackageManager.GET_ACTIVITIES);
} catch (PackageManager.NameNotFoundException e) {
return (false);
}
return (true);
}
}// End of class
you may find app package by looking into Google Play link
https://play.google.com/store/apps/details?id=com.Slack
com.Slack is package name here. us.zoom.videomeetings for Zoom. Then you just start it with Intent. Try it.
If you know the package name for the app, then you can check that if that app is installed on the device or not.
PACKAGE NAMES:
Zoom.Us: us.zoom.videomeetings
Slack: com.Slack
You know the code for it as stated in the comments. By running it you will know if the app is installed on the device or not.
To check if app is installed or not, you need to know the package name of the app you want to check. You can find the package name of the app from Google play store, focus on URL. Id in URL is the package name.
For Example for Zoom.us it is: us.zoom.videomeetings
Since both apps are well established, it is highly unlikely they will update the package name.

Check the availability of GoogleMaps on Android if not direct to Play store for install

I implement a drawing route app on GoogleMaps and that is working but first I need to check for GoogleMaps availablity on Android or not. If the app does not exist, the user should be directed to Google Play Store to install GoogleMaps.
How can I prompt the user to install it?
Try this:
private boolean isPackageInstalled(String packagename, PackageManager packageManager) {
try {
packageManager.getPackageInfo(packagename, 0);
return true;
} catch (NameNotFoundException e) {
return false;
}
}
It attempts to fetch information about the package whose name you passed in. Failing that, if a NameNotFoundException was thrown, it means that no package with that name is installed, so we return false.
in your case
public void someMethod() {
// ...
String googleMapsPackageName = "com.google.android.apps.maps"
PackageManager pm = context.getPackageManager();
boolean isInstalled = isPackageInstalled(googleMapsPackageName, pm);
if(isInstalled)
// go to maps
else
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + googleMapsPackageName)));
// ...
}

Integrating Ola money api in android always getting app not installed?

Hello all i am integrating ola money with in my android app now the problem i am facing is that in their docs it is given that first check for whether the app is installed and for that they have given below code to see that, now i am having ola cabs app installed in my android device but this function is returning false i dont know what i am doing wrong, if somebody has integrated ola in android please tellme how is it working for you
private boolean check_olacabs() {
try {
context.getPackageManager().getApplicationInfo("com.test.olacabs", 0);
return true;
} catch (Exception e) {
return false;
}
}
Thank you in advance
Replace your code with this.
private boolean check_olacabs() {
PackageManager pm = getPackageManager();
try {
pm.getPackageInfo("com.olacabs.customer", PackageManager.GET_ACTIVITIES);
return true;
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}

Check if on device has a certain application

In my application i need to check if on my devices is present "Amazon Appstore". How can I do this?
Guide Me. I have no idea. Thanks.
Try this
private boolean isAppInstalled(String packageName) {
PackageManager packageManager = getPackageManager();
boolean installed = false;
try {
packageManager.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
installed = true;
} catch (PackageManager.NameNotFoundException e) {
installed = false;
}
return installed;
}
Just get the package name and use package manager to get all the apps instead and check one by one with the package name you have. You can get package name from the google play link of the app you need.
Try to see here if could helps you: http://www.coderzheaven.com/2012/03/28/how-to-check-whether-an-application-is-installed-in-your-android-phone-2/

How to detect if Google Play is installed? (Not Market)

Whether the app installed is called Google Play or Market, the package name is the same com.android.vending.
I need to be able to detect whether the app is Google Play or Market, I've checked in PackageInfo and nothing except versionCode and versionName can be of help.
Does anyone know what the first versionCode was or versionName was for Google Play app?
If anyone knows any other way of detecting this let me know.
I figured out how to check the application label. I was using the debugger to see what all was being returned in packageInfo that's why I didn't see it initially.
public static boolean isGooglePlayInstalled(Context context) {
PackageManager pm = context.getPackageManager();
boolean app_installed = false;
try
{
PackageInfo info = pm.getPackageInfo("com.android.vending", PackageManager.GET_ACTIVITIES);
String label = (String) info.applicationInfo.loadLabel(pm);
app_installed = (label != null && !label.equals("Market"));
}
catch (PackageManager.NameNotFoundException e)
{
app_installed = false;
}
return app_installed;
}
You can also try this much simplified solution:
public boolean isGooglePlayAvailable() {
boolean googlePlayStoreInstalled;
int val= GooglePlayServicesUtil.isGooglePlayServicesAvailable(LocationActivity.this);
googlePlayStoreInstalled = val == ConnectionResult.SUCCESS;
return googlePlayStoreInstalled;
}
In my App I check possibility to open play store before fire it like:
public static boolean isResolveActivity(Intent intent) {
return App.getInstance().getPackageManager().resolveActivity(intent, PackageManager.GET_RESOLVED_FILTER) != null;
}
public void isResolveActivity(String appPackage) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackage));
if(isResolveActivity(intent)){
...open intent
}
}
You can use this simple piece of code, its easy and to the point with a consideration for not re-inventing the wheel using GooglePlayServicesUtil:
public static boolean isPlayStoreInstalled(Context context){
try {
context.getPackageManager()
.getPackageInfo(GooglePlayServicesUtil.GOOGLE_PLAY_STORE_PACKAGE, 0);
return true;
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}
This will require you to add this to your dependencies:
compile 'com.google.android.gms:play-services-base:[PLAY_SERVICES_VERSION]'
Latest play-services version is now: 10.0.1
This is probably a better example as it allows for status' where the user can do something about it i.e re-auth or update. Based on the code in the GCM client example project:
public static boolean checkPlayServices(Activity activity) {
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(activity);
if (resultCode != ConnectionResult.SUCCESS) {
if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
GooglePlayServicesUtil.getErrorDialog(resultCode, activity,
PLAY_SERVICES_RESOLUTION_REQUEST).show();
} else {
Toast.makeText(activity.getApplicationContext(), "This device is not supported.", Toast.LENGTH_LONG).show();
activity.finish();
}
return false;
}
return true;
}

Categories

Resources