In my Android application, I have two activity classes. I have a button on the first one and I want to show the second when it is clicked, but I get an error. Here are the classes:
public class FirstActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button orderButton = (Button)findViewById(R.id.order);
orderButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(FirstActivity.this, OrderScreen.class);
startActivity(intent);
}
});
}
}
The second class that should show when the button is clicked, but never does:
public class OrderScreen extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.order);
Button orderButton = (Button) findViewById(R.id.end);
orderButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
}
}
How do I create a button that will show the second activity?
The issue was the OrderScreen Activity wasn't added to the AndroidManifest.xml. Once I added that as an application node, it worked properly.
<activity android:name=".OrderScreen" />
Add this line to your AndroidManifest.xml:
<activity android:name=".OrderScreen" />
----FirstActivity.java-----
package com.mindscripts.eid;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class FirstActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button orderButton = (Button) findViewById(R.id.order);
orderButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(FirstActivity.this,OrderScreen.class);
startActivity(intent);
}
});
}
}
---OrderScreen.java---
package com.mindscripts.eid;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class OrderScreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second_class);
Button orderButton = (Button) findViewById(R.id.end);
orderButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
}
---AndroidManifest.xml----
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mindscripts.eid"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".FirstActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".OrderScreen"></activity>
</application>
Use this code:
Intent intent=new Intent(context,SecondActivty.class);
startActivity(intent);
finish();
context: refer to current activity context,
please make sure that you have added activity in android manifest file.
Following code for adding activity in android manifest file
<Activity name=".SecondActivity">
</Activity>
<activity android:name="[packagename optional].ActivityClassName"></activity>
Simply adding the activity which we want to switch to should be placed in the manifest file
When you create any activity in android file you have to specify it in AndroidManifest.xml like
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".MyCreativityActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".OrderScreen"></activity>
</application>
b1 = (Button) findViewById(R.id.click_me);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, SecondActivity.class);
startActivity(i);
}
});
add the activity in your manifest file
<activity android:name=".OrderScreen" />
In the Manifest
<activity android:name=".OrderScreen" />
In the Java Code where you have to place intent code
startActivity(new Intent(CurrentActivity.this, OrderScreen.class);
you can use the context of the view that did the calling.
Example:
Button orderButton = (Button)findViewById(R.id.order);
orderButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(/*FirstActivity.this*/ view.getContext(), OrderScreen.class);
startActivity(intent);
}
});
Intent i = new Intent("com.Android.SubActivity");
startActivity(i);
Related
I have been trying to have my home menu working. I have 2 Buttons and I want them to start different activities when clicked.
this is my code
package com.example.clicktothink_1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn= (Button)findViewById(R.id.play_btn);
Button btn1 = (Button)findViewById(R.id.howToPlay_btn);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,QuizActivity.class);
startActivity(intent);
}
});
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent1 = new Intent(MainActivity.this,HowActivity.class);
startActivity(intent1);
}
});
}
}
I have already declared both activities in the manifest file. The first Intent works
Intent intent = new Intent(MainActivity.this,QuizActivity.class);
however the second Intent does not. Any idea how to get this working ?
stacktrace
org.xml.sax.SAXParseException; lineNumber: 19; columnNumber: 40; Open quote is expected for attribute "{1}" associated with an element type "android:parentActivityName".
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(Unknown Source)
at com.android.ide.common.xml.AndroidManifestParser.parse(AndroidManifestParser.java:612)
at com.android.ide.eclipse.adt.internal.project.AndroidManifestHelper.parseUnchecked(AndroidManifestHelper.java:75)
at com.android.ide.eclipse.adt.internal.build.builders.PreCompilerBuilder.build(PreCompilerBuilder.java:467)
at org.eclipse.core.internal.events.BuildManager$2.run(BuildManager.java:734)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:206)
at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:246)
at org.eclipse.core.internal.events.BuildManager$1.run(BuildManager.java:299)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:302)
at org.eclipse.core.internal.events.BuildManager.basicBuildLoop(BuildManager.java:358)
at org.eclipse.core.internal.events.BuildManager.build(BuildManager.java:381)
at org.eclipse.core.internal.events.AutoBuildJob.doBuild(AutoBuildJob.java:143)
at org.eclipse.core.internal.events.AutoBuildJob.run(AutoBuildJob.java:241)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)
here is the manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
package="com.example.clicktothink_1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
>
<activity
android:name="com.example.clicktothink_1.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name="com.example.clicktothink_1.HowActivity"/>
<activity android:name="com.example.clicktothink_1.QuizActivity"/>
<activity android:name="com.example.clicktothink_1.ResultActivity"/>
</application>
</manifest>
Try this:
public class MainActivity extends Activity implements View.OnClickListener {
private Button btn;
private Button btn1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.btn = (Button)findViewById(R.id.play_btn);
this.btn1 = (Button)findViewById(R.id.howToPlay_btn);
btn.setOnClickListener(this);
btn1.setOnClickListener(this);
}
public void onClick(View v) {
int id = v.getId();
if (id == this.btn.getId()) {
Intent intent = new Intent(this, QuizActivity.class);
startActivity(intent);
}
else if (id == this.btn1.getId()){
Intent intent1 = new Intent(this, HowActivity.class);
startActivity(intent1);
}
}
}
If doesn't work, take a look in the logcat.
if(check manifest file(androidmanifest.xml)){
if(check activity_main.xml(check all id are specified correctly)){
public void onClick(View v) {
switch(v.getId()){
case R.id.play_btn :
Intent intent = new Intent(MainActivity.this, QuizActivity.class);
startActivity(intent);
break;
case R.id.howtoplay_btn:
Intent intent = new Intent(MainActivity.this, HowActivity.class);
startActivity(intent);
break;
}
}
}else{
correct activity_main.xml
}
}else{
Delete android:layout_width and android:layout_height from the android:manifest.xml.
Your application will run.
If you want to know the reason read
[http://developer.android.com/guide/topics/manifest/manifest-intro.html]
}
I have an application with two Activitys.
The first Activity starts the second Activity on a Button click. In the second Activity, after I call finish(), also on a Button click, I expect that the application will return to the first Activity.
What happen is that the application gets minimized (goes in background). The device on which I am developing is a Sony Xperia Z2 with Android 4.4.2.
Is this an Android issue or is it something wrong that I am doing in code?
The manifest file:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.test.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.test.SecondActivity">
</application>
First activity onClick:
btn1 = (Button) findViewById(R.id.button1);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this, SecondActivity.class);
startActivity(i);
}
});
Second activity onClick:
btn2 = (Button) findViewById(R.id.button2);
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
First thing is that it is not a Android issue.
Secondly, please make sure you have not finished your first Activity when you are moving from First to Second.
If you are doing this in first class:
Intent in=new Intent(A.this, B.class);
startActivity(in);
this.finish();
then remove this.finish(); because it finishes up your first Activity and when you are coming back from second then first Activity is not present in the stack. So how it would be called.
First.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class First extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
}
Second.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Second extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
}
basically am a java developer but now am developing an android app.In my app i can't redirect to the next page when i click on to SIGN IN button and here is my code
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
Button log,sign;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
log = (Button) findViewById(R.id.login);
sign = (Button) findViewById(R.id.signup);
log.setOnClickListener(this);
sign.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()) {
case R.id.login:
break;
case R.id.signup:
Intent open = new Intent("com.example.eblood.Register");
startActivity(open);
break;
}
}
}
}
Here is my next page java code
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Register extends Activity{
Button backlog,regg;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
backlog = (Button) findViewById(R.id.linktologin);
regg = (Button) findViewById(R.id.register);
backlog.setOnClickListener((OnClickListener) this);
}
public void onClick (View v)
{
switch(v.getId()) {
case R.id.register:
break;
case R.id.linktologin:
Intent in = new Intent("com.example.eblood.MainActivity");
startActivity(in);
break;
}
}
}
Here is my manifest code.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.eblood"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.eblood.home"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.eblood.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.eblood.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.eblood.Register"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.REGISTER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
and the error am getting is android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.example.eblood.Register }
And here is my home.java
package com.example.eblood;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class home extends Activity {
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
Thread timer = new Thread(){
public void run(){
try{
sleep(2000);
} catch (InterruptedException e){
e.printStackTrace();
} finally {
Intent openMainActivity = new Intent("com.example.eblood.MAINACTIVITY");
startActivity(openMainActivity);
}
}
};
timer.start();}{
}
}
Try this..
If you use Intent open = new Intent("com.example.eblood.Register"); you will get ActivityNotFoundException change it as Intent open = new Intent(MainActivity.this,Register.class);
Change this
Intent open = new Intent("com.example.eblood.Register");
startActivity(open);
to
Intent open = new Intent(MainActivity.this,Register.class);
startActivity(open);
also
change this
Intent in = new Intent("com.example.eblood.MainActivity");
startActivity(in);
to
Intent in = new Intent(Register.this,MainActivity.class);
startActivity(in);
EDIT
Change this.
Intent openMainActivity = new Intent("com.example.eblood.MAINACTIVITY");
startActivity(openMainActivity);
to
Intent openMainActivity = new Intent(home.this,MainActivity.class);
startActivity(openMainActivity);
I get forceclose at imagebutton on passing intent. instead of passing intent i've passed toast on that onclick it has been running successfully but the intent passed is not running successfully.
enter code here:
what's the possibilities???
package com.account;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
public class AccountTrackerActivity extends Activity implements OnClickListener{
ImageButton user;
ImageButton acc_list;
ImageButton add_acc;
ImageButton add_transaction;
ImageButton search_trans;
ImageButton remainder;
ImageButton recent_trans;
TextView curr_user;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
user=(ImageButton)findViewById(R.id.imageButton2);
acc_list=(ImageButton)findViewById(R.id.imageButton3);
add_acc=(ImageButton)findViewById(R.id.imageButton4);
add_transaction=(ImageButton)findViewById(R.id.imageButton6);
search_trans=(ImageButton)findViewById(R.id.imageButton7);
remainder=(ImageButton)findViewById(R.id.imageButton8);
recent_trans=(ImageButton)findViewById(R.id.imageButton5);
curr_user= (TextView)findViewById(R.id.lblcurrent_user);
user.setOnClickListener(this);
acc_list.setOnClickListener(this);
add_acc.setOnClickListener(this);
add_transaction.setOnClickListener(this);
search_trans.setOnClickListener(this);
remainder.setOnClickListener(this);
recent_trans.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try
{
if(v.getId()==user.getId()){
Intent myIntentlogin=new Intent(AccountTrackerActivity.this,login.class);
startActivityForResult(myIntentlogin,101);
}
if(v.getId()==acc_list.getId()){
//Intent myIntentacc_list=new Intent(getApplicationContext(),acc_list.class);
Toast.makeText(getApplicationContext(),"hi....", Toast.LENGTH_LONG).show();
//startActivityForResult(myIntentacc_list,101);
//startActivity(myIntentacc_list);
}
if(v.getId()==add_acc.getId()){
Intent myIntentadd_acc=new Intent(AccountTrackerActivity.this,add_acc.class);
startActivityForResult(myIntentadd_acc,103);
}
if(v.getId()==add_transaction.getId()){
Intent myIntentadd_transaction=new Intent(AccountTrackerActivity.this,add_transaction.class);
startActivityForResult(myIntentadd_transaction,104);
}
if(v.getId()==search_trans.getId()){
Intent myIntentsearch_trans=new Intent(AccountTrackerActivity.this,search_trans.class);
startActivityForResult(myIntentsearch_trans,105);
}
if(v.getId()==remainder.getId()){
Intent myIntentremainder=new Intent(AccountTrackerActivity.this,remainder.class);
startActivityForResult(myIntentremainder,106);
}
if(v.getId()==recent_trans.getId()){
Intent myIntentrecent_trans=new Intent(AccountTrackerActivity.this,recent_trans.class);
startActivityForResult(myIntentrecent_trans,107);
}
}
catch(Exception e)
{
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
to pass data from one activity
Intent myIntentlogin= new Intent(AccountTrackerActivity.this,login.class);
myIntentlogin.putExtra("message", 103); //pass data to new activity here like this
startActivity(myIntentlogin);
to receive data in another
Intent intent = getIntent();
String message = intent.getStringExtra("message");
Refer this link
Try this in your code
ImageButton ButtonOne=(ImageButton) findViewById(R.id.imageButton1);
And your onclick listener is given below
ButtonOne.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
teamsName=db.getAllTeams();
teamone=true;
Intent intent=new Intent(MainActivity.this,NextActivity.class);
intent.putExtra("item", teamsName);
startActivityForResult(intent, 0);
}
});
And you need to add your NextActivity in manifest file.
Your Mainfest is look like below
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.design.MainActivtiy"
android:label="#string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".NextActivity"
android:screenOrientation="portrait">
</activity>
</application>
</manifest>
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I launch a URL from my application on Android?
I can't seem to figure out how to work on this. I need to click a button that will redirect to a URL / Website. Thanks.
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class GoogleActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button english = (Button)findViewById(R.id.google);//button name in xml file
english.setOnClickListener(google); // on button click listener
}
private Button.OnClickListener google
= new Button.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Uri uri = Uri.parse("http://www.google.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
};
}
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.google.com"));
startActivity(intent);
}
});
load_url.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Uri uri = Uri.parse("http://www.google.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
Update:
manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testtone"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="4"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button mButton = (Button) findViewById(R.id.button1);
mButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Uri uri = Uri.parse("http://www.google.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
}
}