Delete last character of edittext - android

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

Related

Disable a button after is has been clicked, android

I am trying to disable the button after it is clicked. Th app doesn't crash it just doesn't disable it. wondering could anyone help me out?
here is the on click method for the button i am trying to disable.
//Submit button for answer
final Button submit = (Button) findViewById(R.id.submit);
submit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
EditText answerA = (EditText) findViewById(R.id.answerA);
String toCompare = answerA.getText().toString();
TextView score = (TextView)findViewById(R.id.score_text_a);
scoreKeeper scoremgr = new scoreKeeper();
//anaswer to input
if(toCompare.matches("Alligator") || toCompare.matches("alligator") ||
(toCompare.matches("Alligator ") || toCompare.matches("alligator "))) {
//adds to score if inout matches one of the above
scoremgr.addToScore();
score.setText("Your score is " +Integer.toString(scoremgr.checkScore()));
//calls the next letter class
Intent intent_b = new Intent(button_a.this, button_b.class);
startActivity(intent_b);
//displays a toast message if correct
Toast.makeText(button_a.this, "Well Done, You Got it Right", Toast.LENGTH_SHORT).show();
submit.setEnabled(false);
}else{
//displays a toast meaasge if wrong
Toast.makeText(button_a.this, "Wrong Answer, Try Again", Toast.LENGTH_SHORT).show();
}
}
});
You put your "setEnabled" under an if statement. Just put it before. If that's a feature, check if the correct statement is executed, the condition is probably invalid, resulting the button not being disabled.
In first place, check if your code are being executed, you are doing it right, also you can try to use to disable your button:
btn.setEnabled(false);
btn.setClickable(false);
Also, change your Button variable to an instance variable without final
and access it inside your onClick method.
If you want to remove the button from your layout, you can change the visibility on it:
btn.setVisibility(View.GONE):
in your xml file set
android:clickable="true"
and now
btn.setEnabled(false);
btn.setClickable(false);
and check your (if condition ) does it returns true or not

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
}

Erasing Character from EditText on Button click

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

Button onclick listener must be displayed in edittext

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

how to take input from user in android

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.

Categories

Resources