Highlight All Words that is searched via EditText - android

Hello I want to know how to highlight all Words that is inputted in the EditText and will appear in the TextView this post is related to this one Highlight Textview Using EditText

Say et is your EditText and tv is TextView object. Use the following code:
public class MotivationalQuotesActivity extends Activity {
/** Called when the activity is first created. */
Button next;
EditText et;
TextView tv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et = (EditText) findViewById(R.id.et);
tv = (TextView) findViewById(R.id.tv);
tv.setText("The name of our country is Bangladesh. Bangladesh is a land of rivers.");
next = (Button) findViewById(R.id.button1);
next.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
tv.setText("The name of our country is Bangladesh. Bangladesh is a land of rivers.");
String ett =et.getText().toString();
String tvt =tv.getText().toString();
int ofe = tvt.indexOf(ett,0);
Spannable WordtoSpan = new SpannableString( tv.getText() );
for(int ofs=0;ofs<tvt.length() && ofe!=-1;ofs=ofe+1)
{
ofe = tvt.indexOf(ett,ofs);
if(ofe == -1)
break;
else
{
WordtoSpan.setSpan(new BackgroundColorSpan(0xFFFFFF00), ofe, ofe+ett.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(WordtoSpan, TextView.BufferType.SPANNABLE);
}
}
}
});
}
}
The result is:

One easy and quick way of highlighting a text, is to use string replace method. Replace the desire string with font tag
Spanned strHTML= Html.fromHtml("<center>"+full_string.replaceAll(strSrch,"<font color='yellow'>"+strSrch+"</font>")+"</center><br/>");
TextView tv=(TextView) v.findViewById(R.id.txtPager);
tv.setText(strHTML,TextView.BufferType.SPANNABLE);

Related

In app keyboard "Numbers" with 2 TextView

Button buttonClear;
private TextView inputValue;
private static final String DIGITS = "0123456789.";
private Boolean userIsInTheMiddleOfTypingANumber = false;
here is some animation
TextView image1;
TextView image2;
TextView image3;
TextView image4;
TextView image5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
inputValue = (TextView) findViewById(R.id.ePrice);
findViewById(R.id.button0).setOnClickListener(this);
findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
findViewById(R.id.button3).setOnClickListener(this);
findViewById(R.id.button4).setOnClickListener(this);
findViewById(R.id.button5).setOnClickListener(this);
findViewById(R.id.button6).setOnClickListener(this);
findViewById(R.id.button7).setOnClickListener(this);
findViewById(R.id.button8).setOnClickListener(this);
findViewById(R.id.button9).setOnClickListener(this);
findViewById(R.id.buttonDecimalPoint).setOnClickListener(this);
Button buttonClear = (Button) findViewById(R.id.buttonClear);
/** Backspace for in app keyboard */
buttonClear.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
inputValue.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_DEL));
}
});
/** Backspace */
/** Disable Key API level 11 where suppose to disable the android keybaord i also tried to change the manifest but the keyboard still coming up */
inputValue.setInputType(InputType.TYPE_NULL);
if (android.os.Build.VERSION.SDK_INT >= 11 ) {
inputValue.setRawInputType(InputType.TYPE_CLASS_TEXT);
inputValue.setTextIsSelectable(true);
}
image1 = (TextView)findViewById(R.id.tPercent);
image2 = (TextView)findViewById(R.id.textPercent);
image3 = (TextView)findViewById(R.id.Percent_10);
image4 = (TextView)findViewById(R.id.Percent_pay);
image5 = (TextView)findViewById(R.id.ePrice);
Animation animationFadeIn1 = AnimationUtils.loadAnimation(this, R.anim.fadein_1);
image1.startAnimation(animationFadeIn1);
Animation animationFadeIn2 = AnimationUtils.loadAnimation(this, R.anim.fadein_2);
image2.startAnimation(animationFadeIn2);
Animation animationFadeIn3 = AnimationUtils.loadAnimation(this, R.anim.fadein_3);
image3.startAnimation(animationFadeIn3);
Animation animationFadeIn4 = AnimationUtils.loadAnimation(this, R.anim.fadein_4);
image4.startAnimation(animationFadeIn4);
Animation animationFadeIn5 = AnimationUtils.loadAnimation(this, R.anim.shake);
image5.startAnimation(animationFadeIn5);
// here is first EditText which i can type normally which in app keyboard
final EditText editPrice = (EditText) findViewById(R.id.ePercents);
// here is second EditText where i can't type anything with in app keyboard
final EditText ePercent = (EditText) findViewById(R.id.ePrice);
// to get the 1 result
final TextView result = (TextView) findViewById(R.id.viewResult);
// to get the 2 result
final TextView result2 = (TextView) findViewById(R.id.viewResultPay);
Button buttonConvert = (Button) findViewById(R.id.buttonConvert);
buttonConvert.setOnClickListener(new OnClickListener() {
/**Here is some calculation happening **/
}
/**here is the onclick for buttons for in app keyboard**/
public void onClick(View v) {
/** Numbers */
String buttonPressed = ((Button) v).getText().toString();
if (DIGITS.contains(buttonPressed)) {
// digit was pressed
if (userIsInTheMiddleOfTypingANumber) {
if (buttonPressed.equals(".")
&& inputValue.getText().toString().contains(".")) {
} else {
inputValue.append(buttonPressed);
}
} else {
if (buttonPressed.equals(".")) {
inputValue.setText(0 + buttonPressed);
} else {
inputValue.setText(buttonPressed);
}
userIsInTheMiddleOfTypingANumber = true;
}
}
/** Numbers */
}
What i want is:
To disable the android keyboard.
I need to type in EditText with my made number buttons in each EditText's.. there is 2 EditText.
I tried to duplicate the onclick for digits but its writing in the same time in both EditText's
Appreciate any help.
1/ For disabling android keyboard, make a class extending EditText, and apply it to your edittexts
editPrice.setInputType(InputType.TYPE_NULL);
2/ For adding number to your editText
// On click on a digit button
editPrice.setText(editPrice.getText().toString().concat(String.valueOf(yourNumber)));
// For removing last char (in case of a delete button)
editPrice.setText(editPrice.getText().toString().substring(0, editPrice.getText().toString().length()-1));

Infinite loop when button is clicked on Android

What I'm trying to do is:
If the EditText input is equal to the random number generated, then stop the loop otherwise keep on the loop and reset input text.
For some reason, I'm getting an infinite loop. I am new to programming, any help is really appreciated.
Here is the code:
public class Main extends Activity implements OnClickListener{
private TextView tvResult;
private TextView tvRandTest;
private EditText et1;
private String randonNumber;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tvResult = (TextView) findViewById(R.id.textView4);
tvRandTest = (TextView) findViewById(R.id.textView3);
et1 = (EditText) findViewById(R.id.editText1);
}//End Main
public void myClickHandler(View view)
{
if(view.getId() == R.id.button1)
{
//Generates 6 one digit Random Numbers
int randonNumber1 = (int) (0 + Math.random() * 9);
//Parse Numbers
String rd1 = Integer.toString(randonNumber1);
randonNumber = rd1;
boolean done = false;
do
{
et1.getText().toString();
if(et1.equals(randonNumber))
{
Toast.makeText(Main.this,"Equal Number", Toast.LENGTH_SHORT).show();
tvResult.setText(randonNumber);
done = true;
}//end if
else
{
Toast.makeText(Main.this,"Not Equal Number", Toast.LENGTH_SHORT).show();
et1.setText("");
}//end else
}//End While
while(!done);
}//End if
if(view.getId() == R.id.button2)
{
tvRandTest.setText(randonNumber);
}
}//End Method
#Override
public void onClick(View arg0) {
// TODO
}
}//End Class
if(et1.equals(randonNumber))
I will change it with
if(et1.equals(String.valueOf(randonNumber)))
you put directly the int value inside the equals method two things will happen:
the autboxing will create an Integer object starting from the int value
the toString() method will be called through this object.
the toString() method of Integer in android, as the doc stands:
Returns a string containing a concise, human-readable description of this object.
So you are compareing the address of the new object with the content of et1 and not with its real value. Here the reference
In this line et1.getText().toString(); you need to assign result to variable, for example String input = et1.getText().toString(); Then in next line you need to compare two strings, if(input.equals(randonNumber)) But your program can hang cause of infinity loop on UI thread. You should use TextWatcher
to handle when text in EditText was changed

Delete or backspace function. I created a button but cannot do a delete/backspace in my edittext

I want to delete the characters in my edittext one by one. I've research quite a bit but there are some problems, please advise. this is my sample code.
I created a delete button "ImageButton buttonDelete;"// XML imageButton1
and my edittext is "EditText display;"
display = (EditText) findViewById(R.id.editText1);
buttonDelete.setOnClickListener(new View.OnClickListener()
{
public void onClick()
{
// Get edit text characters
String textInBox = display.getText():
//Remove last character//
String newText = textInBox.substring(0, textInBox.length()-1);
// Update edit text
display.setText(newText);
Try this:
// Get edit text characters
String textInBox = display.getText().toString();
if(textInBox.length() > 0)
{
//Remove last character//
String newText = textInBox.substring(0, textInBox.length()-1);
// Update edit text
display.setText(newText);
}
BtonBackSpace.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String bs = null;
if(Tfield.getText().length()>0){
StringBuilder Strb = new StringBuilder(Tfield.getText());
Strb.deleteCharAt(Tfield.getText().length() - 1);
bs = Strb.toString();
Tfield1.setText(bs );
}

Highlight Textview Using EditText

Im currently making a search engine like application for android and i want to highlight the searched word from edittext to textview... this is that i got so far and it only highlights the first word in the textview
TV.setText("Hello World", TextView.BufferType.SPANNABLE);
Spannable WordtoSpan = (Spannable) TV.getText();
WordtoSpan.setSpan(new BackgroundColorSpan(0xFFFFFF00), 0, notes.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
TV.setText(WordtoSpan);
I think you want to highlight a specific word of TextView which user types in a EditText.
Say et is your EditText and tv is TextView object. Use the following code:
String ett =et.getText().toString();
String tvt =tv.getText().toString();
int index = tvt.indexOf(ett);
Spannable WordtoSpan = new SpannableString( tv.getText() );
if(index != -1)
{
WordtoSpan.setSpan(new BackgroundColorSpan(0xFFFFFF00), index, index+ett.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(WordtoSpan, TextView.BufferType.SPANNABLE);
}
else
tv.setText("The name of our country is Bangladesh");
Here is the outcome:
Here is the complete code:
public class MotivationalQuotesActivity extends Activity {
/** Called when the activity is first created. */
Button next;
EditText et;
TextView tv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et = (EditText) findViewById(R.id.et);
tv = (TextView) findViewById(R.id.tv);
tv.setText("The name of our country is Bangladesh");
next = (Button) findViewById(R.id.button1);
next.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String ett =et.getText().toString();
String tvt =tv.getText().toString();
int index = tvt.indexOf(ett);
Spannable WordtoSpan = new SpannableString( tv.getText() );
if(index != -1)
{
WordtoSpan.setSpan(new BackgroundColorSpan(0xFFFFFF00), index, index+ett.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(WordtoSpan, TextView.BufferType.SPANNABLE);
}
else
tv.setText("The name of our country is Bangladesh");
}
});
}
}
it could help
TextView tv = (TextView) findViewById(R.id.hello);
SpannableString s = new SpannableString(getResources().getString(R.string.linkify));
Pattern p = Pattern.compile("abc");
Matcher m = p.matcher(s);
while (m.find()) {
int start = m.start();
int end = m.end();
s.setSpan(new ForegroundColorSpan(Color.RED), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
tv.setText(s);

Button click problem

I'm a beginner in application development. My problem is, that when I run my app and I click on the Calculate button, the program stops. The code:
public class screen1 extends Activity {
private EditText name;
private CheckBox box1;
private Spinner spinner;
private TextView text1, text2, text3, text4, text5, text6;
private Button calcbutton, closebutton;
String strength;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner hubSpinner = (Spinner) findViewById(R.id.myspinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( this, R.array.military_ranks , android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
hubSpinner.setAdapter(adapter);
name = (EditText)findViewById(R.id.editText1);
strength = name.getText().toString();
box1 = (CheckBox)findViewById(R.id.checkBox1);
text1 = (TextView)findViewById(R.id.textView4);
text2 = (TextView)findViewById(R.id.textView6);
text3 = (TextView)findViewById(R.id.textView8);
text4 = (TextView)findViewById(R.id.textView10);
text5 = (TextView)findViewById(R.id.textView12);
text6 = (TextView)findViewById(R.id.textView14);
final Button calcbutton = (Button) findViewById(R.id.button1);
calcbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int str = Integer.valueOf(strength);
int rank = spinner.getSelectedItemPosition()+1;
double sebzes;
if(box1.isChecked()){
sebzes = (((rank-1)/20+0.3)*((str/10)+40))*1*(1+1/100);
text1.setText(Double.toString(sebzes));
}
else{
sebzes = (((rank-1)/20+0.3)*((str/10)+40))*1;
text1.setText(Double.toString(sebzes));
}
}
});
final Button closebutton = (Button) findViewById(R.id.button2);
closebutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
}
In the edittext component you should be able to write numbers only. I don't know why it's not working.
The problem are these two lines:
int str = Integer.valueOf(strength);
int rank = spinner.getSelectedItemPosition()+1;
The first won't fail if you use only numbers in your EditText but it would be better to ensure that or at least catch the exception that is thrown when you try to convert a character to a numberical value. Additionally you could also use Integer.valueOf(strength).intValue(); even so it is normally not really necessary.
The real problem is the second line. You declared the variable spinner but you never instantiate it. That's why you will get a NullPointerException there.
On an unrelated note: You also should start your class name with a capital letter to follow the Java naming conventions.
You're not instantiating spinner anywhere, but you're referencing it in the second line of your button click method. Probably a null reference problem.

Categories

Resources