EditText get value to pass Textview - java

I am working on an activity in which I have taken edittext and textview. I have to get integer value from edittext and set it on textview.
I have to take 10-digit number(mobile no) on edittext and as soon as the edittext column gets 10-digits,its automatically sets 10-digit number on textview within the same activity.
I have implemented in this manner
Getting value on edittext using
Integer.parseInt(edittext.getText().toString());
in a String str.
Applying condition to it (str.length == 10) and getting it on textview.
As the above process is not working so try to help me friends.
I am working on this code:
public class MobileNo extends Activity {
EditText et;
TextView tv;
int i;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
et = (EditText) findViewById(R.id.edit);
tv = (TextView) findViewById(R.id.text1);
et.getText().toString();
et.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable textVal) {
// TODO Auto-generated method stub
Editable inputNo = textVal;
if (inputNo.length() == 10) {
tv.setText(inputNo);
}
}
});
}
}

I would try:
#Override
public void afterTextChanged(Editable textVal) {
String inputNo = textVal.toString();
if (inputNo.length() == 10) {
tv.setText(inputNo);
}
}
If your phone number is formatted like "(800) 549-2992" or "800-549-2992" and you only wants the numbers then you'd have to do the following:
#Override
public void afterTextChanged(Editable textVal) {
// takes out all non-digits from string
String inputNo = textVal.toString().replaceAll("[^\\d]","");
if (inputNo.length() == 10) {
tv.setText(inputNo);
}
}

Related

Is it possible to have 3 EditTexts which change each other EditText which are interchangable between all 3?

This is in android studio.
So I have 3 edit texts which need to be interchangeable between all 3. They do math so for example let's say they are each labeled editText1, editText2, and editText3. If I were to type a number into editText1 and editText2 it should update editText3. If I were to type a number into editText2 and into editText3 it would update editText1. I then should be able to delete any data between any of the editTexts and change them on the fly without any EditText instances getting locked up. I have been able to change any of them on the get go, but I end up locking it up.
Would it be possible to make them interchangeable without locking at least one EditText up? Thanks
Here try something like that
public class watcher implements TextWatcher {
EditText changeAble;
EditText Effector;
public watcher(EditText otherEffector,EditText otherEditor){
this.Effector =otherEffector;
this.changeAble = otherEditor;
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//To not change the other editText in case of deleting
if(!s.toString().isEmpty()){
changeAble.setText(Integer.valueOf(s)+Integer.valueOf(Effector.getText().toString()));
}
}
#Override
public void afterTextChanged(Editable s) {
}
}
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText editText1 = findViewById(R.id.editText);
EditText editText2 = findViewById(R.id.editText2);
EditText editText3 = findViewById(R.id.editText3);
watcher watcher = new watcher(editText2,editText1);
editText3.addTextChangedListener(watcher);
editText2.addTextChangedListener(new watcher(editText3,editText1));
}
}
I'm not sure what do you mean by locking up the edittexts, but you could try to use TextWatcher to achieve that.
eg.
EditText editText1 = (EditText)findViewById(R.id.edittext1);
editText1.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//Add the logic necessary to achieve your requirements here
eg.
if(!editText2.getText().toString().isEmpty){
//Do your math and set the text to editext3
val value1 = Integer.parseInt(editText1.getText().toString());
val value2 = Integer.parseInt(editText2.getText().toString());
editText3.setText(value1 + value2)
}
}
});
and so on....

addTextChangedListener MoneyWatcher Only Working on Second List Entrance

When I enter text into the "edit" EditText field in the app, it doesn't format as per the MoneyTextWatcher, however the second entry I make does format correctly. Any ideas on what's going on?
I was thinking about rewriting this so that I could use private variables, but my app crashed every time I tried that.
public class addBills extends ListActivity {
ArrayList<String> list = new ArrayList<String>();
ArrayAdapter<String> adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_add_bills);
Button btn = (Button) findViewById(R.id.btnAdd);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
OnClickListener listener = new OnClickListener() {
#Override
public void onClick(View v) {
EditText edit = (EditText) findViewById(R.id.txtItem);
edit.addTextChangedListener(new MoneyTextWatcher(edit));
EditText billName = (EditText) findViewById(R.id.billName);
list.add(billName.getText().toString() + " " + edit.getText().toString());
edit.setText("");
adapter.notifyDataSetChanged();
}
};
/** Setting the event listener for the add button */
btn.setOnClickListener(listener);
/** Setting the adapter to the ListView */
setListAdapter(adapter);
}
public class MoneyTextWatcher implements TextWatcher {
private final WeakReference<EditText> editTextWeakReference;
public MoneyTextWatcher(EditText editText) {
editTextWeakReference = new WeakReference<EditText>(editText);
}
#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 editable) {
EditText editText = editTextWeakReference.get();
if (editText == null) return;
String s = editable.toString();
if (s.isEmpty()) return;
editText.removeTextChangedListener(this);
String cleanString = s.replaceAll("[$,.]", "");
BigDecimal parsed = new BigDecimal(cleanString).setScale(2, BigDecimal.ROUND_FLOOR).divide(new BigDecimal(100), BigDecimal.ROUND_FLOOR);
String formatted = NumberFormat.getCurrencyInstance().format(parsed);
editText.setText(formatted);
editText.setSelection(formatted.length());
editText.addTextChangedListener(this);
}
}
}
Remove these two lines from onCLick
EditText edit = (EditText) findViewById(R.id.txtItem);
edit.addTextChangedListener(new MoneyTextWatcher(edit));
And place these inside onCreate method but outside onClick

Restricting ParseQuery by user text input

I'm trying to write a restriction to my ParseQuery whereby the query only returns usernames that contain the first two characters input by my user into the AutoCompleteTextView. Can I use substring to find the first two characters of the string input into AutoCompleteTextView, then run a query based on that?
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.searchUserTextField);
String input = textView.toString();
String recommendation = input.substring(0,2);
userQuery.whereStartsWith(ParseConstants.KEY_USERNAME, recommendation);
userQuery.findInBackground(new FindCallback<ParseUser>() {
...
I realize this isn't really how you'd do this, and I haven't set up handlers to generate new queries based on how the text changes, but I'm trying to figure this out in baby steps.
This is what I have in the onCreate() method of my Activity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from listview_main.xml
setContentView(R.layout.activity_search);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
final AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.searchUserTextField);
textView.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (count % 2 == 1) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
textView.showDropDown();
UserSuggestionQuery(textView);
}
}, 1000);
}
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
UserSuggestionQuery(textView);
}
public void afterTextChanged(Editable s) {
UserSuggestionQuery(textView);
}
});
textView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterview, View v,
int position, long arg3) {
// TODO Auto-generated method stub
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.searchUserTextField);
textView.setText(adapterview.getItemAtPosition(position)
.toString());
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
textView.setOnTouchListener(new View.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event){
UserSuggestionQuery(textView);
return false;
}
});
SubmitSearch();
// Execute RemoteDataTask AsyncTask
new RemoteDataTask().execute();
}
The following function appears in the above code. This is the actual query for the users input to the AutoCompleteTextView:
private void UserSuggestionQuery(AutoCompleteTextView textView) {
ParseQuery<ParseUser> userQuery = ParseUser.getQuery();
String input = textView.getText().toString();
System.out.println(input);
String recommendation = input.substring(0);
userQuery.whereStartsWith(ParseConstants.KEY_USERNAME, recommendation);
userQuery.findInBackground(new FindCallback<ParseUser>() {
public void done(final List<ParseUser> parseUsers, ParseException e) {
if (e == null) {
Log.d("users", "Retrieved " + parseUsers.size());
ParseUser[] data = parseUsers.toArray(new ParseUser[parseUsers.size()]);
String[] strings = new String[data.length];
for (int i = 0; i < data.length; i++) {
strings[i] = data[i].getString(ParseConstants.KEY_USERNAME).toLowerCase();
}
System.out.println(Arrays.toString(strings));
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_selectable_list_item, strings);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.searchUserTextField);
textView.showDropDown();
textView.setThreshold(1);
textView.setAdapter(adapter);
// Query based on text change
adapter.setNotifyOnChange(true);
}
}
});
}

Convert Editable to Edittext

I have set a tag to An Edittext variable.i want get that tag inside text TextWatcher afterTextChanged method.but there is a Editable parameter not Edittext.
here is my code.....
EditText filled = new EditText(DamageCount.this);
filled.setId(100);
filled.setTag(5);
and i want get that tag in
#Override
public void afterTextChanged(Editable s) {
}
this method
------Edited----------------
my whole java class
public class DamageCount extends Activity {
private Button button_next;
static TableLayout tl = null;
private DbWorker dbworker;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_damage_count);
DamageCount.this
.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
button_next = (Button) findViewById(R.id.button_next);
tl = (TableLayout) findViewById(R.id.show_competitor_product);
dbworker = new DbWorker(this);
Static_Values.arrayListdam_no = new ArrayList<DamegeItemSerialNoModel>();
button_next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
/*Intent new_one = new Intent(DamageCount.this,
Invoice_or_prev.class);
startActivity(new_one);
finish();*/
for(DamegeItemSerialNoModel dmmm :Static_Values.arrayListdam_no)
{
System.out.println("Item id is "+dmmm.getItem_id()+" serial no is "+dmmm.getSerial_no());
}
}
});
Cursor cur_products= dbworker.getAllProduct();
int i=0;
if(cur_products.moveToFirst())
{
do{
i++;
// Static_Values.item.put(, value)
add_tbl_row(i,cur_products.getString(3),cur_products.getString(1));
}
while(cur_products.moveToNext());
}
else {
}
cur_products.close();
}
public void add_tbl_row(int value, String name, String tag) {
Static_Values.damaged_id=tag;
TableRow new_row = new TableRow(DamageCount.this);
System.out.println(Static_Values.damaged_id+" id bbb");
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.ic_launcher);
new_row.addView(imageView);
TextView size = new TextView(DamageCount.this);
size.setId(1);
size.setText(name);
size.setPadding(5, 5, 5, 5);
new_row.addView(size);
EditText filled = new EditText(DamageCount.this);
filled.setId(100 * value);
filled.setTag(tag);
filled.setHint("Enter Damaged");
filled.setInputType(InputType.TYPE_CLASS_NUMBER);
new_row.addView(filled);
filled.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
// Toast.makeText(DamageCount.this, s.toString(), Toast.LENGTH_SHORT).show();
if(s.toString().equals("")==false)
{
final LinearLayout layout = new LinearLayout(DamageCount.this);
layout.setOrientation(LinearLayout.VERTICAL);
Button dialogButton = new Button(DamageCount.this);
dialogButton.setText("Add");
// dialogButton.setBackgroundColor(R.drawable.yellobutton);
Static_Values.num_of_serials=Integer.parseInt(s.toString());
for(int i=1;i<Integer.parseInt(s.toString())+1;i++)
{
EditText serial = new EditText(DamageCount.this);
serial.setId(33*i);
serial.setHint("Enter serial");
layout.addView(serial);
}
layout.addView(dialogButton);
final Dialog dialog = new Dialog(DamageCount.this);
dialog.setContentView(layout);
dialog.setTitle("Serial Numbers");
dialog.getWindow().setTitleColor(Color.BLUE);
// set the custom dialog components - text, image and button
// dialog.setContentView(filled);
// dialog.getOwnerActivity().setContentView(filled);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 1; i < Static_Values.num_of_serials+1; i++) {
View vv = dialog.findViewById(33*i);
EditText et=(EditText)vv;
System.out.println(et.getText().toString());
DamegeItemSerialNoModel dism = new DamegeItemSerialNoModel();
dism.setItem_id(Static_Values.damaged_id);
System.out.println(Static_Values.damaged_id);
dism.setSerial_no(et.getText().toString());
Static_Values.arrayListdam_no.add(dism);
}
Static_Values.num_of_serials = 0;
dialog.dismiss();
}
});
dialog.show();
}
}
});
tl.addView(new_row, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
}
Create a constructor in yout Textwatcher class and pass the editext to it.
public class MyTextWatcher implements TextWatcher
{
private transient EditText editText = null;
public MyTextWatcher(EditText editText)
{
super();
this.editText = editText;
}
#Override
public void afterTextChanged(Editable arg0)
{
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
{
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
{
// TODO Auto-generated method stub
}
}
If you want to convert Editable to simple text line (String), use this in your method:
#Override
public void afterTextChanged(Editable s) {
String str = s.toString();
}

Getting Data from EditText, RadioButton and Spinner

I search on many websites and many tutorials how can I get the data from editText and radioGroup and spinner DIRECTLY When the user input the data
I tried with this:
MyEditText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View view, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
{case1.setName(CaseName.getText().toString());
}
return true;
}
return false;
}
});
but it didn't take the data, I just use button to make that as shown below:
Test.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.cancel1:
try
{
// 1-Name
case1.setName(CaseName.getText().toString());
}catch(Exception e){
}
break;
}
}});
Can anyone explain what shall I use to save the data in Object(case1) directly when the user input it ?
editext.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// s is your text .
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
})
for Radio button
checkbox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks, depending on whether it's now checked
if (((CheckBox) v).isChecked()) {
Toast.makeText(HelloFormStuff.this, "Selected", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(HelloFormStuff.this, "Not selected", Toast.LENGTH_SHORT).show();
}
}
});

Categories

Resources