Convert Editable to Edittext - java

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();
}

Related

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

It doesn't work when i click on a button in MainActivity in order to show other activity

Please I'm trying to create an activity with 6 buttons, and 6 textviews. Note that the problem gone when I click the button because it works when I tried to comment on block my code and just let this:
super.onCreate(savedInstanceState);
setContentView(R.layout.tasbih);
Her is my activity if you understand were the problem remains please help me:
public class Tasbih extends AppCompatActivity {
private TextView textView, textView2, textView6;
private Button button7, button8, button9, button10, button11, button12;
private int j = 0;
private int k = 0;
private int l = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tasbih);
textView=(TextView) findViewById(R.id.textView);
textView2=(TextView) findViewById(R.id.textView2);
textView6=(TextView) findViewById(R.id.textView6);
button7=(Button)findViewById(R.id.button7);
button8=(Button)findViewById(R.id.button8);
button9=(Button)findViewById(R.id.button9);
button10=(Button)findViewById(R.id.button10);
button11=(Button)findViewById(R.id.button11);
button12=(Button)findViewById(R.id.button12);
button7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
j++;
textView.setText(j);
}
}) ;
button8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
j=0;
textView.setText(j);
}
});
button9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
k++;
textView2.setText(k);
}
});
button10.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
k=0;
textView2.setText(k);
}
}) ;
button11.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
l++;
textView6.setText(l);
}
}) ;
button12.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
l=0;
textView6.setText(l);
}
}) ;
final SharedPreferences sp=
PreferenceManager.getDefaultSharedPreferences(this);
j = sp.getInt("j", 0);
k = sp.getInt("k", 0);
l = sp.getInt("l", 0);
textView.setText(j);
textView2.setText(k);
textView6.setText(l);
textView.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) {
SharedPreferences.Editor editor = sp.edit();
editor.putInt("j", j).apply();
}
});
textView2.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) {
SharedPreferences.Editor editor = sp.edit();
editor.putInt("k", k).apply();
}
});
textView6.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) {
SharedPreferences.Editor editor = sp.edit();
editor.putInt("l", l).apply();
}
});
}
#Override
protected void onPause() {
super.onPause();
/* SharedPreferences sp = getSharedPreferences("tasbih1", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putInt("j", j);
editor.putInt("k", k);
editor.putInt("l", l);
editor.commit();*/
}
}
Where is the problem ?!
You can open other activity from current activity by intent. Like this.
You can add this line in your button click listner. Replace OtherActivity.class name to the class name of your other activity.
startActivity(new Intent(Tasbih.this,OtherActivity.class));

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);
}
}
});
}

EditText get value to pass Textview

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);
}
}

EditText search item in listview doesn't work

Hi guys i'm implementing a edittext in my listview to filter the items. I'm following this example, but nothing happen. It doesn't work and when i try to filter it doesn't do what i want.. This is my part of code of edittext:
ListView lv = (ListView) findViewById(android.R.id.list);
lv.setTextFilterEnabled(true);
lv.setAdapter(listadaptor);
inputSearch.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
MainActivity.this.listadaptor.getFilter().filter(cs);
}
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
};
My listview is a list that show all application installed in the device.. I have this to load the list:
private class LoadApplications extends AsyncTask<Void, Void, Void> {
public ProgressDialog progress = null;
#Override
protected Void doInBackground(Void... params) {
applist = checkForLaunchIntent(packageManager.getInstalledApplications(PackageManager.GET_META_DATA));
listadaptor = new ApplicationAdapter(MainActivity.this,R.layout.snippet_list_row, applist);
return null;
}
#Override
protected void onCancelled() {
super.onCancelled();
}
protected void onDestroy() {
if(progress!=null)
if(progress.isShowing()){
progress.dismiss();
}
}
#Override
protected void onPostExecute(Void result) {
setListAdapter(listadaptor);
progress.dismiss();
super.onPostExecute(result);
}
#Override
protected void onPreExecute() {
progress = ProgressDialog.show(MainActivity.this, null,
"Caricamento applicazioni in corso...");
super.onPreExecute();
}
#Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
so listadaptor is my adapter.. Someone can help me? Thanks
Try this one ..
ArrayList<String> type_name_copy = new ArrayList<String>();
inputSearch.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 theWatchedText) {
ArrayList<String> type_name_filter = new ArrayList<String>();
String text = theWatchedText.toString();
for (int i = 0; i < array.size(); i++) {
if ((array.get(i).toLowerCase()).contains(text
.toLowerCase())) {
type_name_filter.add(array.get(i));
}
}
type_name_copy = type_name_filter;
listUpdate(type_name_copy);
}
});
}
public void listUpdate(ArrayList<String> data) {
lv.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, data));
}
}
I suggest you to use AutocompleteTextView which is Edittext with in-built Search functionality in android.

Categories

Resources