Android spinner get text after comma - android

In my android application I am using spinner for selecting data.. and I created string array for strings that to be displayed in spinner. I put all the details in strings folder. I wanted the selected text t be displayed in edit text once the user selected item..
For example : spinner is used to select country codes suppose user selected USA
then the selected text will be like this
United States of America,+001
I don't need t take all the text and display it in edit text. I need only the text after comma, that is +001. So is there any way to get the text after the comma only
Spinner spinner = (Spinner)findViewById(R.id.spinner);
String text = spinner.getSelectedItem().toString();
I know this will display all text I want only text that dislpaying after comma

You can split your text on the comma:
String text = spinner.getSelectedItem().toString();
String[] splited_text = text.split(",");
text = splited_text[1];

Suppose the text is in a string name text. use this:
String[] temp = text.split(",")
String code = temp[1]; //+001 the code after , temp[0] contains the rest

String seperated[] = spinner.getSelectedItem().toString().split(",");
text = seperated[1];
This will return only "+001".

String code = text.substring(text.indexOf(','));

This is how to get string after last comma:
String founded;
Pattern p = Pattern.compile(".*,\\s*(.*)");
String dd = (String) "Your string, really, really2";
Matcher m = p.matcher(dd);
if (m.find()) {
founded = m.group(1); //returns "really2"
}

Related

Html Styling in textview goes wrong Android

I am selecting a part of the TextView and on click of a "highlight" button, I am sending the start and the end index of selection to the database. Then I am loading all the start and end indexes from db and changing the color of text between them.
The problem is after once or twice, the app is changing the color of text that is not in selection.. and the selected part remains unchanged.
MY CODE:
When user selects and presses the highlight button
int i=contentText.getSelectionStart();
int j=contentText.getSelectionEnd();
db.insertHiglightIndex(String.valueOf(i),String.valueOf(j));
setHighlightedText();
The setHighlightedText() method..
String fullText=contentText.getText().toString();
for(int i=0; i<db.getAllStartIndex().size();i++){
String a=fullText.substring(Integer.parseInt(db.getAllStartIndex().get(i)),Integer.parseInt(db.getAllEndIndex().get(i)));
fullText = fullText.replace(a, "<font color='red'>"+a+"</font>");
}
contentText.setText(Html.fromHtml(fullText), TextView.BufferType.SPANNABLE);
MY SCREENSHOTS.
The selection:
The Result:
Clearly the selected area is from "Garrick" to "Bart", and the result is from "entity" to "2012"
I am not able to understand why is this happening. I think there is some problem with this <font color='red'>"+a+"</font> line.
Thank you
It got wrong indexed because There is already added <font color='red'> in the beginning, So that in second time This tag is also counted as a part of string, So I suggest creating a new temporary String, assign same text to the String but after replacing the previous font tag it held. Use this syntax to remove previous font tag from originalString
String tempString = originalString.replaceAll("[<](/)?font[^>]*[>]", "");
After that work with only tempString. That means again add every previous font tag you have to tempString and set that text.
In next time again do the same first remove all font tag and again add all of them back in tempString as well as current selection using same loop you are using currently.
You have wrong indexes because you are modifying the fullText content within the loop.
Taking a look at this example you can figure it:
final TextView tv = (TextView) findViewById(R.id.textView);
tv.setText( "abcdefghijklmnopqrstuvwxyz0123456789");
String fullText= tv.getText().toString();
// your first iteration
String a = fullText.substring(1,3);
// a contains "ab"
fullText = fullText.replace(a, "<font color='red'>"+a+"</font>");
After the first iteration full text contains now
a<font color='red'>bc</font>defghijklmnopqrstuvwxyz0123456789"
Then the substring() in the second iteration won't returns the substring base on your initial content.
If you want to be able to have multiple substrings colored in red you can try this:
String fullText = contentText.getText().toString();
StringBuilder result = new StringBuilder();
for(int i=0; i < db.getAllStartIndex().size(); i++){
fullText = applyFont(result, fullText, Integer.parseInt(db.getAllStartIndex().get(i)), Integer.parseInt(db.getAllEndIndex().get(i)));
}
// Add here the remaining content
result.append(fullText);
contentText.setText(Html.fromHtml(result.toString()), TextView.BufferType.SPANNABLE);
private String applyFont(StringBuilder result, String source, int from, int to){
result.append(source.substring(0, from));
result.append("<font color='red'>");
result.append(source.substring(from, to));
result.append("</font>");
return source.substring(to, source.length());
}

How to Show data from String Array ,in Single Android textview with cross image Each line contains Two Strings

1.I am getting data from server storing inside string Array.
2.then i want to show in Android TextView ,each TextView line should Contain 2 strings(Like Skill Sets) from String array.
3.then need to add cross Image in right side of the Textview.
please help me, how to achieve this.
String mystring = "This is my first sentence";
String arr[] = mystring.split(" ", 3);
String firstWord = arr[0]; //This
String secondWord = arr[1]; //is
String theRest = arr[2]; //my first sentence
yourtextview.setText(firstWord +" "+ secondWord);
and regarding for Image in right side of the Textview put this in ur txtview'sxml.
android:drawableRight="#drawable/image"

adding some elements to edittext i.e some character between any word

I need to receive user's input from an edittext, then add some elements inside it?
how can I do it programmatically?
let me explain:
user enters this phrase : ( Hello World )
I want my app change it to : ( Hoelplom wwoerlldc) for example
Any suggestion?
get the text from your edittext
String userInput = myEditText.getText().toString();
and then modify it as you want
To 'Modify' the String you can do it in many ways
You can use substrings , replace() , ... etc
also you can convert it to char array and modify it , then build your string again
String userInput = myEditText.getText().toString();
char[] myArray = userInput.toCharArray();
// your modification(s) here
userInput = new String(myArray);

Is it possible to change text color for one database item when it is printed from a cursor?

I have a database that I use a cursor to display. What I am doing right now is just appending the results to a string and displaying. What I am trying to do is change the text color of the title of each database result based on the type of item it is but I don't think that is possible when appending text using a Stringbuilder.
Is there something else I could use to make this possible or have I overlooked something using Stringbuilder that would allow me to do this?
Here is an example of the output I am trying to get:
Item Name 1 (blue)
Item Description (white) Rating (white)
Model (white)
Item Name 2 (red)
Item Description (white) Rating (white)
Model (white)
Item Name 3 (green)
Item Description (white) Rating (white)
Model (white)
Item Name 4 (green)
Item Description (white) Rating (white)
Model (white)
And so on...
The color of the Item Name field would depend on the item itself. Thanks for any help or point in the right direction.
Edit 1
Here is a shortened example (with some of the database columns taken out) of how I am displaying the results:
public void showItems(Cursor cursor) {
StringBuilder ret = new StringBuilder();
while (cursor.moveToNext()) {
String name = cursor.getString(1);
String model = cursor.getString(2);
ret.append(name + "\n" + model + "\n);
}
sResults.setText(ret);
}
It's from a tutorial I found online and works great except for the whole changing certain parts of the text to a different color.
Edit 2
Based on the below suggestion I have tried this:
public void showItems(Cursor cursor) {
StringBuilder ret = new StringBuilder();
while (cursor.moveToNext()) {
String name = cursor.getString(1);
String model = cursor.getString(2);
Spanned styleText = (Html.fromHtml("<b><style='color:red;'>" + name + "</style></b><br />" + model + "<br />"));
ret.append(styleText);
}
sResults.setText(ret);
}
I get the same results as my original code without any color. Any other suggestions?
Use fromHtml: this will allow you to style your string.
String styleText = "This is <style='color:red;'>red</style>.";
textView.setText(Html.fromHtml(styleText), TextView.BufferType.SPANNABLE);

Removing space from Edit Text String

In my android app, I am getting the String from an Edit Text and using it as a parameter to call a web service and fetch JSON data.
Now, the method I use for getting the String value from Edit Text is like this :
final EditText edittext = (EditText) findViewById(R.id.search);
String k = edittext.getText().toString();
Now normally it works fine, but if we the text in Edit Text contains space then my app crashes.
for eg. - if someone types "food" in the Edit Text Box, then it's OK
but if somebody types "Indian food" it crashes.
How to remove spaces and get just the String ?
Isn't that just Java?
String k = edittext.getText().toString().replace(" ", "");
try this...
final EditText edittext = (EditText) findViewById(R.id.search);
String k = edittext.getText().toString();
String newData = k.replaceAll(" ", "%20");
and use "newData"
String email=recEmail.getText().toString().trim();
String password=recPassword.getText().toString().trim();
In the future, I highly recommend checking the Java String methods in the API. It's a lifeline to getting the most out of your Java environment.
You can easily remove all white spaces using something like this. But you'll face another serious problem if you just do that. For example if you have input
String input1 = "aa bb cc"; // output aabbcc
String input2 = "a abbcc"; // output aabbcc
String input3 = "aabb cc"; // output aabbcc
One solution will be to fix your application to accept white spaces in input string or use some other literal to replace the white spaces. If you are using only alphanumeric values you do something like this
String input1 = "aa bb cc"; // aa_bb_cc
String input2 = "a abbcc"; //a_abbcc
String input3 = "aabb cc"; //aabb_cc
And after all if you are don' caring about the loose of information you can use any approach you want.

Categories

Resources