switching between activities in java eclipse - java

I have an app where there is a temperature converter on the page and once this is used there is a button to switch to a new activity. However when i click on the button to change to the second activity on the emulator nothing happens?
package com.example.assignment2project;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
import android.content.Intent;
import android.widget.Button;
public class MainActivity extends Activity {
private EditText text;
public void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button next = (Button) findViewById(R.id.Button01);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), SecondScreen.class);
startActivityForResult(myIntent, 0);
}
});
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (EditText) findViewById(R.id.editText1);
}
// This method is called at button click because we assigned the name to the
// "OnClick property" of the button
public void onClick(View view) {
switch (view.getId()) {
case R.id.button1:
RadioButton celsiusButton = (RadioButton) findViewById(R.id.radio0);
RadioButton fahrenheitButton = (RadioButton) findViewById(R.id.radio1);
if (text.getText().length() == 0) {
Toast.makeText(this, "Please enter a valid number", Toast.LENGTH_LONG).show();
return;
}
float inputValue = Float.parseFloat(text.getText().toString());
if (celsiusButton.isChecked()) {
text.setText(String.valueOf(convertFahrenheitToCelsius(inputValue)));
celsiusButton.setChecked(false);
fahrenheitButton.setChecked(true);
} else {
text.setText(String.valueOf(convertCelsiusToFahrenheit(inputValue)));
fahrenheitButton.setChecked(false);
celsiusButton.setChecked(true);
}
break;
}
}
// Converts to celsius
private float convertFahrenheitToCelsius(float fahrenheit) {
return ((fahrenheit - 32) * 5 / 9);
}
// Converts to fahrenheit
private float convertCelsiusToFahrenheit(float celsius) {
return ((celsius * 9) / 5) + 32;
}
}

You have an onCreate1 method and an onCreate method. Android just calls the onCreate method, and not onCreate1. You need to rename onCreate1 to onCreate and delete the other onCreate to get it to work.
Edit : And use #Override annotation over the new onCreate

use this code
Button next = (Button) findViewById(R.id.BUTTON_ID);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(getApplicationContext, SecondScreen.class);
startActivity(myIntent);
}
});
put this on your oncreate
You need to add your second activity on your manifest like this
<activity android:name=".SecondScreen" android:label="#string/app_name">
</activity>

adding to previous answers:
you can create button in your xml layout as much as you want, this doesn't have relation with the oncreate method.
all you have to do to use these button in your mainactivity java file is to initialize it like this:
Button name=(Button)findViewById(R.id."button name or id in your xml file");
you can press "Ctrl plus space" in the same time after "R.id." to get you all the objects you have created in the xml file and choose what you want.
no to switch to another activity do this in your onclicklistener.
startActivity(new Intent("yourcurrent activity".this, "the name of activity you want to switch to".class));
which in this case is:
startActivity(new Intent(MainActivity.this,SecondScreen.class));
then go to you AndroidManifest file and type this inside the application part...you can type it at the very end of the application part.
<activity android:name=".the name of your second activity"/>
which in your case is:
<activity android:name=".Secondscreen"/>
you have to do this whenever creating a new activity.
then in your second activity you should tell what xml file you are viewing so you should do this in the second class activity, just like default of the mainacitivty once you have created it.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout."name of your xml file you want to view");
and i suggest you have a look at this tutorial:
http://www.youtube.com/watch?v=q6-4E1JGT_k

Related

Clicking on button opens blank screen

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.

Prevent Starting the launcher activity again if it is already started, just resume it

I am working on android activities. I have one main activity and two other activities, these two activities are launched from the main activity. There are back buttons on each of When any of these two activities are launched , on pressing the back button i want the intent to start the resumed main activity, not to relaunch it on other page.
Below is the code for Main Activity
package com.example.nadeemahmad.smartcalculator;
import android.app.Activity;
import android.app.IntentService;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainActivity extends Activity {
Button show_cam_ctrl,
show_voice_ctrl;
TextView ma_res_txt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Control Buttons
show_cam_ctrl = (Button) findViewById(R.id.show_cam_ctrl);
show_voice_ctrl = (Button) findViewById(R.id.show_voice_ctrl);
show_cam_ctrl.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this,cam_calculator.class);
startActivity(i);
}
});
show_voice_ctrl.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this,voice_calculator.class);
startActivity(i);
}
});
}
Codes for the two activities
public class voice_calculator extends Activity {
Button back_frm_voice;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_voice_calculator);
back_frm_voice = (Button) findViewById(R.id.back_frm_voice);
back_frm_voice.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(voice_calculator.this,MainActivity.class);
startActivity(i);
finish();
}
});
}
}
public class cam_calculator extends Activity {
Fragment cam_fragment;
Button back_frm_came;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cam_calculator);
back_frm_came = (Button) findViewById(R.id.back_frm_came);
back_frm_came.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(cam_calculator.this,MainActivity.class);
startActivity(i);
finish();
}
});
}
}
This is the main activity, having two buttons on top BTN1 and BTN2
The second activity is launched on the BTN1 press, but when i press the back button on the top
This mainactivity is launched, but not the resumed one, when i press the back button on my phone then it get close and the main activity with calculations on screen appears, what i want is , when i press the back button, so the intent should take me to the main activity with resumed calculations.
Just call cam_calculator.this.finish() or voice_calculator.this.finish() from the OnClickListener. Those activities will finish and "automatically" return to the MainActivity.
edit:
If you put a code like this: startActivity(new Intent(this, SomeActivity.class)); you'll directly telling the framework to start an activity.
Just remove that!
Please use the below edited code,
public class voice_calculator extends Activity {
Button back_frm_voice;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_voice_calculator);
back_frm_voice = (Button) findViewById(R.id.back_frm_voice);
back_frm_voice.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Intent i = new Intent(voice_calculator.this,MainActivity.class);
//startActivity(i);
finish();
}
});
}
}
public class cam_calculator extends Activity {
Fragment cam_fragment;
Button back_frm_came;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cam_calculator);
back_frm_came = (Button) findViewById(R.id.back_frm_came);
back_frm_came.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Intent i = new Intent(cam_calculator.this,MainActivity.class);
//startActivity(i);
finish();
}
});
}
}
The MainActivity will be in the stack, so when you do finish() in your second activity, the MainActivity will be popped and onResume() will be called instead onCreate()
From what I understand, you're launching two children activities from a parent activity and when you press back, the parent activity is restarting. It's because the Android call stack doesn't know that the parent activity is a parent activity for the two children activities. In your Android Manifest, specify parentActivity for the two child activities like this -
android:parentActivityName="com.example.android.ParentActivity"
Let me know if it still doesn't work. Happy Coding!

implementing onclicklistener is not working?

I'm trying to implement onclicklistener but it isn't working on my phone or emulator.
here is the code:
package com.slaps.guess;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
TextView tv;
Button one;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
one = (Button) findViewById(R.id.button1);
tv = (TextView) findViewById(R.id.tvd);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
tv.setText("Anything");
break;
}
}
}
the text view is not changing to anything it is still.
note: button1 exists, and their is nothing wrong with my xml.
i want to implement because i have alot of button.
You are missing one.setOnClickListener(this) in your code.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
one = (Button) findViewById(R.id.button1);
one.setOnClickListener(this);
tv = (TextView) findViewById(R.id.tvd);
}
Before using listener you will have to add it in the object for which you want it.
I guess you want for the Button one here.
Use one.setOnClickListener(this) after one = (Button) findViewById(R.id.button1);
It looks like you're missing the required XML attribute in res/layout/activity_main.xml. Ensure it looks like:
<Button
android:id="#+id/button1"
onClick="onClick"
/>
The key being you have supplied the onClick attribute with the name of your method to invoke.
But if you want to set the click listener programmatically than you will need to use the Button.setOnClickListener method. If you do this your method signature will need to change to:
one.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});

Need an easier way to set up onClickListener for several variables in Android calculator program

I'm programming an Android calculator in Eclipse. I want to be able to set up the OnClickListener for several variables instead of having to code a listener for each one. Seems like overkill. Is there a way to do this using an array, maybe? Much help would be appreciated.
package rechee.cool;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class HelloAndroidActivity extends Activity {
/** Called when the activity is first created. */
int counter=0;
//Just have two buttons so far, I'm going to have like 10 more
Button one;
Button two;
EditText display;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Associate the button variable with the xml reference
one= (Button) findViewById(R.id.bOne);
display= (EditText) findViewById(R.id.editText1);
//When button is clicked, display the text. How do I do this for the rest of my variables?
one.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//String string= Integer.toString(counter);
display.setText("1");
}
});
}
}
The way I'd do this is add the same onClick attribute in XML to every button, like this:
<Button android:onClick="onButtonClick"
... />
and then add the method that you used in that attribute to your activity, differentiating the buttons by id:
public void onButtonClick(View v) {
switch(v.getId()) {
case R.id.button1:
// do something when button 1 is pressed
break;
case R.id.button2:
// do something when button 2 is pressed
break;
// and so on ....
}
}
Alternatively you can use findViewById() to get each button and assign the same listener to all the buttons. Then differentiate by id inside onClick()as shown above. This adds a few useless codelines though, so I think this example here is slightly more clean.
Yes you just need to override the onClick method of the Activity and tell it to implement the OnClickListener:
import com.ewe.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class HelloAndroidActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
int counter=0;
//Just have two buttons so far, I'm going to have like 10 more
Button one;
Button two;
EditText display;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Associate the button variable with the xml reference
one= (Button) findViewById(R.id.bOne);
display= (EditText) findViewById(R.id.editText1);
//When button is clicked, display the text. How do I do this for the rest of my variables?
one.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//String string= Integer.toString(counter);
display.setText("1");
}
});
}
#Override
public void onClick(View v){
switch (case v.getId()){
case R.id.bOne:
//Put code for bOne here
break;
case R.id.editText1:
//Put code for editText1 here
break;
}
}
}

How can i make a sound repeatedly without having to press it multiple times (android)

I have a button in an activity and when i press it the sound plays. The sound itself is 2 seconds long. And it only plays when i press on the button. I wanna make it that a user can hold down the button and the sound plays till he releases the button. How can i do this? Here's my current code.
package android.app;
import android.app.R;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class activity2 extends Activity{
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
//back button that takes u to main.xml
Button next = (Button) findViewById(R.id.Back);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
} );
//Button that plays sound (whippingsound)
Button sound = (Button) findViewById(R.id.sound);
sound.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(activity2.this, R.raw.whippingsound);
mp.start();
}
} );
}
}
Thanks!!!
The solution to your problem is a combination of
Triggering event when Button is pressed down in Android
and
play sound while button is pressed -android.
Namely, you're using an onClickListener instead of onTouchListener.
Try this instead (note: I also moved the media player out so you create it once and use it over and over again).
public class activity2 extends Activity{
MediaPlayer mp = null;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
//back button that takes u to main.xml
Button next = (Button) findViewById(R.id.Back);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
} );
mp = MediaPlayer.create(activity2.this, R.raw.whippingsound);
//Button that plays sound (whippingsound)
Button sound = (Button) findViewById(R.id.sound);
sound.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mp.setLooping(true);
mp.start();
break;
case MotionEvent.ACTION_UP:
mp.pause();
break;
}
return true;
}
});
}

Categories