I want to know by using which method I can get whether the user selecting text in edittext or not so I can use getSelectionStart and getSelectionEnd to perform my desire task.
You can check if your EditText widget has been focused (usually when user touches it on the screen).
findViewById(R.id.editText1).setOnFocusChangeListener(this);
and then implement the listener (in this case within the same class)
public void onFocusChange(View arg0, boolean arg1) {
switch(arg0.getId()){
case R.id.editText1:
if(arg1){
// has focus
}
else{
// doesn't
}
break;
}
}
Related
My Android app shows a floorplan, on which transparent Buttons are set on specific areas. I also implemented a Voice Recognition, that returns a Listview. When the user chooses one item the performClick() Method is performed, which calls the OnClick method. The OnClick method is also called, when the user clicks on the specific area on the display (transparent button)
My question is, how can I see, if the Button is clicked by performClick() or by click on the display?
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
button.setBackgroundColor(Color.GREEN);
}
});
wordsList.setOnItemClickListener(
new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long id) {
// TODO Auto-generated method stub
Object o = wordsList.getItemAtPosition(position);
String pen = o.toString();
if (pen.equals("String")) {
button.performClick();
Toast.makeText(getApplicationContext(), "You have chosen: " + " " + pen, Toast.LENGTH_LONG).show();
}
}
}
);
I already found that post, but I don't know how to implement this in Java.
how to check if the click is Performclick or not
I had the exact same question. Surely I thought there must be a built-in return method of some sort that could tell us whether or not a click was performed by the actual onClick() method or by performClick() but after a while of searching I instead decided to make my own solution...
In the class that perform clicked is called create:
public static boolean performClicked;
In your method that calls "performClick()" simply change
performClicked = true;
right before the call to perform click.
in your onClick use a similar if statement:
if (performClicked){
performClicked = false;
//DO WHAT YOU NEED TO DO WITH THIS INFO
}
I know this is kind of an obvious answer, but maybe not.
I have a table with multiple rows, each containing an EditText which corresponds to a dimension. I want to have some sort of listener that will return whichever EditText is currently selected. Can this be done with getCurrentFocus(); or onFocusChange();?
My idea is to have the user select/focus the dimension field they want edited, and then completing the field using an output slope/angle from the device's gyroscope when a "write data" button is pressed. I'd probably do this using selected_dimension_edittext.setText(gyro_value);
You can know which EditText has Focus by the ID of the view like this:
EditText edText1 = (EditText)findViewById(R.id.editText1);
EditText edText2 = (EditText)findViewById(R.id.editText2);
OnFocusChangeListener mFocusChangeListener = new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(hasFocus){ //Check if View has focus
switch (v.getId()){
case R.id.editText1: {
//Code A
} break;
case R.id.editText2: {
//Code B
} break;
}
}
}
};
edText1.setOnFocusChangeListener(mFocusChangeListener);
edText2.setOnFocusChangeListener(mFocusChangeListener);
If the editTexts are in a ArrayList you could pass a Tag to each editText which is the index.
For example:
for(int i=0;i<editTextList.size();i++){
editTextList.get(i).setTag(i);
}
Then you can check with a listener (textChangedListener or TextWatcher-->http://lecturesnippets.com/android-textchanged-listener-textwatcher/) which EditText is changed or selected by reading the tag. Just iterate through your editTextList and call the setText()-Method if the edit text has the selected tag.
I've got two EditText fields and a button,
EditText topNumericInputField; // Top text field
EditText bottomNumericInputField; // Bottom text field
Button clearButton; // Regular Button
I'm trying to clear the text that's in them when I change focus, my onClick method looks like this:
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.topInputField:
clearTextBoxes();
break;
case R.id.bottomNumericInputField:
clearTextBoxes();
break;
case R.id.clearButton:
clearTextBoxes();
break;
}
}
private void clearTextBoxes() {
topNumericInputField.getText().clear();
bottomNumericInputField.getText().clear();
}
Now, it clears both fields fine with either:
a) The field I click on already has focus
or
b) I click the clear button
If I change focus from the top field to the bottom field, it gets focus, then I need to click it one more time.
I'm not sure exactly what's happening, but I'm sure it's related to something that keeps coming up in the LogCat log, every time I change focus two lines appear:
Tag: IInputConnectionWrapper Text: beginBatchEdit on inactive InputConnection
Tag: IInputConnectionWrapper Text: endBatchEdit on inactive InputConnection
I've tried searching SO and I've seen some submissions that are similar, but none seem to really be what I'm looking for.
Thanks!
I think problem is not with setting text but problem lies in capturing wrong event.
As you said you want to clear edittext when focus is changed, then you should override onFocusChange(View v, boolean hasfocus) to capture that event. You need to implement FocusChangeListener.
edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean focus) {
if (focus) {
((EditText)v).setText("");
}
}
});
A simple implementation should look like above. Refer this for detailed information
You can set the text null.
topNumericInputField.setText("");
bottomNumericInputField.setText("");
You just need to clear your Text by:
topNumericInputField.setText("");
bottomNumericInputField.setText("");
I'm doing a app wich have two EditText. What I wanna do is, if you click on one and write on it, the other must erease anything It has and only shows the hint string. I'm triying doing it witha a check method that does:
if(celsius.isFocused()){
faren.setText(faren.getHint().toString());
}
if(faren.isFocused()){
celsius.setText(celsius.getHint().toString());
}
Then I call this method within the onCreate() method, but of course It only checks one time, and if use that checkMethod inside a loop, the app doesn't show anthing, It freezes. An suggestions?
Use the OnFocusChangeListener.
faren.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus)
celcius.setText(""); // This will automatically show the hint text
}
});
In my application when I click an EditText, I have to perform some logic. I have the code. But it is not going into the click method.
My code:
EditText des=(EditText)findViewById(R.id.desinc);
des.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
java.lang.System.out.println("Inside click");
EditText income=(EditText)findViewById(R.id.editText1);
// TODO Auto-generated method stub
String inc=income.getText().toString();
int indexOFdec = inc.indexOf(".");
java.lang.System.out.println("index="+indexOFdec);
if(indexOFdec==0)
{
java.lang.System.out.println("inside index");
income.setText(inc+".00");
}
}
});
What am I doing wrong? Help me.
Try overriding onTouch by setting up an onTouchListener in the same way as an onClickListener. Use this code as a reference.
EditText dateEdit = (EditText) findViewById(R.id.date);
date.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
//anything you want to do if user touches/ taps on the edittext box
}
return false;
}
});
UPDATE(why this behavior):
The first click event focuses the control, while the second click event actually fires the OnClickListener. If you disable touch-mode focus with the android:focusableInTouchMode View attribute, the OnClickListener should fire as expected.
You can also try this: set android:focusableInTouchMode="false" for your EditText box in the xml. See if it works with the existing code.
You should use OnFocusChangeListener()
Try clicking EditText twice because at first instance EditText gets focus and after that EditText's click event executes. So, if you want your code to execute on first click write your code for focus change of EditText using OnFocusChangeListener().