I'm developing an app in Android and need to implement a common app function- accepting numeric input from the user via a EditText. After typing in this value, I want the user to be able to be select a 'go' button. A result should then appear below the first TextView. What should I use to display the result (TextView etc?) and how can I go about implementing this process. Thanks
You are new to this so First check d link http://www.tutorialspoint.com/android/android_event_handling.htm .
Here You have to define 1 TextView , 1 EditText and 1 Button
like
Button _go = (Button) findViewById(R.id._btngo);
than
_go.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TextView _getdata= (TextView) findViewById(R.id._tvresult);
EditText _result= (EditText) findViewById(R.id._etgetinput);
// get the input value and store as a string
String result = _result.getText().toString();
// display the result in textview
getdata.setText(result);
}
});
Hope you will find the solution .. :)
Related
I create a dialog window in an activity, then I put the buttons in this dialog window and listened to them. However, I want to listen to android device found in the back button. and dialog.setOnkeyListen method I used for it. I have put the program by executing dialog window EDITTEXT not enter data into the field. So when I add code to the setOnkeyListen I can not enter data into the EditText field. I hope you know what I mean
If you're asking how to set the OnClickListener for a textview here is an example hope this helps:
TextView textView = (TextView) findViewById(R.id.textView);
textView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// Code you want to execute on the click goes here
}
});
So I have 6 edit texts and a button as shown below:
My question is how do I use the input from the EditTexts (which I have stored in content_main.xml) to do mathematical operations like calculating an average which I want to show up in a toast when the calculate button is pressed. I have already written some code in the MainActivity.java file that brings up a toast when the calculate button is pressed (also in content_main.xml), I just need to figure out how to use the inputs from the EditTexts in the toast.
EditText myText // = findViewById...
String text = myText.getText().toString();
What you should do first is to give each of its elements ID to also recognize from the Activity.
Then you should use the click event of the button
//Here it is referring to the id that gave his element in its layout
Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
And finally, like the button, get their input values EditText
//Here it is referring to the id that gave his element in its layout
EditText text = (EditText)findViewById(R.id.editText01);
And in order to do math, parse the string value remaining on a double (double for decimals can give the exact calculation if you want something, if you want to be an int approximately)
try{
Double value = Double.parseDouble(text);
}catch(NumberFormatException e){
//Message for error parse
}
I am trying to make an android app for my local fence painting business and I simply want it to calculate quotes and payments. My problem is figuring out how to get a simple input box for the square feet of the fence (edittext) with the help of a button to display a that input to a textview. Will you help me?
Also if there is a different way to do this I would love to hear it.
Thank you!
My problem is figuring out how to get a simple input box for the
square feet of the fence (edittext) with the help of a button to
display a that input to a textview.
Pretty simple. Create a TextView, a Button and an EditText object:
TextView txtView;
Button btn;
EditText edtText;
Initialize them:
txtView = (TextView) findViewById(R.id...);
btn = (Button) findViewById(R.id...);
edtText = (EditText) findViewById(R.id...);
This is how you get the input of the EditText by clicking the Button and set the input to the TextView:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String inputText = edtText.getText().toString(); // Get the input
txtView.setText(inputText); // Set the text to the TextView
}
});
I am developing an application for Android.
My application contains ten buttons, to which I have set an onclicklistener() method.
The ten buttons contains the digits 0-9.
Now, if I click any two or three buttons among the ten buttons, the corresponding digits must be entered into edit text and it must be shown in the edittext box.
I am able to display the single digit if I click on any of the buttons, but if I click on another button, then the previous value disappears and the new value is shown.
But what I want is this: no matter how many buttons I click, that no. of digits will appear in the edittext box.
Please can anyone explain to me the code, or give me a hint so that it can be made in a simpler way.
Using Shared Preferences:
I think, you may used Shared Preferences when you button was click, get value from Edittext and put on shared preferences. After click next button get that shared preferences value. You may used each button click put on value shared preferences.
Go to this problem, which is help you to solve: >> SharedPreference problem in android
Using Intent:
May be used this code on button click event:
Bundle extras = getIntent().getExtras();
String value1 = extras.getString("Value1");
String value2 = extras.getString("Value2");
if (value1 != null && value2 != null) {
EditText text1 = (EditText) findViewById(R.id.EditText01);
EditText text2 = (EditText) findViewById(R.id.EditText02);
text1.setText(value1);
text2.setText(value2);
}
Other useful resources:
Get Value of a Edit Text field
http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-edittext-controls/
http://www.java2s.com/Code/Android/UI/GetvaluefromEditText.htm
http://geekswithblogs.net/bosuch/archive/2011/01/17/android---passing-data-between-activities.aspx
You are using editText.setText("");
Instead you must use editText.append();
You can take a public static String variable and concat the new value to previous and set it to EditText
Use below code
public class AsActivity extends Activity {
/** Called when the activity is first created. */
Button b1,b2,b3;
EditText et;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1=(Button)findViewById(R.id.button1);
b2=(Button)findViewById(R.id.button2);
b3=(Button)findViewById(R.id.button3);
et=(EditText)findViewById(R.id.editText1);
}
public void onclick(View v){
switch(v.getId()){
case R.id.button1:
et.append("1");
break;
case R.id.button2:
et.append("2");
break;
case R.id.button3:
et.append("3");
break;
}
}
}
here i have use property of button you can use switch statement as shown above in ur onclicklistener
In all 0-9 buttons onclick event you can write following code.
editText.setText((editText.getText().toString) +""+ nevText);
When you click on button then above code set newText with previous text in edittext box.
You should use the EditText append() method which appends data to the EditText.
So each time a new button is clicked just use :
myEdtiText.append(str);
So, today I decided to try out Android, so please understand that I am a beginner in it.
What I want to achieve right now is to have a EditText, and a set of buttons to be used to enter data into the EditText.
What I've done currently is stick a set of button widgets in the XML layout, and I use this code to make the buttons insert stuff into the EditText:
final EditText inputline = (EditText) findViewById(R.id.textentry);
final Button my_button = (Button) findViewById(R.id.my_btn);
my_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
inputline.append("a");
}
});
This kind of works, but I need help with a few issues:
it always appends the character at the end of the string, not at the current cursor position
similarly, when I call inputline.selectAll() and press my button, it inserts the text at the end of the string again; whereas I want it to delete the text first (as it's selected) and then insert the character
it seems tedious to write all that code for each of the buttons I have. Is there a better way to do this altogether?
Thanks for your help!
I have now pretty much solved by replacing inputline.append("a"); etc. with my custom function, lineInsert(), which you can see below.
public void lineInsert(CharSequence text) {
final EditText inputline = (EditText) findViewById(R.id.textentry);
int start = inputline.getSelectionStart();
int end = inputline.getSelectionEnd();
inputline.getText().replace(Math.min(start,end), Math.max(start,end), text, 0, text.length());
inputline.setSelection(inputline.getSelectionEnd());
}
This has the same behavior as the soft keyboard.