Cursor not focus next edittext - android

This my code please check it and help me find out this problem. Basically move cursor from one edit-text to another one if click any letter in field. if you have any better way for this then please help me.I was trying this yesterday.but i not got any solution.actually after second edit-text cursor still and not move next one.I was doing this another way but not success.Thanks in advance.
package osvin.com.edittext;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity implements TextWatcher{
EditText edt_1,edt_2,edt_3,edt_4;
StringBuilder sb;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intialize();
edt_1.addTextChangedListener(this);
edt_2.addTextChangedListener(this);
edt_3.addTextChangedListener(this);
edt_4.addTextChangedListener(this);
}
public void Intialize(){
edt_1=(EditText)findViewById(R.id.edt_1);
edt_2=(EditText)findViewById(R.id.edt_2);
edt_3=(EditText)findViewById(R.id.edt_3);
edt_4=(EditText)findViewById(R.id.edt_4);
sb=new StringBuilder();
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if(sb.length()==1)
{
sb.deleteCharAt(0);
}
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (sb.length() == 0&&edt_1.length() == 1) {
sb.append(s);
Log.e("StringBuilderLength", "--------" + sb.length());
edt_1.clearFocus();
edt_2.requestFocus();
edt_2.setCursorVisible(true);
}
if (sb.length() == 0&&edt_2.length() == 1) {
sb.append(s);
Log.e("StringBuilderLength", "--------2" + sb.length());
edt_2.clearFocus();
edt_3.requestFocus();
edt_3.setCursorVisible(true);
}
if (sb.length() ==0&&edt_3.length() == 1) {
sb.append(s);
Log.e("StringBuilderLength", "--------3" + sb.length());
edt_3.clearFocus();
edt_4.requestFocus();
edt_4.setCursorVisible(true);
}
if (sb.length() ==0&&edt_4.length() == 1) {
sb.append(s);
Log.e("StringBuilderLength", "--------4" + sb.length());
edt_4.requestFocus();
edt_4.setCursorVisible(true);
}
}
#Override
public void afterTextChanged(Editable s) {
}
}

The main problem was with your if's logic.
Try with this:
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (edt_1.getText().toString().equals(s.toString())) {
Log.e("StringBuilderLength", "--------");
edt_1.clearFocus();
edt_2.requestFocus();
edt_2.setCursorVisible(true);
}
if (edt_2.getText().toString().equals(s.toString())) {
Log.e("StringBuilderLength", "--------2");
edt_2.clearFocus();
edt_3.requestFocus();
edt_3.setCursorVisible(true);
}
if (edt_3.getText().toString().equals(s.toString())) {
Log.e("StringBuilderLength", "--------3");
edt_3.clearFocus();
edt_4.requestFocus();
edt_4.setCursorVisible(true);
}
if (edt_4.getText().toString().equals(s.toString())) {
Log.e("StringBuilderLength", "--------4");
edt_4.requestFocus();
edt_4.setCursorVisible(true);
}
}

Related

Android:Disable Button on app launch enable only when Edit text is filled

I want to Android:Disable Button on app launch enable only when Edit text is filled. help needed. thanks in advance. I am Android Beginner.
Tried using TextWatcher. app is running fine when i don't add this feature.
please suggest how i can implement the TextWatcher part. rest of the app is working fine.
here is the code:
package com.example.intentopennextscreen;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
public TextView editText1;
public Button submit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//editText1.addTextChangedListener(ebert);
}
/*TextWatcher ebert = new TextWatcher(){
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String message = editText1.getText().toString().trim();
submit.setEnabled(!message.isEmpty());
}
#Override
public void afterTextChanged(Editable s) {
}
}; */
/* Called when the user taps the Send button */
public void sendMessage(View view) {
/* Creating instance of Intent object */
Intent intent = new Intent(this, DisplayMessageActivity.class);
/* finding the user-input provided in the editText as an object */
EditText editText1 = (EditText) findViewById(R.id.editText1);
/*Finding Button Element*/
Button submit = (Button) findViewById(R.id.button);
/* converting the user-input of editText as String */
String message = editText1.getText().toString().trim();
/* sending the message to other activity as Key-Value Pair */
intent.putExtra("Value", message);
/* starting the intent */
startActivity(intent);
}
}
You can move the button manipulation inside afterTextChanged
#Override
public void afterTextChanged(Editable s) {
submit.setEnabled(!s.isEmpty());
}
Try this below code,
Add this in your onCreate() method
submit.setEnabled(false);
editText1.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
if (editText1.getText().length() > 0) {
submit.setEnabled(true);
} else {
submit.setEnabled(false);
}
}
});

edittext cursor remains at starting position

I have 3 editText fields of the type Number(Decimal). Two of which have onTextChanged listeners supposed to carry out mathematical functions when any integer is entered. My problem is the edittext fields with the listeners have the cursors stuck on the starting position i.e if i try to type 25, it ends up being 52.
CODE:
package com.example.Prototype;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
public class productsFragmentTab extends Fragment {
Button newProduct, clear;
Spinner productList;
EditText quantity, unit, total;
String invoice_id;
ShowAlert alert = new ShowAlert();
int unitcost, totalcost;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.product_layout, container, false);
newProduct = (Button)rootView.findViewById(R.id.button4);
clear = (Button)rootView.findViewById(R.id.button5);
productList = (Spinner)rootView.findViewById(R.id.spinner2);
quantity = (EditText)rootView.findViewById(R.id.editText5);
unit = (EditText)rootView.findViewById(R.id.editText7);
total = (EditText)rootView.findViewById(R.id.editText8);
invoice_id = GlobalApp.data().id;
newProduct.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Save current records in array
//Clear all textboxes
}
});
clear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//clear all textboxes
}
});
unit.addTextChangedListener(new TextWatcher() {
boolean isChangingByCode = false;
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (isChangingByCode) {
return;
}
String qty = quantity.getText().toString();
if (qty.matches("")) {
} else {
totalcost = Integer.parseInt(qty) * Integer.parseInt(unit.getText().toString());
isChangingByCode = true;
total.setText(Integer.toString(totalcost));
isChangingByCode = false;
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
total.addTextChangedListener(new TextWatcher() {
boolean isChangingByCode = false;
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (isChangingByCode) {
return;
}
String qty = quantity.getText().toString();
if (qty.matches("")) {
} else {
unitcost = Integer.parseInt(total.getText().toString()) / Integer.parseInt(qty);
isChangingByCode = true;
unit.setText(Integer.toString(unitcost));
isChangingByCode = false;
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
return rootView;
}
}
Use setSelection() method to set cursor position at last.
Add following code:
total.setText(Integer.toString(totalcost));
total.setSelection(total.getText().length());
in place of total.setText(Integer.toString(totalcost)); in onTextChanged() callback.
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (isChangingByCode) {
return;
}
String qty = quantity.getText().toString();
if (qty.matches("")) {
} else {
totalcost = Integer.parseInt(qty) * Integer.parseInt(unit.getText().toString());
isChangingByCode = true;
total.setText(Integer.toString(totalcost));
total.setSelection(total.getText().length());
isChangingByCode = false;
}
}

use of TextWatcher() in android

i am building an app where if the user enters an "o" or "O" in the start of the edit text then the following should be executed:
replace the entered "o"or "O" with "P" at runtime in the EditText and set the TextView to "ORANGE".
Similarly, if the user enters "g" or "G" then the entered "g"or "G" should be replace with "P" at runtime in EditText and set the TextView to "Green".
and so on....
how can i accomplish this.I have use TextWatcher() but unable to achieve the goal.Help will be appreciated
package com.example.answer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class data extends Activity implements OnClickListener {
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
}
Button start, startFor;
EditText sendET;
TextView gotAnswer, result;
int flag = 1;
TextWatcher tt = null;
int i = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
final EditText sendET = (EditText) findViewById(R.id.sendet);
final TextView result = (TextView) findViewById(R.id.tv);
tt = new TextWatcher() {
public void afterTextChanged(Editable s) {
sendET.setSelection(s.length());
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (sendET.getText().toString() == "o") {
flag = 0;
}
sendET.removeTextChangedListener(tt);
if (flag == 0) {
sendET.setText(sendET.getText().toString()
.replaceAll("o", "P"));
result.setText("ORANGE");
}
sendET.addTextChangedListener(tt);
}
};
sendET.addTextChangedListener(tt);
}
private void initialize() {
start = (Button) findViewById(R.id.button1);
sendET = (EditText) findViewById(R.id.sendet);
start.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
String bread = sendET.getText().toString();
result = (TextView) findViewById(R.id.tv);
result.setText(bread);
/*
* Bundle basket = new Bundle(); basket.putString("key", bread);
* Intent a = new Intent(data.this, openedclass.class);
* a.putExtras(basket); startActivity(a); break;
*/
}
}
}
if (sendET.getText().toString() == "o")
This is wrong. It's not how you check string equality in Java. Correct usage:
if(sendET.getText().toString().equals("o"))
Also, you remove your listener and add it again. I don't know your app logic, but I guess you don't want addTextChangedListener:
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (sendET.getText().toString() == "o") {
flag = 0;
}
sendET.removeTextChangedListener(tt); // REMOVING
if (flag == 0) {
sendET.setText(sendET.getText().toString()
.replaceAll("o", "P"));
result.setText("ORANGE");
}
sendET.addTextChangedListener(tt); // ADDING AGAIN
}

how to put 2 edit text as textwatch

if I would like to put 2 Edit text as text watch,
how can I put it?
both of them are linked.
Such as if edittext 1 + edittext 2 = ....
meaning
if I add edittext 1 and edittext 2 together,
the outcome wil show it the textviewer
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
Button saveButton = null;
EditText dateEdit;
EditText priceEdit;
EditText pumpEdit;
TextView costView;
EditText odometerEdit;
TextView fconView;
TextWatcher textWatcher;
String priceEditStr ="",pumpEditStr="";
int result;
public boolean isNumeric(String str)
{
return str.matches("-?\\d+(\\.\\d+)?");
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle bundle = getIntent().getExtras();
float price = Float.parseFloat(bundle.getString("priceEdit"));
float pump = Float.parseFloat(bundle.getString("pumpEdit"));
costView = (TextView)findViewById(R.id.tcost);
dateEdit = (EditText)findViewById(R.id.date);
priceEdit = (EditText)findViewById(R.id.fuelprice);
pumpEdit = (EditText)findViewById(R.id.fuelpump);
priceEdit.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void afterTextChanged(Editable editable) {
//here, after we introduced something in the EditText we get the string from it
if(!priceEdit.getText().toString().trim().equalsIgnoreCase("") && !priceEdit.getText().toString().trim().equalsIgnoreCase(null))
priceEditStr = priceEdit.getText().toString().trim();
if(!pumpEdit.getText().toString().trim().equalsIgnoreCase("") && !pumpEdit.getText().toString().trim().equalsIgnoreCase(null))
pumpEditStr = pumpEdit.getText().toString().trim();
if(!priceEdit.getText().toString().trim().equalsIgnoreCase("") && !pumpEdit.getText().toString().trim().equalsIgnoreCase(""))
{
result = Integer.parseInt(priceEditStr) * Integer.parseInt(pumpEditStr);
costView.setText(" "+result);
}
}
});
pumpEdit.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void afterTextChanged(Editable editable) {
//here, after we introduced something in the EditText we get the string from it
if(!priceEdit.getText().toString().trim().equalsIgnoreCase(""))
priceEditStr = priceEdit.getText().toString().trim();
if(!pumpEdit.getText().toString().trim().equalsIgnoreCase(""))
pumpEditStr = pumpEdit.getText().toString().trim();
if(!priceEdit.getText().toString().trim().equalsIgnoreCase("") && !pumpEdit.getText().toString().trim().equalsIgnoreCase(""))
{
result = Integer.parseInt(priceEditStr) * Integer.parseInt(pumpEditStr);
costView.setText(" "+result);
}
}
});
saveButton = (Button) findViewById(R.id.saveBTN);
saveButton.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
DBAdapter dbAdaptor = new DBAdapter(getApplicationContext());
try
{
dbAdaptor.open();
String date = dateEdit.getText().toString();
String price = priceEdit.getText().toString();
String pump = pumpEdit.getText().toString();
String cost = Float.toString(a);
String odometer = odometerEdit.getText().toString();
String fcon = fconView.getText().toString();
dbAdaptor.insertLog(date, price, pump, cost, odometer, fcon);
}
catch(Exception e){
Log.d("Fuel Log", e.getMessage());
}
finally
{
if(dbAdaptor != null)
dbAdaptor.close();
}
}
});
}//oncreate
}//main
Solved::
import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;
public class EditResult extends Activity {
//TextWatcher textWatcher; No need
EditText priceEdit,pumpEdit;
TextView costView;
String priceEditStr="",pumpEditStr="";
int result;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_sample);
priceEdit=(EditText) findViewById(R.id.editText1);
pumpEdit=(EditText) findViewById(R.id.editText2);
costView =(TextView) findViewById(R.id.textView1);
priceEdit.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void afterTextChanged(Editable editable) {
//here, after we introduced something in the EditText we get the string from it
if(!priceEdit.getText().toString().trim().equalsIgnoreCase("") && !priceEdit.getText().toString().trim().equalsIgnoreCase(null))
priceEditStr = priceEdit.getText().toString().trim();
if(!pumpEdit.getText().toString().trim().equalsIgnoreCase("") && !pumpEdit.getText().toString().trim().equalsIgnoreCase(null))
pumpEditStr = pumpEdit.getText().toString().trim();
if(!priceEdit.getText().toString().trim().equalsIgnoreCase("") && !pumpEdit.getText().toString().trim().equalsIgnoreCase(""))
{
result = Integer.parseInt(priceEditStr) * Integer.parseInt(pumpEditStr);
costView.setText(" "+result);
}
}
});
pumpEdit.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void afterTextChanged(Editable editable) {
//here, after we introduced something in the EditText we get the string from it
if(!priceEdit.getText().toString().trim().equalsIgnoreCase(""))
priceEditStr = priceEdit.getText().toString().trim();
if(!pumpEdit.getText().toString().trim().equalsIgnoreCase(""))
pumpEditStr = pumpEdit.getText().toString().trim();
if(!priceEdit.getText().toString().trim().equalsIgnoreCase("") && !pumpEdit.getText().toString().trim().equalsIgnoreCase(""))
{
result = Integer.parseInt(priceEditStr) * Integer.parseInt(pumpEditStr);
costView.setText(" "+result);
}
}
});
}
}
Give me your feedback
You have to do something like this in each edit text view's TextWatcher
public void afterTextChanged(Editable s) {
txtViewTotalPrice.setText(String.valueOf(Double.parseDouble(s.toString()) * Double.parseDouble(edittext_2.getText().toString())));
}
Create one textwatcher like this:
TextWatcher tw=new TextWatcher(){
priceEdit.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s)
{
}
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
public void onTextChanged(CharSequence s, int start, int before, int count)
{
TextView costView = (TextView)findViewById(R.id.tcost);
costView.setText(String.valueOf(Double.parseDouble(pumpEdit.getText().toString()) * Double.parseDouble(priceEdit.getText().toString())));
}
}
and add this to both edit text:
pumpEdit.addTextChangedListener(tw);
priceEdit.addTextChangedListener(tw);

Can't find the symbol addTextChangedListener

I am having issues with addTextChangedListener. I have 2 edittext (edittext1 and edittext2) that when the text (in this case numbers) are added or changed I need it to run a calculation and output it to a textview called resultstext.
I have found many examples and have modified them to do what I need them to do, but when I call the editText1.addTextChangedListener.inputTextwatcher; it tells me it cant find the symbol addTextChangedListener. Below is the code, how to straighten this out?
package com.example.MyProject;
import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.view.FocusFinder;
import android.view.Menu;
import android.widget.EditText;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private android.widget.EditText editText1;
private android.widget.EditText editText2;
private android.widget.TextView resultsText;
private void onItemSelected() {
editText1 = (EditText)findViewById(R.id.textView13);
editText2 = (EditText)findViewById(R.id.textView15);
resultsText = (android.widget.TextView)findViewById(R.id.textView16);
}
android.text.TextWatcher inputTextWatcher = new android.text.TextWatcher()
{
public void afterTextChanged(android.text.Editable s) {
calculateResult();
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){}
};
editText1.addTextChangedListener.inputTextWatcher;
editText2.addTextChangedListener.inputTextWatcher;
private void calculateResult() throws NumberFormatException {
Editable editableValue1 = editText1.getText(),
editableValue2 = editText2.getText();
double value1 = 0.0,
value2 = 0.0,
result;
if (editableValue1 != null)
value1 = Double.parseDouble(editableValue1.toString());
if (editableValue2 != null)
value2 = Double.parseDouble(editableValue2.toString());
result = ((0.5 * value1) / 6.1) * value2;
resultsText.setText(String.valueOf(result));
}
}
Placement of some of your code was wrong. I have attempted to fix it. See if the following works for you:
package com.example.MyProject;
import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.view.FocusFinder;
import android.view.Menu;
import android.widget.EditText;
public class MainActivity extends Activity {
private android.widget.EditText editText1;
private android.widget.EditText editText2;
private android.widget.TextView resultsText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText1 = (EditText)findViewById(R.id.textView13);
editText2 = (EditText)findViewById(R.id.textView15);
resultsText = (android.widget.TextView)findViewById(R.id.textView16);
android.text.TextWatcher inputTextWatcher = new android.text.TextWatcher() {
public void afterTextChanged(android.text.Editable s) {
calculateResult();
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){}
};
editText1.addTextChangedListener(inputTextWatcher);
editText2.addTextChangedListener(inputTextWatcher);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void calculateResult() throws NumberFormatException {
Editable editableValue1 = editText1.getText();
Editable editableValue2 = editText2.getText();
double value1 = 0.0,
value2 = 0.0,
result;
if (editableValue1 != null)
value1 = Double.parseDouble(editableValue1.toString());
if (editableValue2 != null)
value2 = Double.parseDouble(editableValue2.toString());
result = ((0.5 * value1) / 6.1) * value2;
resultsText.setText(String.valueOf(result));
}
}
try to use
private TextWatcher watcher = new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
//your code here
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
};
Then add watcher to your edittext
youredittext.addTextChangedListener(watcher);

Categories

Resources