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.
Related
being new in Android I'm trying to just simply add (sum) adjusted values from two SeekBars using setOnSeekBarChangeListener or using Button.
The project below is work fine but the sum still is 0. I can't find a solution. Thanks for your help. Usually, I'm using Spring Boot and Vaadin and it is so simple :)
private int progress1 = 0;
private int progress2 = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textViewMain;
Button button;
final TextView textViewMain_ = findViewById(R.id.textViewMain);
Button buttonSum = findViewById(R.id.button);
SeekBar seekBar = findViewById(R.id.seekBar);
final TextView textViewSeekBar = findViewById(R.id.textView2);
SeekBar seekBar2 = findViewById(R.id.seekBar2);
final TextView textViewSeekBar2 = findViewById(R.id.textView3);
seekBar.setMax(600);
seekBar.setProgress(5);
seekBar2.setMax(900);
seekBar2.setProgress(5);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public int progress1 = 0;
#Override
public void onProgressChanged(SeekBar seekBar, int progress1, boolean fromUser) {
this.progress1 = progress1;
// getUpdateTotal();
//textViewSeekBar.setText(Integer.parseInt(String.valueOf(seekBar)));//todo not work
textViewSeekBar.setText(Integer.toString(progress1));
Log.i("Seeker1: ", Integer.toString(progress1));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
seekBar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public int progress2 = 0;
#Override
public void onProgressChanged(SeekBar seekBar, int progress2, boolean fromUser) {
this.progress2 = progress2;
//getUpdateTotal();
textViewSeekBar2.setText(Integer.toString(progress2));
Log.i("Seeker2: ", Integer.toString(progress2));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
buttonSum.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getSummaryTwoSeekBarValue(progress1, progress2);
Log.i("", getSummaryTwoSeekBarValue(progress1, progress2));
}
});
}
public String getSummaryTwoSeekBarValue(int progress1, int progress2) {
final TextView textViewMain_ = findViewById(R.id.textViewMain);
int total = progress1 + progress2;
String totalString = String.valueOf(total);
Log.i("", totalString);
textViewMain_.setText(String.valueOf(totalString));
return totalString;
}
}```
[enter image description here][1]
[1]: https://i.stack.imgur.com/HDx12.png
You are defining progress1 and progress2 in both the main class and in the listener, so the class variable will keep being 0, you have to use (your class name).this.progress1 = progress1; and same in the second seek bar to modify the main class one instead of the listener one
private int progress1;
private int progress2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textViewMain;
final TextView textViewMain_ = findViewById(R.id.textViewMain);
SeekBar seekBar = findViewById(R.id.seekBar);
final TextView textViewSeekBar = findViewById(R.id.textView2);
SeekBar seekBar2 = findViewById(R.id.seekBar2);
final TextView textViewSeekBar2 = findViewById(R.id.textView3);
seekBar.setMax(600);
seekBar.setProgress(5);
seekBar2.setMax(900);
seekBar2.setProgress(5);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress1, boolean fromUser) {
getSummaryTwoSeekBarValue(progress1,progress2);
textViewSeekBar.setText(Integer.toString(progress1));
Log.i("Seeker1: ", Integer.toString(progress1));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
seekBar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress2, boolean fromUser) {
getSummaryTwoSeekBarValue(progress1,progress2);
textViewSeekBar2.setText(Integer.toString(progress2));
Log.i("Seeker2: ", Integer.toString(progress2));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
public String getSummaryTwoSeekBarValue(int progress1, int progress2) {//int progress1, int progress2
this.progress1=progress1;
this.progress2=progress2;
final TextView textViewMain_ = findViewById(R.id.textViewMain);
int total = progress1 + progress2;
String totalString = String.valueOf(total);
Log.i("", totalString);
textViewMain_.setText(String.valueOf(totalString));
return totalString;
}
}```
I have 12 seekbars in an activity. Each seekbar needs a listener. At the moment I am manually setting a listener for each seekbar, this doesnt seem particularly efficient.
Can anyone recommend a more efficient practice to set the seekbars???
Try something like this:
public class MainActivity extends AppCompatActivity {
SeekBar seekBar1;
SeekBar seekBar2;
SeekBar.OnSeekBarChangeListener mlistener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekBar1 = findViewById(R.id.seekBar_1);
seekBar2 = findViewById(R.id.seekBar_2);
mlistener = new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
switch (seekBar.getId()) {
case R.id.seekBar_1:
//do something
break;
case R.id.seekBar_2:
//do something else
break;
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
};
seekBar1.setOnSeekBarChangeListener(mlistener);
seekBar2.setOnSeekBarChangeListener(mlistener);
}
If your SeekBars all require different interaction logic then obviously you will need to provide unique code to each of the objects. However, you can re-use listeners if each SeekBar is doing the same thing. For example:
public class SeekBarTest extends Activity{
private Context myContext;
public SeekBarTest(Context c){
myContext = c;
}
private void initialize(){
SeekBar seekbar1 = new SeekBar(myContext);
SeekBar seekbar2 = new SeekBar(myContext);
SeekBar seekbar3 = new SeekBar(myContext);
SeekBar.OnSeekBarChangeListener myListener = new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
//logic
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
//logic
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
//logic
}
};
seekbar1.setOnSeekBarChangeListener(myListener);
seekbar2.setOnSeekBarChangeListener(myListener);
seekbar3.setOnSeekBarChangeListener(myListener);
}
}
Instead of creating switch in onProgressChanged method you can send variables to SeekBar initiating method.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_options);
SeekBar volBar = initiateSeekBar(R.id.volume,R.id.volumeValue,0,1,"%");
SeekBar playSpeedBar = initiateSeekBar(R.id.autoPlay,R.id.autoPlayValue,100, 100,"ms");
}
private SeekBar initiateSeekBar(int barId, int textId, final int min, final int step, final String unit) {
SeekBar bar = findViewById(barId);
final TextView text = findViewById(textId);
bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
int value = min + progress * step;
text.setText(value + " " + unit);
autoPlayDelayTime = value;
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
return bar;
}
You can just make a reference equality, like this:
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
seekBar?. let {
if (this.seekBar1 === it) {
...
}
if (this.seekBar2 === it) {
...
}}
...
}
and call outside:
seekBar1.setOnSeekBarChangeListener(this)
seekBar2.setOnSeekBarChangeListener(this)
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);
}
}
});
}
I have two edittexts for username and password ,I use the seterror method to show an error when both of them are empty.The problem is when the error message pops up in the second field(the password field) , part of the message is missing.This bug is there only on older devices.How do i ensure that the error does not happen in older devices.
My code:
public class SignInPage extends Activity {
EditText txtusername,txtpassword;
Button btnlogin;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.signinpage);
txtusername=(EditText) findViewById(R.id.txtusername);
txtpassword=(EditText) findViewById(R.id.txtpassword);
btnlogin=(Button) findViewById(R.id.btnlogin);
btnlogin.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(txtusername.getText().toString().trim().equals(""))
{
txtusername.setError("Username is mandatory");
txtusername.requestFocus();
}
if(txtpassword.getText().toString().trim().equals(""))
{
txtpassword.setError("Password is mandatory");
txtpassword.requestFocus();
}
else
{
Toast.makeText(getApplicationContext(),"Checking with server",Toast.LENGTH_LONG).show();
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Try this it might help you
fname = FirstName.getText().toString().trim();
if(fname.isEmpty()) //&& fname.matches("[a-zA-Z ]+"))
{
FirstName.requestFocus();
FirstName.setError(Html.fromHtml("<font color='red'>Please enter the First name</font>"));
}
TextWatcher textWatcher = new TextWatcher() {
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
{ }
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
{ }
#Override
public void afterTextChanged(Editable arg0)
{
if(arg0.toString().isEmpty())
{
return;
}
if (FirstName.getText() == arg0) {
Validate(FirstName);
FirstName.addTextChangedListener(textWatcher);
public void Validate(EditText et) {
et.setError(null);
}
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();
}