can anyone help me to modify this code so I can get a Highlighted search result in ListView. I'm still a beginner in this so I don't know anymore how to do it. the search result is already displayed but i need to make it to highlight a spesific character or word.
can anyone out there help me please, i'm really stuck.
and I'm still using Eclipse IDE, cuz actually this is my old project that i made before google changing the official IDE for android to Android Studio. and this project never complete since then.
What I have tried:
package com.artstudio.testhighlight;
import java.util.Locale;
import com.artstudio.testhighlight.DatabaseSong;
import android.os.Bundle;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.content.Context;
import android.content.res.ColorStateList;
import android.text.Editable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextWatcher;
import android.text.style.TextAppearanceSpan;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class MainActivity extends Activity implements OnItemClickListener, TextWatcher {
private ListView daftarLagu;
private String[] tampungLaguLirik = null;
protected static String strSearchLyric = null;
private String mSearchText;
private SQLiteDatabase db = null;
private Cursor LaguCursor = null;
private DatabaseSong datalagu = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
datalagu = new DatabaseSong(this);
db = datalagu.openDataBase();
db = datalagu.getReadableDatabase();
datalagu.onCreate(db);
setContentView(R.layout.activity_search);
daftarLagu = (ListView)findViewById(R.id.listIndexLagu);
daftarLagu.setOnItemClickListener(this);
TextView text = (TextView) findViewById(R.id.pencarian);
text.setText("PENCARIAN LIRIK LAGU");
Button nomorBtn = (Button) findViewById(R.id.btnSwitchPencarian);
nomorBtn.setText("CARI NOMOR, JUDUL, PENGARANG");
nomorBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
db.close();
}
});
EditText textCari = (EditText)findViewById(R.id.editTextcari);
textCari.setHint("Masukan Potongan Lirik Lagu");
textCari.addTextChangedListener(this);
}
public void onItemClick(AdapterView<?> arg0, View arg1, int posisi, long arg3) {
Toast.makeText(MainActivity.this, "ini pas saya click", Toast.LENGTH_SHORT).show();
cariTampilView(""+tampungLaguLirik[posisi]);
posisi = 0;
}
public void cariTampilView(String cari){
LaguCursor = null;
LaguCursor = db.rawQuery("SELECT lirik, nomor||'. '||judul "
+ "FROM tbl_Lagusion WHERE (UPPER('\n\n___ '||nomor||'. '||judul)||'\n\n'||lirik) = '"+ cari +"' ", null);
if (LaguCursor.moveToFirst()) {
strSearchLyric = null;
strSearchLyric = LaguCursor.getString(0);
Toast.makeText(MainActivity.this, LaguCursor.getString(1).toString(), Toast.LENGTH_SHORT).show();
}
if (strSearchLyric.equals("")) {
strSearchLyric = "Terjemahan Not Found";
Toast.makeText(MainActivity.this, "Pencarian tidak ditemukan........!", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, "Sadisssssss\n"+LaguCursor.getString(0).toString(), Toast.LENGTH_SHORT).show();
}
}
void tampilLagu(){
strSearchLyric = null;
EditText text=(EditText)findViewById(R.id.editTextcari);
text.setTextColor(Color.parseColor("#0066FF"));
String value = text.getText().toString();
int length = text.length();
value = value.trim();
value = value.replaceAll("'","’");
if (length == 0){
value = null;
}
int Hitung =0;
LaguCursor = db.rawQuery("SELECT (UPPER('\n\n___ '||nomor||'. '||judul)||'\n\n'||lirik) FROM tbl_Lagusion WHERE lirik LIKE '%"+value+"%' ", null);
tampungLaguLirik = new String[LaguCursor.getCount()];
LaguCursor.moveToFirst();
Toast.makeText(MainActivity.this, "klot "+LaguCursor.getCount(), Toast.LENGTH_SHORT).show();
for (int cc=0; cc < LaguCursor.getCount(); cc++)
{
Hitung = Hitung +1;
LaguCursor.moveToPosition(cc);
tampungLaguLirik[cc] = LaguCursor.getString(0).toString();
}
if(Hitung == 0){
daftarLagu = (ListView)findViewById(R.id.listIndexLagu);
daftarLagu.setFastScrollEnabled(true);
daftarLagu.setBackgroundColor(Color.TRANSPARENT);
daftarLagu.setAnimationCacheEnabled(true);
daftarLagu.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, tampungLaguLirik));
daftarLagu.setSelected(true);
}else{
daftarLagu = (ListView)findViewById(R.id.listIndexLagu);
daftarLagu.setFastScrollEnabled(true);
daftarLagu.setBackgroundColor(Color.TRANSPARENT);
daftarLagu.setAnimationCacheEnabled(true);
daftarLagu.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, tampungLaguLirik));
daftarLagu.setSelected(true);
}
}
private String getItem(int position) {
return null;
}
public void onBackPressed() {
strSearchLyric = null;
db.close();
finish();
}
@Override
public void afterTextChanged(Editable s) {
tampilLagu();
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
View v = getCurrentFocus();
boolean ret = super.dispatchTouchEvent(event);
if (v instanceof EditText) {
View w = getCurrentFocus();
int scrcoords[] = new int[2];
w.getLocationOnScreen(scrcoords);
float x = event.getRawX() + w.getLeft() - scrcoords[0];
float y = event.getRawY() + w.getTop() - scrcoords[1];
if (event.getAction() == MotionEvent.ACTION_UP && (x < w.getLeft() || x >= w.getRight() || y < w.getTop() || y > w.getBottom()) ) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
}
}
return ret;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
}