How do I get user input into an EditText field - java

I am building a larger app and I made this small one just to figure out how to accomplish getting text from user input however it is not working. If I create a string reference at the Text property of my EditText field it works ok. If I leave it blank and enter the text into the field when the application runs in my emulator it does not work. Any ideas.
package com.example.stringtest;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Button;
public class Main extends Activity {
EditText display;
EditText displayTwo;
String displayContents;
Button displayText;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainlayout);
display = (EditText) findViewById(R.id.editText1);
displayContents = display.getText().toString();
displayTwo = (EditText) findViewById(R.id.editText2);
displayText = (Button) findViewById(R.id.button1);
displayText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
displayTwo.setText(displayContents);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.mainlayout, menu);
return true;
}
}

Try this instead!
displayText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
      displayContents = display.getText().toString();
displayTwo.setText(displayContents);
}
});

displayContents = display.getText().toString();
The displayContents will not be updated when the text inside the EditText changes.
Instead of doing this you should call getText() every time you want to get the current text value.
If you want to be notified every time the text in the EditText is being changed then you should add a listener to the appropiate event. In this case it's EditText.addTextChangedListener.
More information about that:
http://developer.android.com/reference/android/widget/TextView.html#addTextChangedListener(android.text.TextWatcher)

Related

How do I transform a string from EditText to a character array, to allow audio implementation?

This is my code:
package com.example.pembroke.finalalgorhythmic;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button asdf;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
asdf = (Button) findViewById(R.id.keybutton);
asdf.setOnClickListener(MainActivity.this);
}
#Override
public void onClick(View v) {
EditText keyInput = (EditText) findViewById(R.id.key);
String notes = keyInput.getText().toString();
}
What I want to do, is create a character array, so that I can use media player class to play certain sounds based on the user input after my button is clicked. Any ideas?
Have you tried
notes.toCharArray() yet?
EDIT: Long version
EDIT 2: Sample implementation
MediaPlayer mp;
char[] currentNotes;
int noteIndex;
// Create the mp in onCreate and register your onCompletion callback
#Override
public void onClick(View v) {
EditText keyInput = (EditText) findViewById(R.id.key);
String notes = keyInput.getText().toString();
currentNotes = notes.toCharArray();
noteIndex = 0;
mp.setDataSource(getNoteResource(currentNotes[0]));
mp.start();
}
// Convert tone values into
public int getNoteResource(char tone) {…}
#Override
public void onCompletion(MediaPlayer mp) {
if(++noteIndex < currentNotes.length) {
mp.setDataSource(getNoteResource(currentNotes[noteIndex]));
mp.start();
}
}

App is not getting deployed

I've started coding in android. While running my app, I'm getting message "Unfortunately, myapp is stopped".
I've attached the code below.
Can you tell me what's wrong with this code?
The code is given below :
package in.developer.rjsharma.myfirstapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View.OnClickListener;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText fno = (EditText) findViewById(R.id.fno);
String no1=fno.getText().toString();
int first=Integer.parseInt(no1);
EditText sno = (EditText) findViewById(R.id.sno);
String no2=fno.getText().toString();
int second=Integer.parseInt(no2);
final int r1=first+second;
final int r2=first-second;
final int r3=first*second;
final int r4=first/second;
Button add = (Button) findViewById(R.id.add);
Button subtract = (Button) findViewById(R.id.subtract);
Button multiply = (Button) findViewById(R.id.multiply);
Button divide = (Button) findViewById(R.id.divide);
final TextView solution = (TextView) findViewById(R.id.solution);
add.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
solution.setText(r1);
}
});
subtract.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
solution.setText(r2);
}
});
multiply.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
solution.setText(r3);
}
});
divide.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
solution.setText(r4);
}
});
}
}
You are not allowed to set Integer in Textview. You have to convert Integer to String
package in.developer.rjsharma.myfirstapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View.OnClickListener;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText fno = (EditText) findViewById(R.id.fno);
String no1=fno.getText().toString();
int first=Integer.parseInt(no1);
EditText sno = (EditText) findViewById(R.id.sno);
String no2=fno.getText().toString();
int second=Integer.parseInt(no2);
final int r1=first+second;
final int r2=first-second;
final int r3=first*second;
final int r4=first/second;
Button add = (Button) findViewById(R.id.add);
Button subtract = (Button) findViewById(R.id.subtract);
Button multiply = (Button) findViewById(R.id.multiply);
Button divide = (Button) findViewById(R.id.divide);
final TextView solution = (TextView) findViewById(R.id.solution);
add.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
solution.setText(String.valueOf(r1));
}
});
subtract.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
solution.setText(String.valueOf(r2));
}
});
multiply.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
solution.setText(String.valueOf(r3));
}
});
divide.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
solution.setText(String.valueOf(r4));
}
});
}
}
You are making some mistakes in the code. You are reading the values of EditText fno in the onCreate(), it has just been created, their value would be an empty String. If you parse that to a value, I don't know if it will be 0 or null.
However, I would try it this way (not tested - likely to be errors with solution or other variables):
public class MainActivity extends AppCompatActivity {
private EditText fno, sno;
private TextView solution;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText fno = (EditText) findViewById(R.id.fno);
EditText sno = (EditText) findViewById(R.id.sno);
Button add = (Button) findViewById(R.id.add);
Button subtract = (Button) findViewById(R.id.subtract);
Button multiply = (Button) findViewById(R.id.multiply);
Button divide = (Button) findViewById(R.id.divide);
TextView solution = (TextView) findViewById(R.id.solution);
add.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
solution.setText(readIntegerFromEditText(fno)+readIntegerFromEditText(sno));
}
});
subtract.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
solution.setText(readIntegerFromEditText(fno)-readIntegerFromEditText(sno));
}
});
multiply.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
solution.setText(readIntegerFromEditText(fno)*readIntegerFromEditText(sno));
}
});
divide.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
solution.setText(readIntegerFromEditText(fno)/readIntegerFromEditText(sno));
}
});
}
private int readIntegerFromEditText (EditText editText){
int no = Integer.parseInt(editText.getText());
return no;
}
}
There are a lot of things to be done yet, like checking for 0 division. You will find out on the way, if you use the LogCat and Debugging messages.

split string, set token and text view

I am writing a method, that will read a string, split a string, and then display one of the values in a text field. Here is what I have so far, it is compiling, but when the button is clicked, nothing is populated into the text field. Thanks
import java.io.DataInputStream;
import java.io.InputStream;
import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
import android.view.*;
public class AboutScreen extends Activity{
Button homeButton=null;
Button getReactant1=null;
TextView tv=null;
String is=null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
homeButton=(Button)findViewById(R.id.Home);
homeButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
getReactant1=(Button)findViewById(R.id.combinationinterface);
getReactant1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String string1 = getResources().getString(R.string.MgandO);
String delims =",";
String[] tokens = string1.split(delims);
String i = null;
tokens[0]= i;
String f = null;
tokens[1]= f;
tv=(TextView)findViewById(R.id.R1TextInput);
is=i;
tv.setText(i);
}
});
}
});}
Because you called the finish() method at the beginning of the onClick(). Which stops your activity without doing the following stuff.
Your first problem is setting OnClickListener of getReactant1 button inside of homeButton's OnClickListener.
Your other problem is using irrelevant variables and not using splitted value at your textView.
Try to use and modify code below:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
homeButton=(Button)findViewById(R.id.Home);
homeButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
getReactant1=(Button)findViewById(R.id.combinationinterface);
getReactant1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String string1 = getResources().getString(R.string.MgandO);
String delims =",";
String[] tokens = string1.split(delims);
String is=token[0];
// or set it to another token you want i.e. token[1]
tv=(TextView)findViewById(R.id.R1TextInput);
tv.setText(is);
}
});
}

Null pointer exception when setting results in a textview

The application is crashing when pressing the Computecost button
package com.example.hw_3;
import android.os.Bundle;
import android.app.Activity;
import android.view.*;
import android.widget.*;
import android.content.*;
public class ShoppingExpensesPage extends Activity
{
TextView et;
Button computecost;
Button save;
Button cancel;
RadioGroup drinks;
RadioButton drink;
int tottalcost=0;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
et=(TextView)findViewById(R.id.tv11);
Bundle extra = getIntent().getExtras();
String val1 = extra.getString("value");
et.setText(val1);
computecost=(Button)findViewById(R.id.btn11);
computecost.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
String val;
int selectedId = drinks.getCheckedRadioButtonId();
drink = (RadioButton)findViewById(selectedId);
val=(String) drink.getText();
if(val=="Juice")
{tottalcost=tottalcost+3;
}
else if (val=="Cola")
{
tottalcost=tottalcost+2;
}
et.setText(Integer.toString(tottalcost));
}
});
save=(Button)findViewById(R.id.btn21);
save.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
Intent returnIntent = new Intent();
String str=Integer.toString(tottalcost);//(String) et.getText();
returnIntent.putExtra("return", str);
setResult(RESULT_OK,returnIntent);
finish();
}
});
}
}
You haven't set the view for drinks.
int selectedId = drinks.getCheckedRadioButtonId();
Find a view for it, before computecost.setOnClickListener:
drinks = (RadioGroup) findViewById(...);
This is the wrong way to compare Strings
if(val=="Juice")
In Java, "==" compares the reference of the Objects but not their values. You need to use .equals()
if("Juice".equals(val))
{ // do something }
If this doesn't solve your problem then please post the logcat from the error but this still needs to be changed.

button can not be resolved

This is my starting activity. I'm trying to get this button to work but it's been giving me this error.
Line 15 button can not be resolved.
package com.synamegames.giveaway;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class GiveawayActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Button register = (Button) findViewById(R.id.register);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
}
});
setContentView(R.layout.main);
}
}
Please try this..
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button register = (Button) findViewById(R.id.register);
register.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
}
});
}
The problem is You have defined the Button instances as register in the line final Button register = (Button) findViewById(R.id.register); But you are setting onclick listener to the button instance which is not defined. You should have
register.setOnClickListener(new OnClickListener() {
instead of
button.setOnClickListener(new OnClickListener() {
U can use android:onClick from xml and pass a the view into the .java file
eg:
android:onClick="bactToList"
in java:
public void bactToList(View view){
}

Categories