Make a edittext box display on a textview? - android

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

Related

EditText and button(When button is pressed value shoud be entered in edittext field)

Am creating a calculator,i want when button is pressed for example say 5,how to set value 5 to the button and if that button is clicked . The value should be printed in edit text!!
My source code:
ed=(EditText)findViewById(R.id.editText);
b=(Button)findViewById(R.id.button);
gt=Double.parseDouble(ed.getText().toString());
ed.setText(""+gt);
}
If I understand your question correctly:
U got some buttons with text or digits.
And if someone presses one of those buttons u want to display the text of the button in your editText box?
If so u can do something like the following:
EditText myEditText = (EditText) findViewById(R.id.editText);
Button myButton0= (Button) findViewById(R.id.button0);
myButton0.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View v) {
//Get the button text and display it in the editText
//This will replace all text in the editText box
myEditText.setText(myButton0.getText().toString());
//Or (Thanks to #cherry-wave)
//This will append the text, instead of replacing
myEditText.setText(myEditText.getText() + myButton0.getText().toString());
}
});
Or (to handle all your buttons with only one method):
Add the following line to all your buttons in your layout xml:
android:onClick="onClick"
And place the following method in your activity class file:
//Your onClick method
public void onClick(View v) {
Button myButton = (Button)v;
//Don't forget to declare your edittext
//This will replace all text in the editText box
myEditText.setText(myButton.getText().toString());
//Or (Thanks to #cherry-wave)
//This will append the text, instead of replacing
myEditText.setText(myEditText.getText() + myButton.getText().toString());
}
Hope this helps u out.
Take the value from button and add to edittext .
Just like this .
editText.setText(btnFive.getText().toString());
Before button click get value from editText .
String edtValue = editText.getText.toString();
Than set editText.setText(edtValue+btnFive.getText().toString());

How to use the Edittext in android

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
}

Android thread sleep inputted by user

I have a input box which the users set a time for the delay. Is it possible set delay time by user input in android?
If you are truly just wanting the user to input an integer you could do the following:
EditText tv = (EditText) view.findViewById(R.id.textview);
Button btn = (Button) view.findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Thread.Sleep(Integer.valueOf(tv.getText().toString());
}
});
Obviously the code above needs to be changed to fit your app (and checked for errors) but that is the general gist of the solution. You could check the user input for validation too.

Displaying a result after receiving user input

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 .. :)

Change Text in EditText when pressing the EditText

Some EditText (Like when you write a text message) can be clicked and opened, so you can change the text inside the EditText. It also gives you the keyboard so you can write, etc.
How do I achieve the same thing?
Currently I got this:
public void onDescriptionBoxClick(View view) {
EditText et = (EditText) this.findViewById(R.id.editText1);
}
And I don't know what to do other than that.
Do I have to call the keyboard forth so the user can edit the text in the box?
I would place an EditText element right next to your textView which has the attribute android:visibilty="gone"
then in your code:
public void onDescriptionBoxClick(View view) {
TextView tv = (TextView) this.findViewById(R.id.textView2);
tv.setVisibility(View.GONE);
EditText et = (EditText) this.findViewById(R.id.editText2);
et.setVisibilty(View.VISIBLE);
}
You can't edit text using a TextView. to accomplish what you want you have to replace the TextView with a EditText widget. and then replace it with the TextView containing the text entered in the EditText.
You can accomplish this in a few ways:
Using Fragment, more compicated.
You can use simple TextView and EditText widgets and set their Visability accordingly.

Categories

Resources