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.
Related
My scenario is as it follows: I have a search fragment and in that fragment, I have 3 text fields(Search by name, by zip code and by distance). When the focus is applied on the "Search by name" field, the other 2 fields dissapear and the width of the selected field increases. My problem is that I can't lose focus of the first field and so, after I am done with writing the information, I can't use the other 2 fields.
What I am looking for is a way to lose focus of the fields when I press "enter" or when I press anywhere else on the screen but the text field.
Here is the code that I am running right now:
final EditText searchByName = (EditText) getActivity().findViewById(R.id.search_by_name);
final EditText searchByZipcode = (EditText) getActivity().findViewById(R.id.search_by_zipcode);
final EditText searchByDistance = (EditText) getActivity().findViewById(R.id.search_by_distance);
searchByName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
/* When focus is lost check that the text field
* has valid values.
*/
if (hasFocus) {
searchByName.getLayoutParams().width=900;
searchByZipcode.setVisibility(View.INVISIBLE);
searchByDistance.setVisibility(View.INVISIBLE);
} else {
searchByName.getLayoutParams().width=405;
searchByZipcode.setVisibility(View.VISIBLE);
searchByDistance.setVisibility(View.VISIBLE);
}
}
});
searchByZipcode.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
/* When focus is lost check that the text field
* has valid values.
*/
if (hasFocus) {
searchByZipcode.getLayoutParams().width=700;
searchByName.setVisibility(View.INVISIBLE);
} else {
searchByZipcode.getLayoutParams().width=240;
searchByName.setVisibility(View.VISIBLE);
}
}
});
Inside your Activity Class , add this below method , it will work for you .It will hide keyboard and will clear focus anywhere outside you click in that one it will work .
#Override
public boolean onTouchEvent(MotionEvent event) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.
INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
return true;
}
How show hint text when open edittext in fullscreen mode?
I'm can show hint text with OnFocusChangeListener.
editChangeName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
editChangeName.setHint(b ? getResources().getString(R.string.name) : "");
}
});
But.
How hide hint text after closing edittext?
// assuming you have a EditText view with id
View viewById = getView().findViewById(R.id.editTextId);
if(viewById!=null) {
// set listener to this view "many views could share one listener"
viewById.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
// if view match id && has no focus
if(v.getId() == R.id.editTextId && !hasFocus)
// clear hint
((EditText) v).setHint(null);
// else show hint
}
});
}
hasFocus <- as name says
has (true) -> show
doesn't have (false) -> hide
if you are not sure if view by id is one you assign to an edit text do a check ( double check will not hurt)
if (EditText.class.isAssignableFrom(v.getClass()))
// do cast
EditText editText = (EditText) v;
This code is correct. But. How hide hinttext, after closing softkeyboard.. edittext have focus – Eugenu Yansky
Detecting when user has dismissed the soft keyboard
How to remove focus without setting focus to another control?
How to clear focus and remove keyboard on Android?
I want to know is there any method by which i can remove the specific character from the EditText. I want to remove charter from EditText by specifying position in edit text
is there something like removeAt() for EditText?
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button b1=(Button)findViewById(R.id.button1);
final EditText ed1=(EditText)findViewById(R.id.editText1);
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
//i want something like this
ed1.removeAt(5);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
EXTRA INFORMATION
the main problem is i want to make text bold inside edittext but not all the text. The text that is typed after setting bold button on(bold is my toggle button).
ok here is the code
ed1.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
int i = 0;
switch(event.getAction())
{
case KeyEvent.ACTION_UP:
if(bold.isChecked())
{
i=ed1.getSelectionStart();
ed1.append(Html.fromHtml("<b>"+ed1.getText().toString().charAt(i-1)+"</b>"));
}
return true;
}
return false;
}
});
the problem is that i get double character's one is normal character and then again the bold one so i want to remove that normal characters by specifying there position
You have to change it to string first because getText returns editable, e.g:
int charIndex;
String text = ed1.getText().toString();
text = text.substring(0, charIndex) + text.substring(charIndex+1);
ed1.setText(text);
To remove a Character and keep the BOLD characters the same :
ed1.setText((Spanned)ed1.getText().delete(indexStart , indexEnd));
This will remove the Characters from index "indexStart" to "indexEnd -1".
e.g. if you use delete(0,1); it will delete the first Character.
I hope that helps :)
My Implementation is given below. I hope it will be helpful
int cursorPosition = mDialerEditText.getSelectionStart();
if (cursorPosition > 0) {
mDialerEditText.setText(mDialerEditText.getText().delete(cursorPosition - 1, cursorPosition));
mDialerEditText.setSelection(cursorPosition-1);
}
EditText is just like any other TextView, but it can also be edited. Therefore you can use the getText() method, store it as a String, remove the desired char, and call setText(str) method on the EditText to change the text.
Get the input from the EditText and store in a String.
String temp = ed1.getText().toString();
Now manipulate this String temp to remove the character at the desired position.
(I suggest you use String.substring(..) function to achieve this.)
Lastly, set the manipulated string temp in the EditText.
ed1.setText(temp);
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().
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;
}
}