Hide an android ListView until search string is entered - android

I've followed this tutorial http://www.androidpeople.com/android-listview-searchbox-sort-items and I have the search working on the list. The only change I'd like to make is to have the list view hidden initially and the results only appearing when the user starts typing. Is there a way to do this?
EDIT: Added code.
package com.example.testapp;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
private ListView lv1;
private EditText ed;
private String lv_arr[]= {"Android","Cupcake","Donut","Eclairs","AndroidPeople","Froyo",};
private ArrayList<String> arr_sort= new ArrayList<String>();
int textlength=0;
#Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.activity_main);
lv1=(ListView)findViewById(R.id.ListView);
ed=(EditText)findViewById(R.id.EditText01);
// By using setAdpater method in listview we an add string array in list.
lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));
ed.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
if(lv1.getVisibility() != View.VISIBLE)
lv1.setVisibility(View.VISIBLE);
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
textlength=ed.getText().length();
arr_sort.clear();
for(int i=0;i<lv_arr.length;i++)
{
if(textlength<=lv_arr[i].length())
{
if(ed.getText().toString().equalsIgnoreCase((String) lv_arr[i].subSequence(0, textlength)))
{
arr_sort.add(lv_arr[i]);
}
}
}
lv1.setAdapter(new ArrayAdapter<String> (MainActivity.this,android.R.layout.simple_list_item_1 , arr_sort));
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}

I would do it like this:
EditText etSearch = (EditText) getView().findViewById(R.id.etSearch);
etSearch.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {
if(view.getVisibility() != View.Visible)
view.setVisiblity(View.Visible);
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){}
});

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

Android - Store Spans in SQLiteDatabase

I'm making an app in which I'm using Spannble class to create a highlighter mechanism in an EditText, which works fine. Now, I want to store the span position(s) in a SQLiteDatabase, so that I can reload them and have the Spanned Text to show it in a TextView.
Here's my TextView alongwith the highlighter mechanism -
package com.Swap.RR;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.text.Editable;
import android.text.Spannable;
import android.text.TextWatcher;
import android.text.style.BackgroundColorSpan;
import android.view.Menu;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ToggleButton;
public class Add_New_Note extends Activity {
private EditText note;
private int mStart;
private Typeface Roboto_lt;
private int end;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add__new__note);
note = (EditText) findViewById(R.id.notecontent);
ToggleButton highlighter = (ToggleButton) findViewById(R.id.highlightToggle);
Roboto_lt = Typeface.createFromAsset(getAssets(), "Roboto-Light.ttf");
note.setTypeface(Roboto_lt);
mStart = -1;
final TextWatcher highlightWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count)
{
if(mStart > 0)
{
end = note.getText().length();
note.getText().setSpan(new BackgroundColorSpan(getResources().getColor(R.color.highlighter_green)), mStart, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
public void afterTextChanged(Editable s) {
}
};
highlighter.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton button, boolean isChecked) {
if (isChecked == true) {
//start highlighting when the ToggleButton is ON
if(mStart == -1) mStart = note.getText().length();
else mStart = -1;
mStart = note.getText().length();
note.addTextChangedListener(highlightWatcher);
} else {
//stop highlighting when the ToggleButton is OFF
note.removeTextChangedListener(highlightWatcher);
}
}
});
}
public void noteDone(View v) {
String noteContent = note.getText().toString();
Intent i = new Intent("com.Swap.Notes_page");
i.putExtra("NOTE", noteContent);
setResult(RESULT_OK, i);
finish();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_add__new__note, menu);
return true;
}
}
Please help me with some suggestions on how to do it. Thanks!
Your best bet is to use Html.toHtml() to convert the Spannable into HTML. You can then use Html.fromHtml() to convert the HTML back into a Spannable when you query the database later on. You will want to do adequate testing, though, as toHtml() and fromHtml() are not guaranteed to support the same HTML tags.

Filtering SimpleCursorAdapter

This code is working properly by showing all the data in database but i have a problem in filtering. I try many code but nothing works , can someone help me out ? thanks
package com.example.dictionary;
import java.util.List;
import com.example.dictionary.R;
import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.support.v4.widget.SimpleCursorAdapter;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
public class MainActivity extends Activity{
Cursor cursor;
ListView listView;
SimpleCursorAdapter adapter;
Button back, clear;
List<String> items;
String get;
EditText et;
int textlength = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) findViewById(R.id.editText1);
Opening my database and getting all data
Historydb db = new Historydb(this);
db.open();
cursor = db.getword();
startManagingCursor(cursor);
cursor.moveToFirst();
adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, cursor,
new String[] { "word" }, new int[] { android.R.id.text1 });
listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(adapter);
I set addtextchangelistener in my edittext.
et.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
// Abstract Method of TextWatcher Interface.
}
public void beforeTextChanged(CharSequence s, int start, int count,int after) {
// Abstract Method of TextWatcher Interface.
}
public void onTextChanged(CharSequence s, int start, int before,int count) {
adapter.getFilter().filter(s);
}});
}
}
it is not working because adapter.getfilter().filter(cs); does not work directly for SimpleCursorAdapter.
you have to use adapter.setFilterQueryProvider first for SimpleCursorAdapter.
here is the complete description: ListView, SimpleCursorAdapter, an an EditText filter -- why won't it do anything?
and this: Using an EditText to filter a SimpleCursorAdapter-backed ListView
Try this....
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
((SimpleAdapter) YourActivity.this.adapter).getFilter().filter(cs);
}

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

How to implement action listener in ListView?

Basically, I want to create a ListView with a bunch of items on it, and whenever the user click any item, it will open a new Activity according to which item he clicks. PLease, help me I would really appreciate it a lot.
Here is my my code:
package com.hipeople;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
public class char1 extends Activity {
/** Called when the activity is first created. */
private ListView lv1;
private EditText ed;
private String lv_arr[]={"Android","Cupcake","Donut","Eclairs","AndroidPeople","Froyo",};
private ArrayList<String> arr_sort= new ArrayList<String>();
int textlength=0;
#Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
lv1=(ListView)findViewById(R.id.ListView01);
ed=(EditText)findViewById(R.id.EditText01);
lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));
ed.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) {
textlength=ed.getText().length();
arr_sort.clear();
for(int i=0;i<lv_arr.length;i++)
{
if(textlength<=lv_arr[i].length())
{
if(ed.getText().toString().equalsIgnoreCase((String) lv_arr[i].subSequence(0, textlength)))
{
arr_sort.add(lv_arr[i]);
}
}
}
}
});
}
}
Try this,
lv1.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3)
{
String str = ((TextView) arg1).getText().toString();
Toast.makeText(getBaseContext(),str, Toast.LENGTH_LONG).show();
Intent intent = new Intent(getBaseContext(),your_new_Intent.class);
intent.putExtra("list_view_value", str);
startActivity(intent);
}
});

Categories

Resources