Calculation from user input - Android Studio

I work at a casting company & sometimes have to calculate weights. I am building a simple app in android studio to do this & was hoping someone could help as im struggling. I have 3 user inputs set, but I want to take those inputs & run them through a calculation & the have the result displayed at the end. I want to set a button so when I have the 3 imputs & can click go, & the result gets displayed. This is my code at present.
Code:
package com.example.nealu.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onButtonClick(View V) {
EditText e1 = (EditText)findViewById(R.id.editText);
EditText e2 = (EditText)findViewById(R.id.editText2);
EditText e3 = (EditText)findViewById(R.id.editText3);
TextView t1 = (TextView) findViewById(R.id.textView);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I think im going wrong on the edit text bit but I have searched around & tried taking bits out of code for a calculator, but I need to do a little more than 10*5 etc when working a weight of a casting out so need to take the input & run it through a formula.
Any help would be greatly appreciated.
Thanks.

Related

[Q] Notiy the user of a variable - Android

Hi y'all. I have recently been trying to make a calculator app in AS. However, the app stops when I click the calculate button. I also wanted to have it notify the user the result contained in the result variable, but the setContentText doesn't want to work. Code:
Code:
package firstapp.stars.com.firstapp;
import android.os.Bundle;
import android.app.Notification;
import android.app.NotificationManager;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class AddScrn extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
final Button button4;
button4 = (Button) findViewById(R.id.button4);
button4.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TextView textView4 = (TextView) findViewById(R.id.textView4);
EditText editText7 = (EditText) findViewById(R.id.editText7);
EditText editText8 = (EditText) findViewById(R.id.editText8);
String mynum1=editText7.getText().toString();
String mynum2=editText8.getText().toString();
int result = Integer.parseInt(mynum1) + Integer.parseInt(mynum2);
textView4.setText(Integer.toString(result));
textView4.setText(result);
Notification notification = new Notification.Builder(getApplicationContext())
.setContentTitle("Calculator - Calculation result:")
String content = String.valueOf(result);
.setContentText(content);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// hide the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_add_scrn, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Help.
Whats-a-username? said:
Hi y'all. I have recently been trying to make a calculator app in AS. However, the app stops when I click the calculate button. I also wanted to have it notify the user the result contained in the result variable, but the setContentText doesn't want to work. Code:
Code:
package firstapp.stars.com.firstapp;
import android.os.Bundle;
import android.app.Notification;
import android.app.NotificationManager;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class AddScrn extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
final Button button4;
button4 = (Button) findViewById(R.id.button4);
button4.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TextView textView4 = (TextView) findViewById(R.id.textView4);
EditText editText7 = (EditText) findViewById(R.id.editText7);
EditText editText8 = (EditText) findViewById(R.id.editText8);
String mynum1=editText7.getText().toString();
String mynum2=editText8.getText().toString();
int result = Integer.parseInt(mynum1) + Integer.parseInt(mynum2);
textView4.setText(Integer.toString(result));
textView4.setText(result);
Notification notification = new Notification.Builder(getApplicationContext())
.setContentTitle("Calculator - Calculation result:")
String content = String.valueOf(result);
.setContentText(content);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// hide the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_add_scrn, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Help.
Click to expand...
Click to collapse
Regarding the following code:
textView4.setText(Integer.toString(result));
textView4.setText(result);
Why you assign the value twice?
The first assignment is OK, but the second one need to removed.
The result variable is an Integer type, and when you assign Integer to TextView, it will look for a sting resource id (in the strings xml).
If the id is not found, it can throw an exception.
Yes, but the app still crashes when the button is clicked and still doesn't want to notify me.
Still Active Errors
1. In the setContentTitle it says" ; expected", but when ; is placed, it highlights everything.
2. "Cannot resolve method setContentText".
Whats-a-username? said:
1. In the setContentTitle it says" ; expected", but when ; is placed, it highlights everything.
2. "Cannot resolve method setContentText".
Click to expand...
Click to collapse
Your code is incorrect.
Change you code to the following:
String content = String.valueOf(result);
Notification notification = new Notification.Builder(this)
.setContentTitle("Calculator - Calculation result:")
.setContentText(content).build();

Android Studio Error MainActivity class Help

Hey guys.I am getting these errors in MainActivity java file when I create new project.I am a newbie.
package com.gaama.learnit;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Okay guys its okay now.Admin you can remove the post

Pass data from one activity to another

Can you help me. I have developed a app with a quiz. When the quiz is finished a new activity (result) appears showing the score. I want to pass the score to a new activity namely score. Here is my code. What is wrong. The score must be showed in a textview.
Result.java
Code:
package app.mobiledevicesecurity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Result extends Activity
{
private static Button playbtn;
private static Button menubutton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
OnClickPlayButtonListener();
OnClickMenuButtonListener();
TextView textResult = (TextView) findViewById(R.id.textResult);
Bundle b = getIntent().getExtras();
int score = b.getInt("score");
textResult.setText("You scored" + " " + score + " for the quiz.");
Intent i = new Intent(getApplicationContext(), Result.class);
i.putExtra("somevariable",score);
startActivity(i);
}
public void OnClickPlayButtonListener() {
playbtn = (Button) findViewById(R.id.btn);
playbtn.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("app.mobiledevicesecurity.Quiz");
startActivity(intent);
}
}
);
}
public void OnClickMenuButtonListener() {
menubutton = (Button) findViewById(R.id.menubtn);
menubutton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
}
);
}
Scores.java
Code:
package app.mobiledevicesecurity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.content.Intent;
public class Scores extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scores);
Intent intent = getIntent();
String value = intent.getStringExtra("somevariable");
TextView txtScore1 = (TextView) findViewById(R.id.txtScore1);
txtScore1.setText("You scored" + " " + value + " for the quiz.");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_scores, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Try this
In Result.java
i.putExtra("somevariable",score);alter this code like below..
i.putExtra("somevariable", score.toString());
you are passing integer and getting as string..try to pass it as string may help
or simply change this in scores.java
String value = intent.getStringExtra("somevariable")alter this code like below
Int value = intent.getIntExtra("somevariable")
Sent from my SM-N900 using XDA Free mobile app
As noted by taku_coder you are sending and int then you try to extract a String at least be consistent if you want to send an int then extract as an int on your receiving Activity.
Also i noted that the that you are sending the value to Result Activity shouldnt it be the Scores Activity?
Intent i = new Intent(getApplicationContext(), Result.class);
i.putExtra("somevariable",score);
startActivity(i);
Shouldnt it be like this?
Intent i = new Intent(getApplicationContext(), Scores.class);
i.putExtra("somevariable",score);
startActivity(i);
In Result.java
intent.putExtra("KeyName",X.toString());
you are passing as string, you can get it by using
intent.getextra("KeyName")
Intent i = new Intent(getApplicationContext(), Result.class);
i.putExtra("somevariable",score);
startActivity(i);
It seems to be the code above in OnCreate() method of Result make recursive creation of Result class.

Try and catch for a bmi calculator

Hello, I am new to the app making world and a pretty bad coder while we're at it.
my bmi calculator crashes on launch in the emulator.
here is my code:
package com.gilad_inc.myapplication;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText height, weight;
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
makwork();
ConvertDouble();
}
public void makwork()
{
height = (EditText) findViewById(R.id.Height);
weight = (EditText) findViewById(R.id.Weight);
}
public void ConvertDouble()
{
EditText height = this.height;
EditText weight = this.weight;
Double H = Double.parseDouble(height.getText().toString());
Double W = Double.parseDouble(weight.getText().toString());
}
public void buttonOnClick(View v) {
makwork();
ConvertDouble();
float h = Float.valueOf(height.getText().toString());
float w = Float.valueOf(weight.getText().toString());
/***
* Time for math!
* BMI is calculated
* (weigth in kg / (height in meter * height in meter)
* But since we want the user to input in CM, we just
* multiply it with 10 000 to get the correct value.
*/
double BMI = w / (h * h) * 10000;
TextView tvBMI = (TextView) findViewById(R.id.BMI);
tvBMI.setText("" + BMI);
}
@override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
how can I prevent it from crashing?

How to long press on my app based webview

Hi guy this is my first application android, i using android studio and i need the help. i long press in not show context menu to download the image, below I leave the code if someone is kind enough to give me help.
Code:
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity {
WebView webView;
ProgressBar progressBar;
private AudioManager audio;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setSupportZoom(true);
webView.setVerticalScrollBarEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.setHorizontalScrollBarEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
//webView.getSettings().setBuiltInZoomControls(true);
webView.loadUrl("https://alpha.wallhaven.cc/");
//Barra de progreso
progressBar = (ProgressBar) findViewById(R.id.progressBar);
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(0);
progressBar.setVisibility(View.VISIBLE);
MainActivity.this.setProgress(progress * 1000);
progressBar.incrementProgressBy(progress);
if (progress == 100) {
progressBar.setVisibility(View.GONE);
}
}
});
/*Adding shortcut for MainActivity
//on Home screen
Intent shortcutIntent = new Intent(getApplicationContext(),
MainActivity.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "HelloWorldShortcut");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(getApplicationContext(),
R.drawable.ic_launcher));
addIntent.putExtra("duplicate", false);
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);*/
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
return true;
case KeyEvent.KEYCODE_BACK:
if (webView.canGoBack()) {
//if Back key pressed and webview can navigate to previous page
webView.goBack();
// go back to previous page
return true;
} else {
finish();
// finish the activity
}
return super.onKeyDown(keyCode, event);
default:
return false;
}
}
}

Categories

Resources