I created an Application to dial particular contact number.
It has one EditText and ten buttons for the digits from 0 to 9 and a BACK button.
I want to erase single digit from EditText on each click event of BACK button.
Is there any way to do so ?
Namaskar modiji, try
myButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
String text = et.getText().toString();
if(!TextUtils.isEmpty(text)){
String newText = text.substring(1, text.length()); //delete from left
//or
String newText1 = text.substring(0, text.length() - 1); //delete from right
et.setText(newText);
et.setSelection(newText.length());
//or
et.setText(newText1);
et.setSelection(newText1.length());
}
}
}
Related
for example, if I have 9 edittext how I can dump the value of a selected edittext and the others not
for example edittext1 = 1, edittext2 = 2, edittext3 = 3, edittext4 = 4, edittext5 = 5, edittext6 = 6, edittext7 = 7, edittext8 = 8, edittext9 = 9
here I want to erase 1 and the other number remains
I need your help
There are two ways:
First method:
edittext1.setText("");
Second method:
edittext1.getText().clear();
You can do this whenever you press a button or select an editText...for example this will clear your editText when you click it
edittext1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
edittext1.getText().clear();
}
});
Also, if you want to prevent the software keyboard from popping up just add this when you declare your editText
edittext1.setInputType(InputType.TYPE_NULL);
Happy coding!
When click edittext that time get data and clear that data and also directly to set value on editext box click event.
editText1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
editText1.setText("");
editText1.getText().clear();
}
});
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());
I have 2 TextView, and 1 Button. I want to be able to click the button and to save the text on the first TextView and show the value on the second TextView.
I've tried this code:
public void buttonOnClick(){
Button submit = (Button) findViewById(R.id.submit);
editName = (EditText) findViewById(R.id.name);
editEmail = (EditText) findViewById(R.id.email);
textout = (TextView) findViewById(R.id.outputText);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
textout.setText(editName.getText()+"\n"+editEmail.getText());
}
});
}
But I get an error on 'textout'. When I clock the red light buble, it says 'create a local variable', field text'.
Try this
public void onClick(View v) {
textout.setText(editName.getText().toString()+"\n"+editEmail.getText().toString());
}
Your problem stimulates from the fact that EditText getText() returns Editable Object. It's not a String and you can't concat 2 Editable Objects.
You have 2 Options:
textout.setText(editName.getText().toString() + "\n" + editEmail.getText().toString());
And Second you can use SpanableStringBuilder
SpannableStringBuilder sb = new SpannableStringBuilder(editName.getText());
sb.append("\n").append(editEmail.getText());
The Second options allows you also to decorate the Text and save you the Need to build a String which is (maybe) better.
I have a quick question.
I have a screen with some numbers, when you click one of the numbers, the number gets appended to the end of the edittext.
input.append(number);
I also have a backbutton, when the user clicks this button I want to remove the last character.
At the moment I have the following :
Editable currentText = input.getText();
if (currentText.length() > 0) {
currentText.delete(currentText.length() - 1,
currentText.length());
input.setText(currentText);
}
Is there an easier way to do this ? Something in the line of input.remove()?
I realise this is an old question but it's still valid. If you trim the text yourself, the cursor will be reset to the start when you setText(). So instead (as mentioned by njzk2), send a fake delete key event and let the platform handle it for you...
//get a reference to both your backButton and editText field
EditText editText = (EditText) layout.findViewById(R.id.text);
ImageButton backButton = (ImageButton) layout.findViewById(R.id.back_button);
//then get a BaseInputConnection associated with the editText field
BaseInputConnection textFieldInputConnection = new BaseInputConnection(editText, true);
//then in the onClick listener for the backButton, send the fake delete key
backButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
textFieldInputConnection.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL));
}
});
try this out,
String str = yourEditText.getText().toString().trim();
if(str.length()!=0){
str = str.substring( 0, str.length() - 1 );
yourEditText.setText ( str );
}
i have a EditText in android in which i want the user to enter the text and checks for the condition "BYE"
Code sample:
EditText text = (EditText)findViewById(R.id.EditText01);
String abc= text .getText().toString();
while( !(abc).equals("bye")){
abc = text.getText().toString();//user should enter the text from keyboard and the while loop should go and chech the condition.but not able to enter the text
//do some operation with abc
}
How can i make user to enter the text??The UI should wait for the text to be entered(something like we have InputStreamReader in java applications).
Very Simple:-
EditText text = (EditText)findViewById(R.id.EditText01);
String str = text.getText().toString();
now in str u will get string which is entered in EditText
I don't think you need a loop to do this. From your comment it looks like you also have an "Enter" button or something that you click to do the checking. Just set an onclicklistener and onclick you can make the edittext invisible (or un-editable), check is the edittext is equal to "BYE" and then do your actions might look something like this:
final EditText ET = (EditText) findViewById(R.id.EnterText);
Button B1 = (Button) findViewById(R.id.EnterButton);
B1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ET.setVisibility(View.INVISIBLE);
if(ET.getText().toString() == "BYE")
{
//do something if it is "BYE"
} else {
Context context = getApplicationContext();
CharSequence text = "Please enter BYE";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
ET.setVisibility(View.VISIBLE);
} });
Instead of running this check in an infinite loop, only run it on every onKeyUp of the EditText. You know, anyway, that the condition will only ever be fulfilled if the user actually enters something.