[Q] Notiy the user of a variable - Android - Android Studio

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();

Related

[Q] Setting restriction on editText

Hello, I am new to java and having difficulties trying to set a restriction on one of my editText.
I am trying to develop an app that allows user to fill there information in the application and then send through an email client.
So for my phone number section i am trying to make a restriction for only 11 characters to be entered, also for all the fields to be completed to process through the next stage.
here is my coding:
package com.example.rumel.booking2;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
EditText personName, numberGuests, phoneNumber, date, time;
String name, guests, number, dte, tme;
Button sendEmail;
@override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializerVars();
sendEmail.setOnClickListener(this);
}
private void initializerVars() {
personName = (EditText) findViewById(R.id.etName);
numberGuests = (EditText) findViewById(R.id.etGuest);
phoneNumber = (EditText) findViewById(R.id.etPhone);
date = (EditText) findViewById(R.id.etDate);
time = (EditText) findViewById(R.id.etTime);
sendEmail = (Button) findViewById(R.id.button);
}
public void onClick (View v) {
// TODO Auto-generated method stub
convertEditTextVarsIntoStringsAndYesThisIsAMethodWeCreated();
String[] to = new String[]{"[email protected]"};
String message = "Name: "
+ name
+ "\n"
+ " Number of Guests: "
+ guests
+ "\n"
+ " Contact Number: "
+ number
+ "\n"
+ " Date: "
+ dte
+ ", Time: "
+ tme
+ "\n"
+ "\n"
+ "Thank you";
Intent emailIntent = new Intent (Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, to);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Reservation");
emailIntent.putExtra(Intent.EXTRA_TEXT, message);
emailIntent.setType("message/rfc822");//rfc822 email protocol
startActivity(Intent.createChooser(emailIntent,"Email"));
}
private void convertEditTextVarsIntoStringsAndYesThisIsAMethodWeCreated() {
// TODO Auto-generated method stub
name = personName.getText().toString();
guests = numberGuests.getText().toString();
number = phoneNumber.getText().toString();
dte = date.getText().toString();
tme = time.getText().toString();
}
@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
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
@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);
}
}
Would appreciate any help, thank you

Calculation from user input

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.

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?

Categories

Resources