java physics app in android ( maths ) - java

Basically, I'm new to java and I have to make an android application that can do simple physics equations such as I=Q*t.... etc etc, basically I cannot for the life of me get the result to output, has anyone got any idea's why this wont work on the emulator?
I have tried putting intents and all sorts in there. Help please
public class Current extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_current);
// Show the Up button in the action bar.
setupActionBar();
#SuppressWarnings("unused")
Intent intent = getIntent();
}
public void Main(String[]args){
Button calc1 = (Button)findViewById(string.Calculate_Current);
calc1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// IIIIII HATE JAVA
EditText Charge1 = (EditText)findViewById(R.id.number_input_2);
EditText Time1 = (EditText)findViewById(R.id.number_input_3);
TextView Distances_answer = (TextView)findViewById(R.id.Distances_answer);
double charge = Double.parseDouble(Charge1.getText().toString());
double Time = Double.parseDouble(Time1.getText().toString());
#SuppressWarnings("unused")
Intent intent = new Intent();
Distances_answer.setText("" + charge + Time);
}
});

I feel like you are taking input of charge and time from two edittexts and then on click of a button you are updating a textview to place that product of charge and time there.Try this out
public class Current extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_name);
// Show the Up button in the action bar.
setupActionBar();
Button calc1 = (Button)findViewById(R.id.buttons_id_in_layout);
calc1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText Charge1 = (EditText)findViewById(R.id.number_input_2);
EditText Time1 = (EditText)findViewById(R.id.number_input_3);
TextView Distances_answer = (TextView)findViewById(R.id.distances_answer);
double charge = Double.parseDouble(Charge1.getText().toString());
double time = Double.parseDouble(Time1.getText().toString());
//Time is a class in Java
Distances_answer.setText("" +charge*time);
}
});
}

Related

Java Android String Intent displaying null when moving to previous screen

So I have a screen where a user enters their name, on button click they are redirected to a menu where it displays their name.
In the menu screen I have a button that takes me to another screen (About Me), on that screen I have a button with an Intent to go back to the Menu Activity.
The issue is that when I click the button with the Intent to go back, the name displays as null instead of what the User entered.
This does not happen when I use the actual android navigation buttons to go to the previous screen, only the Button i created to go back to the Menu Activity.
Launcher Activity where user enters their name
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher_screen);
Button launcherNextBtn = (Button) findViewById(R.id.launcherNextBtn);
EditText launcherVisitorNameEditText = (EditText) findViewById(R.id.launcherVisitorNameEditText);
TextView launcherVisitorNameErrorTextView = (TextView) findViewById(R.id.launcherVisitorNameErrorTextView);
launcherVisitorNameErrorTextView.setText("");
launcherNextBtn.setOnClickListener((View v) -> {
String visitorName = launcherVisitorNameEditText.getText().toString();
if (visitorName.equals("")) {
launcherVisitorNameErrorTextView.setText("Please enter a value");
} else {
launcherVisitorNameErrorTextView.setText("");
Intent goToMenuActivity = new Intent(LauncherScreen.this, MenuScreen.class);
goToMenuActivity.putExtra("visitorName", visitorName);
startActivity(goToMenuActivity);
}
});
Menu activity where it displays the users name
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_screen);
Button menuHomeBtn = (Button) findViewById(R.id.menuHomeBtn);
Button menuAboutMeBtn = (Button) findViewById(R.id.menuAboutMeBtn);
TextView menuNameTextView = (TextView) findViewById(R.id.menuNameTextView);
String visitorName = getIntent().getStringExtra("visitorName");
menuNameTextView.setText("Dear " + visitorName);
menuHomeBtn.setOnClickListener((View v) -> {
Intent goToLauncherActivity = new Intent(MenuScreen.this, LauncherScreen.class);
goToLauncherActivity.putExtra("visitorName", visitorName);
startActivity(goToLauncherActivity);
});
menuAboutMeBtn.setOnClickListener((View v) -> {
Intent goToAboutMeActivity = new Intent(MenuScreen.this, AboutMeScreen.class);
startActivity(goToAboutMeActivity);
});
}
About me activity, when I click the button to back to Menu, i get "Dear null"
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about_me_screen);
Button aboutMeBackBtn = (Button) findViewById(R.id.aboutMeBackBtn);
String visitorName = getIntent().getStringExtra("visitorName");
aboutMeBackBtn.setOnClickListener((View v) -> {
Intent goToMenuActivity = new Intent(getApplicationContext(), MenuScreen.class);
startActivity(goToMenuActivity);
});
}
Your problem is in About me activity screen
aboutMeBackBtn.setOnClickListener((View v) -> {
Intent goToMenuActivity = new Intent(getApplicationContext(), MenuScreen.class);
startActivity(goToMenuActivity);
});
This time you are not passing an extra field, so you are getting null
Answer 1
aboutMeBackBtn.setOnClickListener((View v) -> {
onBackPressed();
});
Answer 2
aboutMeBackBtn.setOnClickListener((View v) -> {
Intent goToMenuActivity = new Intent(getApplicationContext(), MenuScreen.class);
goToMenuActivity.putExtra("visitorName", visitorName)
startActivity(goToMenuActivity);
});

Saving Activity State when switching activities

I'm currently trying to create an activity, which should be creating a new TextView on my main activity, everytime a Button is clicked on the first activity. However, instead of creating a new TextView everytime the button is clicked, it just changes the values of the first created TextView, so that there is always only one TextView. Is there a way to make it so that my first activity will not only create one single textview?
Here's the code from my "NewSubjectActivity":
**public class NewSubjectActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_subject);
Button SaveBtn = findViewById(R.id.SaveBtn);
nsaSaveBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Click();
}
});
}
protected void Click(){
Intent intent = new Intent(NewSubjectActivity.this, MainActivity.class);
Boolean createnewTextView = true;
intent.putExtra("createnewTextView",createnewTextView);
startActivity(intent);
}
}
And here's the (relevant) code from my MainActivity:
protected void ReceiveValue (){
//getting Extras
Intent nsareceivedvalues = getIntent();
boolean createTextView = false;
createTextView = nsareceivedvalues.getExtras().getBoolean("createnewTextView");
//declaring fixed Views
final LinearLayout mainLinearLayout = (LinearLayout)findViewById(R.id.mainLinearLayout);
//Params for TextView
RelativeLayout.LayoutParams Params = new RelativeLayout.LayoutParams(1000, 200);
Params.setMargins(0, 10, 0, 10);
while(createTextView) {
//creating a TextView
TextView newsubject = new TextView(MainActivity.this);
//applying values to the TextView
newsubject.setLayoutParams(Params);
newsubject.setGravity(CENTER);
newsubject.setBackgroundColor(Color.GRAY);
mainLinearLayout.addView(newsubject);
createTextView = false;
}
}
As I said, this only create one text view, everytime I press the button on my "NewSubjectActivity" I think this might be, because the previous text view is not saved and the MainActivity is reset everytime I switch between the activities.
Every help and advise is much appreciated <3
Maybe when you are going back to your main activity you should call to onBackPressed() function, so it will reset nothing at all. Here's the example in kotlin
val back_button:Button = findViewById(R.id.button2)
back_button.setOnClickListener { v -> run {
//Change activity
onBackPressed()
} }
in Java should be:
Button back_button = findViewById(R.id.button2);
back_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Change activity
onBackPressed()
}
});
Let's hope it works!

Sending double from one activity to another Android Studio

Hi,
I am trying to send data from one activity to another activity using this method: how to retrieve a Double value from one activity to another?
Yet every time I open up my application it crashes as its because my code is in the onCreate:
double lat, longi;
Intent getLocation;
Bundle extras;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getLocation = this.getIntent();
extras = getLocation.getExtras();
lat = extras.getDouble("lat");
longi = extras.getDouble("longi");
but when I put it in a button instead It can't resolve this.getIntent();
public void getCoordinates(View view){
Button coordinates = (Button) findViewById(R.id.getCoordinates);
coordinates.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
getLocation = this.getIntent();
extras = getLocation.getExtras();
lat = extras.getDouble("lat");
longi = extras.getDouble("longi");
}
});
}
I would like to receive data either automatically or using a button.
I am quite new in mobile computing so please don't roast me.
this meant isView.OnClickListener() in your code .
And we need YourActivity.this as Context
You should use
public void getCoordinates(View view){
Button coordinates = (Button) findViewById(R.id.getCoordinates);
coordinates.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
getLocation = YourActivity.this.getIntent();
extras = getLocation.getExtras();
lat = extras.getDouble("lat");
longi = extras.getDouble("longi");
}
});
}

How to get last outgoing call in Android

I'm working with an app that uses Android Intent class to make calls.
I can successfully create a call to a number so that is working.
What I want now is to show the last outgoing call number to show in a TextView.
Also I made that TextView to be clickable so by a click I can redial the number.
I'm using CallLog.Calls.getLastOutgoingCall(getApplicationContext()); to get the last called number.
This works only once in my application.
I start the application, enter a number and it makes a call. The first outgoing called number I set in the TextView. After that I enter a second number which is successfully set in the TextView but when I click to redial the app calls the first number!
Why is that?
My last outgoing number is the second.
Why is it calling the first number?
Also if I restart the app then it redials the second outgoing number.
Here is my code:
public class MainActivity extends ActionBarActivity {
Button btnCall;
TextView number;
EditText calledNumber;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnCall = (Button) findViewById(R.id.button);
number = (TextView) findViewById(R.id.textView2);
calledNumber = (EditText) findViewById(R.id.editText);
//Gets the last outgoing number from the call log
final String lastCalledNumber = CallLog.Calls.getLastOutgoingCall(getApplicationContext());
btnCall.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String TheNumber = calledNumber.getText().toString();
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + TheNumber));
startActivity(callIntent);
number.setText(TheNumber);
}
});
//redial number in TextView by click
number.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent call = new Intent(Intent.ACTION_CALL);
call.setData(Uri.parse("tel:" + lastCalledNumber));
startActivity(call);
}
});
}
}
Per the activity lifecycle, onCreate() is only called once and does not run each time your app appears - this would mean that your getLastOutgoingCall() would be correct the first time but wouldn't necessarily work the second time.
If you'd like to run something every time the activity appears, you should move it to onResume() - this ensures it will always be up to date:
String lastCalledNumber;
#Override
protected void onCreate(Bundle savedInstanceState) {
// Same except without the `getLastOutgoingCall()`
}
#Override
public void onResume() {
lastCalledNumber = CallLog.Calls.getLastOutgoingCall(this);
}

Android app quitting prematurely

I am new to android development using JAVA, and I'm having an issue with a simple app I created. (Please don't laugh at it!)
All it's supposed to do is display a number in an editable textview; The three buttons on this main activity are a +, -, and reset. Super simple, right? I can't tell what I did wrong, but everytime I run the app to test, and then click on any of the buttons, it exits the app and goes back to the android home screen. Not sure what I did wrong... but here's the code I have so far:
public class Main extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn_Plus;
Button btn_Minus;
Button btn_Reset;
final EditText sCount= (EditText) findViewById(R.id.txtCount);;
btn_Plus=(Button)findViewById(R.id.btnPlus);
btn_Minus=(Button)findViewById(R.id.btnMinus);
btn_Reset=(Button)findViewById(R.id.btnReset);
//One way I tried to work it
btn_Plus.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//gets the text in the textview, makes it a string,
//converts it to an int, calculates, then puts it
//back.
String iCounter = sCount.getText().toString();
int iCount = Integer.parseInt(iCounter);
iCount += 1;
Integer.toString(iCount);
sCount.setText(iCount);
}
});
//The second way I tried -- neither way works.
bM.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int iCount = Integer.parseInt(sCount.getText().toString());
iCount -= 1;
if(iCount >0)
iCount = 0;
Integer.toString(iCount);
sCount.setText(iCount);
}
});
bR.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int iCount = 0;
Integer.toString(iCount);
sCount.setText(iCount);
}
});
}
}
Thanks so much!
Use a TextView instead, unless you plan to allow the user to directly enter the number (which it seems like you don't). Rather than do text manipulation to get the number, why don't you just store the number as a member variable, update that when a button is pressed, and change the text accordingly? This is how I would write it:
public class Main extends Activity {
int count = 0;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.txtCount);
findViewById(R.id.btnPlus).setOnClickListener(this);
findViewById(R.id.btnMinus).setOnClickListener(this);
findViewById(R.id.btnReset).setOnClickListener(this);
}
private void updateText() {
String text = Integer.toString(count);
textView.setText(text);
}
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.btnPlus:
count++;
break;
case R.id.btnMinus:
count--;
break;
case R.id.btnReset:
count = 0;
break;
}
updateText();
}
}

Categories

Resources