EditText Clear Text with click(Xamarin) - android

Hi i'm using this code when user click on an edittext to clear it.
The Problem is that it doesnt work properly.I mean that i must click my EditText two times for clear it. In first click it opens Keyboard and in second click, it runs my code:
Here is my code:
var comments = FindViewById<EditText>(Resource.Id.txtExtrasComment);
comments.Click+=delegate
{
comments.Text = "";
};
How Can i fix this?

I found my answer with another way. i just added in my edittext
android:hint="comments..."

if you want edit in .cs file you use in onCreate()
...
editText.setOnClickListener(this);
...
public void onClick(View v)
{
editText.setText("");
}
other way in .cs
editText.getText().clear();
if you want edit axml page, you use placeholder properties. You have wrote already up.
android:hint="Enter number"

Try this:
From the forum
You can use a onTouchListener on the edittext to clear the data inside it.
myEditText.Click += (sender, eventArgs) => {
// do my business
// Add the line below
(sender as EditText).OnTouchEvent(eventArgs.Event);
};

Related

Set edittext input to password in fragment. (programatically)

I got problem with setting EditText to password input type.
I try all stuff from stackoverflow, but nothing works when my EditText is in Fragment.
passBox.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD);
passBox.setTransformationMethod(PasswordTransformationMethod.getInstance());
Try both of this and together in any combinations.
public void setViews(Context activity)
{
...
passBox = new EditText(activity);
passBox.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD);
passBox.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
In activity:
cFragment confFrag = new cFragment();
confFrag.setViews(this);
...
confFrag.passBox.setText(settings.getString(PASS, DEFAULT_PASS));
ANSWER:
passBox.setTransformationMethod(PasswordTransformationMethod.getInstance());
Must be called after adding EditText to other Views.
passBox.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
Remove hints, so it is usefull too. Anywhere before transformationMethod.
Try calling the setViews after the onCreateView method from the fragment is done and for the EditText it is enough to use: text.setTransformationMethod(PasswordTransformationMethod.getInstance()); .
try this
passBox.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);

Clearing edit text data

I have certain edit text fields. I save the data entered in these fields to my database, which opens in another activity.
But I have a problem when I navigate back to the first activity (Using the back button on the hardware of the emulator) to add next record, the edit field data is retained.
I tried onPause() and myEditText.setText("") also. But the dat simple clears off the edit fields but as soon as I click the fields to enter data again the previous data reappears.
I also tried using finish() and everything works except I have to go through all the activities again to enter the data.
Try this one also.
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String uid=editText1.getText().toString();
String pwd=editText2.getText().toString();
editText1.setText("");
editText2.setText("");
}
});
}
Just to make sure that I understood you clearly. You enter text in EditText boxes. Then press a button which takes you to a new activity. But, when you go back to the old activity the text in the EditText boxes doesn't get clear. Try to do editText.setText(""); when you click the button. I know you said that you tried it, but did you try it inside the function which listens for the button click?
public EditText editText;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editText = (EditText)findViewById(R.id.editText1);
editText.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// As soon as button is clicked, set is as empty
editText.setText("");
}
Try and see if it makes a difference.
you can use this library to clear text by a clear icon
http://droidparts.org/widgets.html#clearableedittext
Use this:
editText.getText().clear();
after onclick of any action do below step
((EditText) findViewById(R.id.yoursXmlId)).setText("");
or else
write this in XML file
EditText
android:hint="Enter Name" />
i did this way.try
You can use:
myEditText.setText(null);

How do I make a button execute code only if text is entered in a field?

I'm making an app for Android, and if you click the calculate button on one of the pages, without anything entered in the text boxes, it force closes. Some users were wondering if this could be fixed, so I was wondering if there was a way to make the onClickListener execute only if there is something inside the EditText.
You have to check it yourseft, such as:
final EditText editText = ...; // your edit
// check in your onClickListener
if (editText.getText().toString().isEmpty){ // Check if your EditText is
}else{ // If your EditTexit is not null
}
Please, search google before asking any question!
You can try following code,
suppose you have a EditText txtNum1 & txtNum2 , so onClickListener() method you can write following condition
public void onClick(View v)
{
if ( v == cmdCalculate )
{
if ( !txtNum1.getText().equals("") && !txtNum2.getText().equals("") )
{
// your calculation code
}
else
{
// post error msg code
}
}
}

How can I Add Numbers one by one by clicking a button in EditText?

I am making my first app in android that is a simple calculator.
When I press button '1' then it shows "1" in EditText, but when I press it again it doesn't show "11", it shows only "1".
How can I fix this?
Since you've offered no code I can only take a stab in the dark and guess that you're calling setText() on your EditText which would overwrite whatever was in there in the first place. What you would need to get is something like:
myEdit.setText(myEdit.getText + "1");
try this code on button click.use append method to add text number in editview as:
EditText editText = (EditText) findViewById(R.id.editText1);
editText.append("1");
and you can use:
editText.setText(editText.GetText() + title);
you are probably replacing the contents of the EditText completely, while what you want to do is append to it ... something like,
oneButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(editText.getText().toString() + "1");
}
}

Make a custom keyboard in Android having the same behavior as the soft keyboard

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.

Categories

Resources