Hi i'm working on a android launcher for education and I need it to be able to when the user clicks the school tools button it launches the school tools app that is installed on the device
Here's the code
package com.d4a.stzh;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.content.Intent;
import com.actionbarsherlock.app.SherlockFragment;
public class FragmentTab1 extends SherlockFragment {
private Button appbtn;
private Button webbtn;
private Button toolsbttn;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Get the view from fragmenttab1.xml
View view = inflater.inflate(R.layout.fragmenttab1, container, false);
//Get the button from layout
appbtn = (Button) view.findViewById(R.id.app);
webbtn = (Button) view.findViewById(R.id.web);
toolsbttn = (Button) view.findViewById(R.id.tools);
//show all apps installed on the device
appbtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(FragmentTab1.this.getActivity(), MyLauncherActivity.class);
startActivity(intent);
}
});
//luanches google on the default web browser
webbtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String url = "http://www.google.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
//tools button i know ths code is wrong!I need help here!
toolsbttn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(FragmentTab1.this.getActivity(), MyLauncherActivity.class);
startActivity(intent);
}
});
return view;
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
setUserVisibleHint(true);
}
}
I am still new to android coding so please don't judge me
Any help would be amazing
Thanks way in advance
Regards
Rapsong11
This code snippet should do exactly what you are trying to achieve
Intent i;
PackageManager manager = getPackageManager();
try {
i = manager.getLaunchIntentForPackage("com.example.schoolToolApp");
if (i == null)
throw new PackageManager.NameNotFoundException();
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
} catch (PackageManager.NameNotFoundException e) {
}
It will just launch another app by its package name
source - Open another application from your own (intent)
Related
I'm trying to make a quiz app and everything was working fine until I put in my code for my second button, now when I click start nothing happens and clicking study brings up a black screen. Start is supposed to take the user to a different activity, and study is supposed to take them to a website. Can someone check what's wrong with my code?
package com.example.rupin.whosthatpokemon;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class questionactivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_questionactivity);
Button start = findViewById(R.id.start);
start.setOnClickListener(
new Button.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent (getApplicationContext(), one.class);
startActivity(intent);
}
});
start = findViewById(R.id.study);
start.setOnClickListener(
new Button.OnClickListener() {
#Override
public void onClick(View view) {
Intent i;
i = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.pokemon.com/us/pokedex/"));
startActivity(i);
}
});
}
public void goToActivity2 (View view){
Intent intent = new Intent (this, one.class);
startActivity(intent);
}
}
As Loris Securo said in the comments, "
you have btn.setOnClickListener twice instead of start.setOnClickListener". That means you never set the onClickListener of your start button.
Also, in the second onClickListener, you have:
i = new Intent(view.getContext(), one.class);
You should instead do:
i = new Intent(questionactivity.this, one.class);
Although view.getContext() should technically work, I always seen this used as the first parameter in the Intent constructor which is a Context object. Since this (an instance of an Activity) can be cast to a Context, it is better to get the context of the outer class , and this would explain why you get a black screen when trying to go to the other activity.
Side note: Your class names should start with a capital letter and be CamelCased such as ClassOne or QuestionActivity.
Hi I currently made a game and just wish to add a simple menu for it, the are two buttons on the main menu was says English and the Other says French I have managed to get the English button working which takes me to my EnglishVersion.class but I cant seem to get my French button to work. can any one help please.
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainMenu extends Activity {
Button English;
Button French;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
addListenerOnButton();
}
public void addListenerOnButton(){
final Context context = this;
English = (Button) findViewById(R.id.engbtn);
French = (Button) findViewById(R.id.frenbtn);
English.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, EnglishVersion.class);
startActivity(intent);
}
});
}
I have tried to do this and getting and im getting an error
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainMenu extends Activity {
Button English;
Button French;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
English.setOnClickListener(mButtonClickListener);
French.setOnClickListener(mButtonClickListener);
}
private View.OnClickListener mButtonClickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
if (view.getId()==R.id.engbtn) {
Intent intent = new Intent(context, EnglishVersion.class);
startActivity(intent);
} else if (view.getId()==R.id.frenbtn) {
Intent intent = new Intent(context, FrenchVersion.class);
startActivity(intent);
}
}
}
}
add this:
French.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, FrenchVersion.class);
startActivity(intent);
}
});
By the way, it's not a good way to do the internationalization.
Try to read little about it here
Supporting Different Languages
You can just create various files strings-XX.XML in your res/ directory, where XX is given language symbol like FR or EN. It will even work with your default one. So if phone user has French defined as a language, your application will take it as its.
Either use the onClick property in XML like this (for both buttons);
<Button
android:id="#+id/engbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"/>
<Button
android:id="#+id/frenbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"/>
then populate the onClick method in the activity;
public void onClick(View view) {
if (view.getId()==R.id.engbtn) {
//do something
} else if (view.getId()==R.id.frenbtn) {
//do something else
}
}
OR set the onClickListeners for both button like this;
English.setOnClickListener(mButtonClickListener);
French.setOnClickListener(mButtonClickListener);
and then define the shared click listener;
private View.OnClickListener mButtonClickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
if (view.getId()==R.id.engbtn) {
//do something
} else if (view.getId()==R.id.frenbtn) {
//do something else
}
}
}
Hope this helps!
I am working on an application called Android Glossary. In my second activity I have created a TextView which has a link embedded in it. Everything seems fine; the link is highlighted in blue. The problem is that when I click on the link, my application crashes down. I don't know what is wrong. Please help.
Code in my second activity :
package com.mavenmaverick.androidglossary;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class CActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_c);
TextView link = (TextView) findViewById(R.id.link);
link.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Uri adress= Uri.parse("http;//www.cyanogenmod.com");
Intent browser= new Intent(Intent.ACTION_VIEW, adress);
startActivity(browser);
}
});
}
see your code
you have enter url like
Uri adress= Uri.parse("http;//www.cyanogenmod.com");
replace with this
Uri adress= Uri.parse("http://www.cyanogenmod.com");
problm is ; in Http; replace with http:
you should use setData()
String url = "http://www.cynogenmod.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
TextView link = (TextView) findViewById(R.id.link);
link.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent browser = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.cyanogenmod.com"));
startActivity(browser);
}
});
I'm wondering how I can get the code below to work with my project as to combine the two so that it could work. What I have is an implemented Navigation Drawer in the app
1st Java Code Using Fragment:
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class PagesFragment extends Fragment {
public PagesFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_pages, container, false);
return rootView;
}
}
2nd Java Code Using Activity:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class AppointmentActivity extends Activity {
Button sendEmail;
EditText msg;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.appointment_layout);
sendEmail = (Button) findViewById(R.id.sndBtn);
sendEmail.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
msg = (EditText) findViewById(R.id.msgTxt);
String message = msg.getText().toString();
sendEmail(message);
}
});
}
protected void sendEmail(String message) {
String[] to=new String[]{"Shop#email.com"};
String subject=("Shop Appointment");
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, to);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, message);
emailIntent.setType("message/rfc822");
startActivity(Intent.createChooser(emailIntent, "Gmail"));
}
}
I've tried to combine the two myself but I don't have a lot of experience dealing with android to know how to make both of these codes work side by side and not give me a force close. Anything would be helpful!
To combine these codes, you need to declare your Activity like a FragmentActivity (it will be "host" your Fragment). See this answer: https://stackoverflow.com/a/10609839/2668136
And Google training Fragment
Also Google documentation FragmentActivity
Hope this help.
My probablem is I have all the code and there is no errors on it (accrording to eclipse) but when I try to open up "page1" on my app it freezes then crashes, if I get ride of all of the addPoints information the page runs fine, can you help me find out what is causing the crash? Thanks!
heres my code
package com.canadais.civics;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class page1 extends Activity implements OnClickListener
{
TextView Q1A1;
TextView Q1A2;
public static final String PREFS_NAME = "MyPrefsFile";
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
public int testScore = (settings.getInt("YourScore", 0));
Intent page2 = new Intent (this, Page2.class);
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
Q1A1 = (TextView) findViewById(R.id.Q1A1);
Q1A2 = (TextView) findViewById(R.id.Q1A2);
Q1A1.setOnClickListener(this);
Q1A2.setOnClickListener(this);
//test = (TextView) findViewById(R.id.test);
//test.setText(settings.getInt("YourScore", 0));
}
public void onClick(View v)
{
switch(v.getId())
{
case R.id.Q1A1:
addPoints(10);
//Intent page2 = new Intent (this, Page2.class);
startActivity(page2);
break;
case R.id.Q1A2:
addPoints(5);
//Intent page22 = new Intent (this, Page2.class);
startActivity(page2);
break;
}
}
public void addPoints(int points)
{
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("YourScore", (testScore + points));
editor.commit();
}
}
There are several possible reasons if you encounter problems after executing startActivity(). I can think of two right now:
Missing Activity declaration in AndroidManifest.xml;
Incorrect codes in the Activity(Page2.java in your case) to be started.
To ensure which error you have encountered, I suggest you should learn to post error message appeared in the LogCat which is bundled in Android SDK but not the one pop-up on the phone screen, otherwise, no one could accurately figure out the problem(s).