Start New Activity with A button in android [duplicate] - android

This question already has answers here:
Start Activity with Button Android
(5 answers)
Closed 8 years ago.
i am new in android development, i would like to start a new activity when i click a button, but i try so long, and dint work, please any 1 help me.
Here is the Main Project.
package com.example.pdf;
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
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout1);
Button tut1 = (Button) findViewById(R.id.button2);
tut1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("com.example.pdf.main1"));
}
});
}
}
Here is my Manifest
<activity
android:name="com.example.pdf.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.pdf.main1"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN1" />
<category android:name="android.intent.category.Default" />
</intent-filter>
</activity>
hi ,thx for all help, i already know the problem, And fix, thx for all help

Please try this
Intent I=new Intent(MainActivity.this,main1.class);
startActivity(I);

Related

Button doesn't go to new page?

I created a button and I want it to go to another page when clicked on. I think I did everything right but when I ran the project in the emulator the button didn't do anything and it gives me the error that the project has stopped working and the app stops working on the emulator. And ideas?
Here is the xml for the button
<Button
android:id="#+id/button1"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textStyle="bold"
android:hint="#string/Play_Button" />
Here is the java for the button
package com.dakota.amnesiadino;
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
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button play_button = (Button) findViewById(R.id.button1);
play_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("com.dakota.amnesiadino.play_button"));
}
});
}
}
and here is the manifest for the button
<activity
android:name="com.dakota.amnesiadino.levels_home"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.play_button" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
First of all, be more specific when you say "did not go to the next page"
This page is a screen?? Is an activity defined by an xml file? Is a web url??
It is not very clearly typed your question.
As I understand,
probably, you should write something like this inside the OnClick body..
Intent intent = new Intent(getApplicationContext(),levels_home.class);
startActivity(intent);
This solution could work if you have an xml file and a corresponding .java file for the new activity.
for the best way of doing it is like this :
Intent intent = new Intent(getApplicationContext(),levels_home.class);
startActivity(intent);
and setting the manifest:
<activity
android:name="com.dakota.amnesiadino.levels_home"
android:label="#string/app_name" >
in your manifest the issue with LAUNCHER probably copy and paste error it should be DEFAULT:
<intent-filter>
<action android:name="android.intent.action.play_button" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>

Android Applications Linking Activity

I have two different activities in my android applications. The first(MainActivity) has ImageButton through which onClick it's getting navigate two second activity(Numbers). This is my code of MainActivity
package com.android.learning_numbers;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.widget.ImageButton;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
ImageButton imageButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
imageButton = (ImageButton) findViewById(R.id.button1);
imageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(MainActivity.this, Numbers.class);
startActivity(intent);
}
});
}
}
Now here there is no error in error log but when application is getting launched on click of imagebutton it is getting crashed. & showing error in LogCat. What i do now. Please help..
It would seem that you haven't declared the Numbers Activity in the Android Manifest.
If an Activity is not declared in the Manifest, as far as the application is concerned, the Activity does not exist which causes the application to crash.
<application>
<activity
android:name="com.android.learning_numbers.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.android.learning_numbers.Numbers"/>
</application>
Try this.. in your manifest before </application>
<activity
android:name="com.android.learning_numbers.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Numbers" />
<activity
android:name="com.android.learning_numbers.Numbers"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.NoTitleBar"
android:windowSoftInputMode="adjustResize" >

Onclick button beginer's android app isn't running:

I am new here,posting my first question.So this android project about showing a layout onclick of button,isn't running.When I run it,the emulator opens but my project isn't visible on it. I am trying to show another layout when button is clicked.Can anyone help me out?
My layouts are :activity_main,tutorial1 (layout to be showed after button:tutorial1 is clicked),splash(which is showed for 5 seconds on opening).
Also manja is name of sound that is played on opening,for 5 seconds.
Here's my code:
MainActivity.java:
package com.example.thebasics;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity extends Activity {
MediaPlayer logoMusic;
#Override
protected void onCreate(Bundle AmanIsAwesome) {
super.onCreate(AmanIsAwesome);
setContentView(R.layout.splash);
logoMusic = MediaPlayer.create(MainActivity.this, R.raw.manja);
logoMusic.start();
Thread logoTimer= new Thread (){
public void run (){
try{
sleep(5000);
Intent menuIntent=new Intent("com.example.thebasics.MENU");
startActivity(menuIntent);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
finish();
}
}
};
logoTimer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
logoMusic.release();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Menu.java:
package com.example.thebasics;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class menu extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button tut1 = (Button) findViewById(R.id.tutorial1);
tut1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent ("com.example.thebasics.TUTORIAL"));
}
});
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
Tutorial.java:
package com.example.thebasics;
import android.app.Activity;
import android.os.Bundle;
public class Tutorial extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tutorial1);
}
}
}
The basics androidmanifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.thebasics"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/download"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.thebasics.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAINACTIVITY" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.thebasics.MENU"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.thebasics.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.thebasics.TUTORIAL"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.thebasics.TUTORIAL" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Also the console is showing the error:- Emulator[ Warning: No DNS servers found]
whats this?
I have uploaded a fully working of your project.Download it from here.Also use Intent as other people mentioned.
Intent intent = new Intent(MainActivity.this or getApplicationContext(), Tutorial.class);
/* First argument is your current activity.You can also mention it i.e CurrentActivity.this */
/* Second argument is the class where you wanna go i.e OtherActivity.class */
startActivity(intent);
Use this instead :
startActivity(new Intent (context, Tutorial.class));
Your way of doing it is strange too :
dont do this :
<activity
android:name="com.example.thebasics.TUTORIAL"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.thebasics.TUTORIAL" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Just do :
<activity
android:name="com.example.thebasics.Tutorial"
android:label="#string/app_name" >
</activity>
Same thing for the other one.
This doesn't exist :
<action android:name="com.example.thebasics.TUTORIAL" />
This line code
new Intent ("com.example.thebasics.TUTORIAL")
Creates intent with action name "com.example.thebasics.TUTORIAL"
What you want is:
startActivity(new Intent (MainActivity.this, Tutorial.class));
Just like Tsunaze suggested.
do as follow
Intent intent = new Intent(context, Tutorial.class);
// set some flags here according to your need
startActivity(intent);
Intent intent = new Intent(context, Menu.class);
// set some flags here according to your need
startActivity(intent);
and remove this from your manifest file as this is wrong
<intent-filter>
<action android:name="com.example.thebasics.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="com.example.thebasics.TUTORIAL" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>

using of intent for linking activities

I want to link my two activites by clicking on a button i have written the following code
public class IHBCAPTUREActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
ImageView iv;
TextView tx;
Button b1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
iv=(ImageView)findViewById(R.id.imageView1);
tx=(TextView)findViewById(R.id.textView1);
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent calIntent;
**calIntent = new Intent(IHBCAPTUREActivity.this, LoginActivity.class);**
startActivity(calIntent);
}
}
error cumes in this line
calIntent = new Intent(IHBCAPTUREActivity.this, LoginActivity.class);
for LoginActivity.class that
LoginActivity cannot be resolved to a type . How to solve it?
here is the simple code how you can load one activity to another,
i have created two activities like this
FirstActivity.java
package com.rdc;
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 implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
Button btnload = (Button) findViewById(R.id.btn);
btnload.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(this, SecondActivity.class);
startActivity(i);
}
}
SecondActivity.java
package com.rdc;
import android.app.Activity;
import android.os.Bundle;
public class SecondActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}
}
and my manifest file is
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rdc"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<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=".SecondActivity"
android:label="#string/app_name">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
try to implement this in your app and let me know if still any doubt is there.
Check whether you have declared LoginActivity in manifest file as:
<activity
android:name="your.package-name.LoginActivity" >
</activity>
You don't have anything wrong in code.
Its know issue that occurs randomly.
solution is to set the target to other API level in project->properties->android, then set it back.then clean you project once.
I think this will refresh the .classpath or some other files, not sure, but it works.

Stopping an Activity in Android

I had read about this thread around the Internet but unlucky to find a solution. The solutions that are available did not work for me like adding the android:noHistory="true" in the Manifest file. What I wanted is that after the user clicks the play game button, which will redirect the user into the select difficulty page, when the user clicks the back button (android emulator), it will go back to the main menu.
This is my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kfc"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".NewKFCActivity"
android:label="#string/app_name"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".HelpPageOne"
android:label="#string/app_name"
android:noHistory="true">
<intent-filter>
<action android:name="com.kfc.HELPPAGEONE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".HelpPageTwo"
android:label="#string/app_name"
android:noHistory="true">
<intent-filter>
<action android:name="com.kfc.HELPPAGETWO" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".SelectDifficulty"
android:label="#string/app_name"
android:noHistory="true">
<intent-filter>
<action android:name="com.kfc.SELECTDIFFICULTY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
This is my main activity:
package com.kfc;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
public class NewKFCActivity extends Activity {
/** Called when the activity is first created. */
ImageButton bPlay, bHelp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
bPlay = (ImageButton) findViewById(R.id.playGame);
bPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(v.getContext(), SelectDifficulty.class);
startActivityForResult(intent,0);
finish();
}
});
bHelp = (ImageButton) findViewById(R.id.Help);
bHelp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(v.getContext(), HelpPageOne.class);
startActivityForResult(intent,0);
finish();
}
});
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
This is my difficulty class:
package com.kfc;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
public class SelectDifficulty extends Activity{
ImageButton bEasy, bMedium, bHard;
#Override
protected void onCreate(Bundle kfcState) {
// TODO Auto-generated method stub
super.onCreate(kfcState);
setContentView(R.layout.difficultyxml);
bEasy = (ImageButton) findViewById(R.id.easy);
bEasy.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Intent intent = new Intent(v.getContext(), NewKFCActivity.class);
//startActivityForResult(intent,0);
}
});
}
}
First of all, you should know the difference between startActivity() and startActivityForResult(), see Android developers website.
Second, I think you should need to understand the lifecycle of an Activity, you don't need to finish() every activity once you leave it, Android will manage that for you.
Hope it helps :)
Cheers!
Remove the lines:
finish();
after
startActivityForResult(...);
in your Activity NewKFCActivity because finish() is destroying your activity and when you press back, there is no more an Activity to go back to!

Categories

Resources