Reputation: 75
I'm making a little project in which i would enter a code-name, for example, "A" for Facebook. I would like it to open up the Facebook app when i enter "A" in the edittext area. How can i do this? I know there's a way to give it a package name and then open it up using an intent, but that seems really inconvenient.
Upvotes: 1
Views: 42
Reputation: 4651
It's prety easy, declare the editText then get the text in it and if it equals to what ever you want then make an intent that opens the app.
EditText type = (EditText) findViewById(R.id.edittext1);
String xx = type.getText.toString();
if (xx == "A"){
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.package.address");
startActivity(LaunchIntent);
} else{
}
or you can use this method
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName("com.package.address","com.package.address.MainActivity"));
startActivity(intent);
but I recomment the first one because you may not know the mainActivity's name of the app.
Upvotes: 2