How to use EditText value (numberDecimal) in math operations Java - java

I have a EditText view :
<EditText
android:id="#+id/vorgabezeit"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:background="#0277BD"
android:imeOptions="actionDone"
android:inputType="numberDecimal"
android:maxLength="5"
android:textAlignment="textEnd"
android:textColor="#ffffff"
android:textCursorDrawable="#null"
android:textStyle="bold"
/>
The value entered in this EditText I would like to use it in an math operation.
Before I had fixed values :
public void submitOrder(View view) {
displayPrice((procente / myEditNum) * mitarbeiter);
}
where variables were defined as :
int procente = 120;
int mitarbeiter = 1;
Now, I searched for hours on the web how to take that value entered in EditText and use id further, and this is as close as I got :
public class MainActivity extends AppCompatActivity {
int procente = 120;
int mitarbeiter = 1;
EditText myEdit = (EditText) findViewById(R.id.vorgabezeit);
String myEditValue = myEdit.getText().toString();
double myEditNum = Double.parseDouble(myEditValue);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void submitOrder(View view) {
displayPrice((procente / myEditNum) * mitarbeiter);
}
}
The application crashes from the start.
Thank you for your patience and help.
Best regards,
Robert

First you need to post the LogCat when you are having a crash.
Second You can not call findViewById until after setContentView is called. You should to initialize your edit text in onCreate.
// Initialize values in onCreate after setContentView
EditText myEdit;
String myEditValue;
double myEditNum;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myEdit = (EditText) findViewById(R.id.vorgabezeit);
myEditValue = myEdit.getText().toString();
myEditNum = Double.parseDouble(myEditValue);
}

Make sure you add android:inputType="number" to your EditText on the xml to ensure the user can ONLY type numbers on it, then :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.button)
.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
displayPrice((procente / Double.parseDouble(((EditText) findViewById(R.id.vorgabezeit))
.getText().toString())) * mitarbeiter);
}
});
}
btw your code crashes because you called findviewById before setting the content view to the activity so basically it searches on a non-existing layout and your edit text and button become null because of it

What about getText(). I added button, since you have to get text after some user action, not right after onCreate event.
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton = (Button)findViewById(R.id.button);
mEdit = (EditText)findViewById(R.id.vorgabezeit);
mButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
Log.v("EditText", mEdit.getText().toString());
}
});
}

Related

Using NumberPicker to change TextView on a different activity [android]

I'm trying to change a textview value on one activity by using a numberpicker on the previous activity. Any help would be appreciated.
Here's the relevant part of my Java from activity1
public class activity_game extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
final NumberPicker fizzNumberPkr = (NumberPicker)findViewById(fizzNumberPicker);
fizzNumberPkr.setValue(3);
fizzNumberPkr.setMinValue(1);
fizzNumberPkr.setMaxValue(20);
fizzNumberPkr.setWrapSelectorWheel(true);
final NumberPicker buzzNumberPkr = (NumberPicker)findViewById(buzzNumberPicker);
buzzNumberPkr.setValue(5);
buzzNumberPkr.setMinValue(1);
buzzNumberPkr.setMaxValue(20);
buzzNumberPkr.setWrapSelectorWheel(true);
}
public void toActivityPlay (View view) {
Intent toActivityPlay = new Intent(this, activity_play.class);
toActivityPlay.putExtra("fizzNumber", fizzNumber);
toActivityPlay.putExtra("buzzNumber", buzzNumber);
startActivity(toActivityPlay);
}
And my relevant java from activity2
public class activity_play extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
TextView fizzNumberText = (TextView)findViewById(R.id.fizzNumber);
fizzNumberText.setText(getIntent().getExtras().getString("fizzNumber"));
if(getIntent().hasExtra("fizzNumber")) {
fizzNumber = getIntent().getIntArrayExtra();
} else {
throw new IllegalArgumentException("Error: Fizz number not found");
}
If there's any relevant code that I may not have posted please let me know and i'll edit my post.
Some ideas come to me now to communicate two activities..
You can create a method setValuePicker()/getValueFromPicker() in the first Activity, and call getValue() in the other activity.
public void setValuePicker(Parameter value){
number = value;
}
public Parameter getValuePicker(){
return value;
}
If you variable have been set global only need implement getValue() method.
If you someday want use fragments need implement callback.
EDIT:
Using your code is something like this:
- FirstActivity
public class MainActivity extends AppCompatActivity {
private int number;
public static final String FIZZ_TAG = "fizz_numer";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final NumberPicker picker = (NumberPicker) findViewById(R.id.numberPicker);
picker.setMinValue(0);
picker.setMaxValue(10);
picker.setValue(5);
picker.setWrapSelectorWheel(true);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
number = picker.getValue();
Intent intent = new Intent(getApplicationContext(), Main2Activity.class)
.putExtra(FIZZ_TAG,number);
startActivity(intent);
}
});
}
}
Second Activity need be something like this.
.
.
.
.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
int s = getIntent().getIntExtra(MainActivity.FIZZ_TAG,0);
TextView textView = (TextView) findViewById(R.id.text_test);
textView.setText(String.format("%s = %d","number ",s));
}

Password text Field

I am doing a project for class where I would like to create a little password login in Android Studio. I want to create something simple and I know how to do it on Java but I don't know how I would go about doing it in this application. I basically want to get up a password box and a button. On down of the button I would like to test the input of the edit text password box to see if it equals the variable. This variable would be set and definite to something like root. I need to find a way to test that output on the password field to see if it equals the variable. If it does then it would move to another page. The code will be below
my Java file:
package com.example.murdocbgould.passwordpt4;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String passwordA = "root";
}
}
my XML file:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.murdocbgould.passwordpt4.MainActivity">
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword"
tools:layout_editor_absoluteX="85dp"
tools:layout_editor_absoluteY="260dp" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password:"
tools:layout_editor_absoluteX="160dp"
tools:layout_editor_absoluteY="226dp" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
tools:layout_editor_absoluteX="148dp"
tools:layout_editor_absoluteY="437dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="328dp"
android:layout_height="43dp"
android:text="Bluetooth Texting Login"
android:textSize="30sp"
tools:layout_editor_absoluteX="28dp"
tools:layout_editor_absoluteY="147dp" />
</android.support.constraint.ConstraintLayout>
Here getting text from edittest and compare on button click if it matches than go to another activity.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String passwordA = "root";
EditText editText = (EditText) findViewById(R.id.editText);
Button button = (Button) findViewById(R.id.button);
TextView textView2 = (TextView) findViewById(R.id.textView2);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textView2.setText(editText.getText().toString().trim());
if(editText.getText().toString().trim().equals(passwordA)){
Intent i = new Intent(MainActivity.this, SecondActivity.class);
startActivity(i);
}else{
// Do what you want when password is not matches.
}
}
});
}
}
you can get the input from the EditText and compare it with the password variable in onClick of the button.
//Making reference of edittext.
EditText etPassword = (EditText)findViewById(R.id.editText)
// setting onclick to Button.
((Button)findViewById(R.id.button)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// checking the required condition.
if(etPassword.getText().toString().trim().equal(YOUR_PASSWORD_VARIABLE))
{
//PASSWORD MATCHES
}else
{
//PASSWORD MISSMATCHE
}
}
});
EditText editText = (EditText)findViewById(R.id.edittext);
Button button = (Button)findViewById(R.id.Button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (editText.getText().toString().equals(passwordA)){
startActivity(new Intent(this,OtherActivity.class));
}
}
});
First, you need to reference your buttons in the java code and then you have to get references to all the necessary textfields too, see the simple implementation below:
package com.example.murdocbgould.passwordpt4;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//make references to your edittext and buttons in the activity_main.xml
EditText passwordField = (EditText) findViewById(R.id.editText);
Button submitButton = (Button) findViewById(R.id.button);
String passwordA = "root";
//listen for button clicks here
submitButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String password = passwordField.getText().toString();
//compare your password with password and continue
//if you wish to move to another page (which I assume is an activity), edit the next line
startActivity(new Intent(MainActivity.this, NewActivity.class));
}
});
}
}
EditText editText = (EditText)findViewById(R.id.edittext);
Button button = (Button)findViewById(R.id.Button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (editText.getText().toString().equals(passwordA)){
startActivity(new Intent(this,OtherActivity.class));
}else{
//Pasword Not Match
}
}
});
EditText mEditText = (EditText)findViewById(R.id.edittext);
Button mBtn = (Button)findViewById(R.id.Button);
mBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (editText.getText().toString().equals(passwordA)){
startActivity(new Intent(this,AnotherActivity.class));
}
}
});
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textView.setText(editText.getText().toString().trim());
if(editText.getText().toString().trim().equals(passwordA)){
Intent i = new Intent(MainActivity.this, SecondActivity.class);
startActivity(i);
}else{
// Show the user as a Toast that the password is incorrect.
}
}
});

How to add a counter in Java?

The app stops. Where am I wrong? The button when clicked change the text to the number of times the item was clicked.
private Button button;
int countClicks = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
countClicks++;
button.setText(countClicks);
}
});
That's a classic mistake
You are using View.setText() on an int, and it needs to be on a String.
Try
button.setText(countClicks + "");
By concatenating the integer with an empty String, it will auto-cast it to a String

Want to display stored name/value data in intent to new activity screen

I am semi-ok with the understanding of how you store and retrieve data through intents, what I am not sure about however is the code implementation. What I want to accomplish is to pass stored data (name/value pairs and a bundle object) to my ThirdActivity class from MainActivity class. Here is a snippet what i have attempted thus far (which when the user clicks on the a3button it just displays a blank page with no string):
MainActivity from which I want to store data to send to ThirdActivity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button a2_button = (Button) findViewById(R.id.a2_button);
a2_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent a2intent = new Intent("com.tester.lab.x");
startActivityForResult(a2intent, 1);
}
});
Button a3_button = (Button) findViewById(R.id.a3_button);
a3_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent a3intent = new Intent(v.getContext(), ThirdActivity.class);
a3intent.putExtra("greeting","my Name");
startActivity(a3intent);
}
});
}
}
ThirdActivity:
public class ThirdActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
getIntent().getStringExtra("greeting");
}
}
How would I display the string "my Name" once the user clicks ThirdActivity button? Any help is wonderful, thanks!
Everything is good, just add String type for value that you are getting and use this String as you want (to show in TextView else..):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
EditText text = (EditText) findViewById(R.id.your_textView_id);
String value = getIntent().getStringExtra("greeting");
text.setText(value);
}
Or by using Button:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
EditText text = (EditText) findViewById(R.id.your_textView_id);
Button three = (Button) findViewById(R.id.your_button_id);
three.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String value = getIntent().getStringExtra("greeting");
text.setText(value);
}
});
}

Android get text from editText

#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton(){
button = (Button) findViewById(R.id.button);
textView = (TextView)findViewById(R.id.textView3);
button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View arg0) {
input1 = editTextView1.getText().toString();
input2 = editTextView2.getText().toString();
First initialize the editText just like you initialize the Button and TextView then you are able to get the text from editText
you must have initialize.
(EditText, input1 and input2).
in Activity :
EditText editTextView1;
EditText editTextView2;
String input1;
String input2;
in onCreate :
editTextView1 = (EditText) findViewById(yourEditText1_Id);
editTextView2 = (EditText) findViewById(yourEditText2_Id);
Here is a little code that might help point you in the right direction. Post your code and I'll make the code fit your needs.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
TextView textView = (TextView)findViewById(R.id.textView3);
EditText editText1 = (EditText) findViewById(R.id.editTextView1);
EditText editText2 = (EditText) findViewById(R.id.editTextView2);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button:
input1 = editText1.getText().toString();
input2 = editText2.getText().toString();
break;
}
}
Just going to make some of the answers above a bit clearer. An Activity is like a Class in a regular Java program. That means that it has fields (variables) and methods (functions). You can't access a field from within a method unless you declare in within the body of the class, like this:
public class MainActivity extends Activity implements OnClickListener{
EditText input1;
protected void onCreate(Bundle savedInstanceState){
// normal onCreate stuff
input1 = (EditText) findViewById(R.id.input1);
// this should be the id in your layout.xml file
}
public String getText(){
String s = input1.getText().toString().trim();
return s;
}
}
Note the use of the trim() method, which will remove any extra whitespace the user may have added at the end of their input.

Categories

Resources