I want, that when you click to open a new screen (Activity), but have a problem, the new screen does not open. What changes are made to the code, so that the new window opened?
package com.android.yalt;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
Button btnny;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnny = (Button) findViewById(R.id.ny);
btnny.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.ny:
startActivity(NY.class);
break;
default:
break;
}
}
private void startActivity(Class<NY> class2) {
// TODO Auto-generated method stub
}
}
You have to use
startActivity(new Intent(getBaseContext(), NY.class));
or
Intent i = new Intent(getBaseContext(), NY.class);
startActivity(i);
or In a method format:
public void startMyActivity(Class activityToStart){
startActivity(new Intent(getBaseContext(), activityToStart.class));
}
Just as a sidenote, one can replace getBaseContext() with your own context if one wants to e.g. startActivity(new Intent(yourOnGoingActivityGoesHere.this, yourClassGoesHere.class));
Also as a utility:
/*This is not tested but should work */
public class AndroidUtility(){
/**
*Usage: AndroidUtility.startMyActivity(MainActivity.this, NY.class);
*/
public static void startMyActivity(Activity onGoingActivity, Class activityToStart){
onGoingActivity.startActivity(new Intent(onGoingActivity.getBaseContext(), activityToStart.class));
}
}
In your case the easiest would be:
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.ny:
startActivity(new Intent(getBaseContext(), NY.class));
break;
default:
break;
}
}
Related
I am not able to use the startActivity() function in my class and now I am stuck. But I have to open a new activity from this class. Please help me...
Here is my class and I want to open a new activity by using the onClick event in this class which is defined below
package com.fva_001.flashvsarrow.com.fva_001;
import android.app.Activity;
import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.*;
import android.view.View;
import android.view.Window;
import android.widget.Button;
public class MapFoundDialog extends Dialog implements android.view.View.OnClickListener {
public Activity c;
public Dialog d;
public Button yes;
public MapFoundDialog(Activity a) {
super(a);
// TODO Auto-generated constructor stub
this.c = a;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
setContentView(R.layout.dialog_map_found);
yes = (Button) findViewById(R.id.btn_hide_dialog_map_found);
yes.setOnClickListener(this);
}
#Override
public void onClick(View v) {
new ButtonClick(getContext(), v);
switch (v.getId()) {
case R.id.btn_map_open:
// i want to open a new activity from here....
break;
case R.id.btn_hide_dialog_map_found:
dismiss();
break;
default:
break;
}
dismiss();
}
}
First of all, I would not suggest passing the Activity to the dialog, since it may cause Activity Leak, and that's serious performance issue!
Why u pass activity to the dialog? You can pass ApplicationContext.
Use this code:
public class MapFoundDialog extends Dialog implements android.view.View.OnClickListener {
public Context c;
public Dialog d;
public Button yes;
public MapFoundDialog(Context c) {
super(c);
// TODO Auto-generated constructor stub
this.c = c.getApplicationContext();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
setContentView(R.layout.dialog_map_found);
yes = (Button) findViewById(R.id.btn_hide_dialog_map_found);
yes.setOnClickListener(this);
}
#Override
public void onClick(View v) {
new ButtonClick(c, v);
switch (v.getId()) {
case R.id.btn_map_open:
// i want to open a new activity from here....
Intent intent = new Intent(c, NewActivity.class);
c.startActivity(intent);
break;
case R.id.btn_hide_dialog_map_found:
dismiss();
break;
default:
break;
}
dismiss();
}
}
I am very new to android programming. I want to use a code that takes me to MainActivity from my current activity on a click of a Button.
Here is my current code:
package com.example.flashlightapp;
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 Whitelight extends Activity implements OnClickListener {
Button b1 = (Button) findViewById(R.id.b3);
Intent i = new Intent(this, MainActivity.class);
{
this.startActivity(i);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_whitelight);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
What should I put in
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
At first you must check if you declared all your activities in the manifest.xml file.
and in your Java code try this:
package com.example.flashlightapp;
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 Whitelight extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_whitelight);
Button b1 = (Button) findViewById(R.id.b3);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(Whitelight.this, MainActivity.class);
this.startActivity(i);
}
});
}
}
This tutorial explains how to use intents and listeners : http://goo.gl/phLWkx
Try this..
public class Whitelight extends Activity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_whitelight);
Button b1 = (Button) findViewById(R.id.b3); // Initialization of Button
b1.setOnClickListener(this); // Initialization of ClickListener to the Button
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
}
}
Follow the links
http://developer.android.com/index.html
http://developer.android.com/training/index.html
http://www.mkyong.com/tutorials/android-tutorial/
Use following :
Button b1;
public class Whitelight extends Activity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_whitelight);
b1 = (Button) findViewById(R.id.b3); // Initialization of Button
b1.setOnClickListener(this); // Initialization of ClickListener to the Button
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v==b1){
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
}
}
}
in onClick(...) :
you can choose if your button is press then perform specific activity, in your case if you press b1 button then perform button specific activity :
so we can check view v==b1 button. if you want to more button then
if(v==b1){
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
}
else if(v==b2)
{
// perform another action ;
}
My scale/zoom animation won't start with the finish(). I hope there's a way to do it without deleting the said statement because it is necessary not to go back to the current activity.
Here's the code:
package com.capstone.mainmobidyx.filipino;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import com.capstone.mainmobidyx.R;
public class F_FilipinoYunit1 extends Activity implements OnClickListener {
Button btnLesson1, btnLesson2, btnLesson3;
Intent lesson1, lesson2, lesson3;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.f_yunit1);
btnLesson1 = (Button) findViewById(R.id.btnY1Lesson1);
btnLesson1.setOnClickListener(this);
btnLesson2 = (Button) findViewById(R.id.btnY1Lesson2);
btnLesson2.setOnClickListener(this);
btnLesson3 = (Button) findViewById(R.id.btnY1Lesson3);
btnLesson3.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
final Animation zoomAnim = AnimationUtils.loadAnimation(this,R.anim.zoom_in);
v.startAnimation(zoomAnim);
switch (v.getId()) {
case R.id.btnY1Lesson1:
lesson1 = new Intent(v.getContext(), SX_Lesson01.class);
startActivity(lesson1);
finish();
break;
case R.id.btnY1Lesson2:
lesson2 = new Intent(v.getContext(),
SX_ScienceLesson02Menu.class);
startActivity(lesson2);
finish();
break;
case R.id.btnY1Lesson3:
lesson2 = new Intent(v.getContext(),
SX_Lesson03Menu.class);
startActivity(lesson2);
finish();
break;
}
}
}
SOLUTION:
Using the method onAnimation end. I can now start the animation before finishing the activity by inserting the codes inside the method.
here is the working code:
#Override
public void onClick(final View v) {
// TODO Auto-generated method stub
final Animation zoomAnim = AnimationUtils.loadAnimation(this,R.anim.zoom_in);
zoomAnim.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(Animation anim) {
switch (v.getId()) {
case R.id.btnY1Lesson1:
lesson1 = new Intent(v.getContext(), SX_Lesson01.class);
startActivity(lesson1);
finish();
break;
case R.id.btnY1Lesson2:
lesson2 = new Intent(v.getContext(),
SX_ScienceLesson02Menu.class);
startActivity(lesson2);
finish();
break;
case R.id.btnY1Lesson3:
lesson2 = new Intent(v.getContext(),
SX_Lesson03Menu.class);
startActivity(lesson2);
finish();
break;
}
}
}
public void onAnimationRepeat(Animation arg0) {}
public void onAnimationStart(Animation arg0) {}
});
v.startAnimation(zoomAnim);
Set an animation end listener on the animation and finish() / startActivity() when the animation has ended :
http://developer.android.com/reference/android/view/animation/Animation.AnimationListener.html
example :
final Animation animation = AnimationUtils.loadAnimation(this,R.anim.zoom_in);
animation.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(Animation anim) {
// Start your new activity and finish() the current activity here!
}
public void onAnimationRepeat(Animation arg0) {}
public void onAnimationStart(Animation arg0) {}
}
animation.startAnimation()
Pease help me with my code.
When i click any button, button1, button2 or button3 opens new activity, but layout is empty, without any text's and others.
Code of activity from calling the new activity:
package com.novator.inweld;
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 TentsCatalog extends Activity implements OnClickListener
{
private Button button1;
private Button button2;
private Button button3;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.tents_catalog);
button1 = (Button)findViewById(R.id.button1);
button2 = (Button)findViewById(R.id.button2);
button3 = (Button)findViewById(R.id.button3);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
}
#Override
public void onClick(View view)
{
if(view == button1)
{
Intent intent = new Intent(this, TentPage.class);
intent.putExtra("tentId", "1");
startActivity(intent);
}
if(view == button2)
{
Intent intent = new Intent(this, TentPage.class);
intent.putExtra("tentId", "2");
startActivity(intent);
}
if(view == button3)
{
Intent intent = new Intent(this, TentPage.class);
intent.putExtra("tentId", "3");
startActivity(intent);
}
}
}
Code of my new activity:
package com.novator.inweld;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class TentPage extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent intent= getIntent();
String tentId = intent.getStringExtra("tentId");
if(tentId == "1")
{
setContentView(R.layout.tent1);
}
if(tentId == "2")
{
setContentView(R.layout.tent2);
}
if(tentId == "3")
{
setContentView(R.layout.tent3);
}
}
}
use equals(), not ==
if(tentId.equals("1"))
change onClick method with:
#Override
public void onClick(View v)
{
Intent intent = new Intent(this, TentPage.class);
switch (v.getId()) {
case R.id.button1:
intent.putExtra("tentId", "1");
startActivity(intent);
break;
case R.id.button2:
intent.putExtra("tentId", "2");
startActivity(intent);
break;
case R.id.button3:
intent.putExtra("tentId", "3");
startActivity(intent);
break;
default:
break;
}
and use tentId.equals(""); for String check you must use equals() method and for number value use == like:
if(tentId.equals("1"))
{
setContentView(R.layout.tent1);
}
if(tentId.equals("2"))
{
setContentView(R.layout.tent2);
}
if(tentId.equals("3"))
{
setContentView(R.layout.tent3);
}
Java is a b*%$#ch... Use equals():
if(tentId.equals("1")) ...
Also, according to this answer, Android supports Java 1.7's strings in switch statements, meaning you can rewrite your code in a tidier fashion:
switch(tendId) {
case "1": ...
case "2": ...
case "3": ...
}
Keep in mind that the simplest solution would be to pass tendId as an int and not a string:
intent.putExtra("tendId", 1);
int tendId = intent.getIntExtra("tendId");
Replace your TentPage like:
package com.novator.inweld;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class TentPage extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent intent= getIntent();
String tentId = intent.getStringExtra("tentId");
if(tentId.equals("1"))
{
setContentView(R.layout.tent1);
}
if(tentId.equals("2"))
{
setContentView(R.layout.tent2);
}
if(tentId.equals("3"))
{
setContentView(R.layout.tent3);
}
}
}
MY program is note working on the emulator. There are no errors in the code from eclipse. It says that "the process has stopped unexpectedly". I have tried to run the xml code by it self and it comes up without a problem. I also have a db file of the same activity however I am more suspicious of this file being the cause of the problem. I cannot find any thing to help find the answer. I do not know much about programming but I have been researching this application. The solution to this would help many people who might have this problem or are interested in this activity.
I am interested in inserting a fixed text from a button into a database.
import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class Watchnotes extends Activity implements OnClickListener,
android.view.View.OnClickListener {
Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_);
button1.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity, menu);
return true;
}
// public interface
public void onClick(View arg0) {
boolean didItWork = true;
try{
switch (arg0.getId()) {
case R.id.button1:
String note = "XXXX";
NAME entry = new NAMEdb(NAME.this);
entry.open();
entry.createEntry(note);
entry.close();}
}catch (Exception e){
didItWork = false;
}finally{
if (didItWork){
Dialog d = new Dialog(this);
d.setTitle("XXXX");
d.show();}
}
}
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
switch (((View) arg0).getId()){
case R.id.button5:
Intent i = new Intent("com.intent.LIST");
startActivity(i);}}}
we have the same problem. Just initialize
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main3);
addListenerOnButton1();
addListenerOnButton2();
}
public void addListenerOnButton1() {
final Context context = this;
button = (Button) findViewById(R.id.btn1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, App2Activity.class);
startActivity(intent);
}
});