EditText SetPosition - android

I'm trying to make when I click on my EditText cursor position go to the end of the text, my code is as follows:
tb_acrescimo.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
int position = tb_acrescimo.length();
Editable etext = tb_acrescimo.getText();
Selection.setSelection(etext, position);
}
});
But it works on Android API 2.3 but does not work on 4.1, does anyone know what should I do?
I find other solution for my problem but don't answer my question, flees the topic, so I will not post the solution, ok?

Try this:
tb_acrescimo.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus) return;
int position = tb_acrescimo.length();
Editable etext = tb_acrescimo.getText();
tb_acrescimo.setSelection(etext, position);
}
});

Related

How to display view in foreground based on focus?

JAVA CODE:
mEdtTextPickup.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
mLayoutDrop.invalidate();
mEdtTextPickup.bringToFront();
}
}
});
mEdtTextDrop.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
mLayoutPickup.invalidate();
mEdtTextDrop.bringToFront();
}
}
});
`
I have two layouts in my application Source and Destination in which one overlaps the other inside a FrameLayout.(just like the attached image)
Now how do i bring destination layout on top of source layout animated when it is focused. I googled a lot but found no solutions.
You can use a focus change listener to identify when the field is focused and then bringToFront to bring the view above the others.
destinationEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
edittext.bringToFront();
}
}
});

issue with OnFocusChangeListener() in android

I have a form for member registration in my android app, I have applied the animation effect on fields which are required (some EditText views in this case) if those fields are not filled the effect should occur then like this :
mEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View arg0, boolean arg1) {
// TODO Auto-generated method stub
if (mEditText.getText.equals("")) {
mEditText.setAnimation(MyAnimation.animate());}
and MyAnimation.animate() is like :
public class MyAnimation {
public static Animation animate(){
TranslateAnimation mAnimate = new TranslateAnimation(0, 5, 0, 0);
mAnimate.setInterpolator(new CycleInterpolator(50));
mAnimate.setDuration(600);
return mAnimate;
}
}
but the problem is it occurs when the mEditText gains the focus, and my need is this should occur on focus leave if mEditText is empty.
Implement setOnFocusChangeListener and check there's a boolean parameter for hasFocus. When this is false, you've lost focus to another control.
EditText txtEdit= (EditText) findViewById(R.id.edittxt);
txtEdit.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus)
{
//do job here owhen Edittext lose focus
if (mEditText.getText().equals("")) {
mEditText.setAnimation(MyAnimation.animate());}
}
}
});

EditText setOnFocusChangeListener on all EditTexts

I have several EditText fields which I want to save to the SQLiteDatabase with setOnFocusChangeListener. Do I have to set an onFocusChangeListener on each one individually, or is there a catch-all of some sort? (getActivity().findViewByID because this is a fragment)
final TextView txtName = (TextView)getActivity().findViewById(R.id.clientHeader);
final TextView txtCompany = (TextView)getActivity().findViewById(R.id.txtContactCompany);
final TextView txtPosition = (TextView)getActivity().findViewById(R.id.txtContactPosition);
txtName.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus) {
saveThisItem(txtClientID.getText().toString(), "name", txtName.getText().toString());
}
}
});
txtCompany.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus) {
saveThisItem(txtClientID.getText().toString(), "company", txtCompany.getText().toString());
}
}
});
txtPosition.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus) {
saveThisItem(txtClientID.getText().toString(), "position", txtPosition.getText().toString());
}
}
});
Like... is there some way to have a ArrayList < EditText > of EditText Views, assign a pointer (sorry, not sure how) to the existing editTexts and set the onFocusChangeListener to the whole arraylist? Or, even, iterate through the ArrayList and set the onFocusChangeListener to each member?
Or a way to detect ANY onFocusChangeListener events, and just save all data to the database, regardless of what EditText the even occurred on?
Well, you could have your activity implement OnFocusChangeListener. That way, all your changes will be on that one metho,d but you will have to check which view changed focus by getting the view id with v.getId() and handle accordingly.
#Override
public void onFocusChange(View v, boolean hasFocus) {
switch(v.getId()){
case r.id.editText1:
break;
...etc
}
}
There's plenty of ways you could make this simpler. For one, how about abstracting this into a method and calling it for each TextView you want to add the event for:
private void setOnFocusChangeListener(TextView textView, String name){
textView.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus) {
saveThisItem(txtClientID.getText(), name,
textView.getText());
}
}
});
}
Then you can call this method for each of your TextView instances:
setOnFocusChangeListener(txtName, "name");
setOnFocusChangeListener(txtCompany, "company");
setOnFocusChangeListener(txtPosition, "position");
My suggestion would be to use a custom EditText to handle this for you:
public class SaveEditText extends EditText implements View.OnFocusChangeListener {
private String mDataKey;
private String mClient;
public SaveEditText (Context context) {
super(context);
setOnFocusChangeListener(this);
}
public SaveEditText (Context context, AttributeSet attrs) {
super(context, attrs);
setOnFocusChangeListener(this);
}
public void setDataKey (String dataKey) {
mDataKey = dataKey;
}
public void setClient (String client) {
mClient = client;
}
#Override
public void onFocusChange (View v, boolean hasFocus) {
saveThisItem(mClient, mDataKey, getText().toString());
}
}
You might decide to handle the client and data keys differently, but the idea is that you use a custom base class that will handle it automatically.
Or even simpler
OnFocusChangeListener listener;
listener = new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
//Do something
}
};
textbox1. setOnFocusChangeListener(listener);
textbox2. setOnFocusChangeListener(listener);
textbox3. setOnFocusChangeListener(listener);
Since onFocusChange(View v, boolean hasFocus) has the parameter View you can use that to tell which view called it

how to update textview after text changed in edittext field

I have this application where field 1 is edittext and field 2 is textfield.
I want to add date into textview depending on the data entered in edittext field. That is after entering the data in edittext field and when i move to next edittext field the textview of 1st edittext field must be updated.
I know about add textchangedlistener but if it is the solution how am i supposed to implement it.
EDIT : I tried setonfocuschanged but it din work
k1_e.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View arg0, boolean arg1) {
// TODO Auto-generated method stub
if((v_k1>=5.63)&&(v_k1<=11.25)){
k1_m.setText("mm");
}else if((v_k1>=30)&&(v_k1<=60)){
k1_m.setText("D");
}
}
});
k2_e.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if((v_k2>=5.63)&&(v_k2<=11.25)){
k2_m.setText("mm");
}else if((v_k2>=30)&&(v_k1<=60)){
k2_m.setText("D");
}
}
});
edit 2 :
and details here
dr=Float.parseFloat(dr_e.getText().toString());
k1=Float.parseFloat(k1_e.getText().toString());
k2=Float.parseFloat(k2_e.getText().toString());
al=Float.parseFloat(al_e.getText().toString());
al_const=Float.parseFloat(alconst_e.getText().toString());
v_k1=Float.valueOf(k1);
v_k2=Float.valueOf(k2);
EDIT 3 :
I get data into edittext fields using
if(dr_e.getText().toString().length()==0|k1_e.getText().toString().length()==0|k2_e.getText().toString().length()==0|al_e.getText().toString().length()==0|alconst_e.getText().toString().length()==0){
flag=1;
}else{
dr=Float.parseFloat(dr_e.getText().toString());
al=Float.parseFloat(al_e.getText().toString());
al_const=Float.parseFloat(alconst_e.getText().toString());
}
Try this
you can use onFocusChangeListener on youredittext
k1_e.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View arg0, boolean arg1) {
String data=k1_e.getText().toString().trim();
if(data.length!=0)
{
k1=Float.parseFloat(data);
v_k1=Float.valueOf(k1);
if((v_k2>=5.63)&&(v_k2<=11.25)){
k2_m.setText("mm");
}else if((v_k2>=30)&&(v_k1<=60)){
k2_m.setText("D");
}
}
}
});

Get text from an EditText

In my app I have an EditText that I want to get the value out of when it looses focus. How should I do this?
Thanks!
Along the lines of this should work.
EditText.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
#override
public void onFocusChange(View v, boolean hasFocus)
{
if (!hasFocus) {
string value = (EditText) v.getText().ToString();
}
}
}

Categories

Resources