I am making a Mad Libs game of sorts for practice, and so I will have things like EditTexts so the user can input data.
However right now the only way I can get this to sort of work is if I use a horizontal LinearLayout and then do something like a TextView followed by an EditText followed by another TextView, but then if the user enters something long in the EditText, the text does not "wrap nicely" but rather it squishes the rightmost TextView. This is to be expected, but it is not my end-desired behavior.
Is there a better way to put EditTexts inside of TextViews?
Just have one TextView and then you can use a ClickableSpanand even add multiple ClickableSpan(s) to a given TextView. This gives you the effect of multi-line TextView with certain spots acting like an EditText.
If an object of this type is attached to the text of a TextView with a
movement method of LinkMovementMethod, the affected spans of text can
be selected. If clicked, the onClick(View) method will be called.
SpannableString madLibString = new SpannableString("Our school cafeteria has really adjective food");
ClickableSpan clickableSpan = new ClickableSpan() {
#Override
public void onClick(View textView) {
Toast.makeText(getApplicationContext(), "The text 'adjective' has been clicked. Do something.",
Toast.LENGTH_SHORT).show();
}
#Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
// NOTE: set styling here to give UI feedback that this is text that can be clicked and edited.
ds.setColor(getResources().getColor(android.R.color.holo_red_dark));
}
};
madLibString.setSpan(clickableSpan, 32, 41, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(madLibString);
textView.setMovementMethod(LinkMovementMethod.getInstance());
Related
I need to add a country code to phone numbers to dial it from Android, but I would like to display the phone numbers without the country code. I can get the dialler working using setText() for the phone number (with country code) in Java and adding Autolink in the XML.
However I can't seem to find a way of displaying the phone number without the country code i.e. when using <a href= you can have different text to the actual hyperlink.
So I want to display 01234 567890, but dial +441234567890.
I've tried using this HTML in a string but it doesn't work:
01234 567890
You can remove first three characters of your string line:
numberString.substring(3);
and it will return you:
01234 567890
Hope it will help you :)
Use a ClickableSpan:
final SpannableString span = new SpannableString("01234 567890");
final ClickableSpan onClick = new ClickableSpan() {
#Override
public void onClick(View textView) {
//Launch your Intent to dial the number, with whatever number you like
}
#Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
}
};
span.setSpan(onClick, 0, span.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
final TextView tv = (TextView) findViewById(R.id.text_view);
tv.setText(span);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setHighlightColor(Color.GREEN);
You may need to add clickable:true in your XML as well.
Or, if your TextView just contains the number, you can set a click listener on the entire TextView and launch your dial from there
Is it somehow possible to achieve that?
In example: we have listView with 20 items, every item has some text. User want to select half of ending text from item 1. and the half of another item text (same behaviour like in webView or selectable textView). Did someone think about that feature? Where should I search the solution?
This topic will be updated when solution will be found.
ps. I know you will say "show us code first". I do not have it yet.
In your istview on item click listener code like this
lv.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3)
{
// TODO Auto-generated method stub
TextView tv;
String text_from_tv, finalText;
tv= (TextView) view.findViewById(R.id.textview_id);
text_from_tv = tv.getText();
if(!firstPartSelected)
finalText += text_from_tv.substring(0,text_from_tv.length()/2);
else
finalText += text_from_tv.substring(text_from_tv.length()/2,text_from_tv.length());
//Save this finalText String to any string array and use those values
}
});
and that string array contains the second halfs of the selected word, if it helpful up vote the answer!!
In your list_item.xml for your list view. You will want to set,android:textIsSelectable="true" and make android:clickable="true" for that same item.
I won't give any code, just how I would do it :
boolean firstPartSelected false;
String finalText ="";
// ListView Definition
// OnItemClickListener
OnItemClick(...){
if(!firstPartSelected)
finalText += TextFromItem.substring(0,TextFromItem.length()/2)
else
finalText += TextFromItem.substring(TextFromItem.length()/2,TextFromItem.length())
}
This is not some real code, just an idea of how to implement it. Is that what you wanted ?
Try handle OnFocusChangeListener for your EditText, when focus will be change, color selected text in EditText using spans (Android: Coloring part of a string using TextView.setText()?). If you have large count of EditText you can use view.setTag(etNumber) and (Integer)view.getTag() for each EditText and use this information while concat output string in loop (look for more info Android - get children inside a View?)
P.S. EditText is inheritor of TextView, what yo can do with TextView you will can do with EditText
How to change the color of a paragraph word by word with timer. like at first second i want to change color of The in the below paragraph, after 5th second change color of text, after 8th second change color of will be and so on....
The text will be wrapped in tags, and displayed in a monospaced font.
just use Timer and change the font color of your edit text accordingly and stop timer in focus lost.
i think you can do something like this :
Split the paragraph to words by using the method :
split(String separator);// it will return an array of Strings
//in your case you will do somthing like this
myWords = paragraph.split(" ");// the separator is the space
And then , you can use the method to colorate what ever you want of those words by using the method :
myNewParagraph.append(HTML.fromHtml("<font color...>... Your HTML Code to colorate your Text"));
and when you finish coloring each word , you update your textView to display the new text colored
Hope it helps
You can use spans to control the appearance of text. Take a look at Spannable and CharacterStyle.
Here is an example. Of course you would need to put this in some sort of timer.
Spannable spannableText = getSpannableText(yourTextView);
spannableText.setSpan(new TextAppearanceSpan(...), wordStart, wordEnd)
yourTextView.setText(spannableText);
private Spannable getSpannableText(TextView tv) {
CharSequence cs = tv.getText();
if (cs instanceof Spannable) {
return (Spannable)cs;
} else {
return SpannableString.valueOf(cs);
}
}
So, today I decided to try out Android, so please understand that I am a beginner in it.
What I want to achieve right now is to have a EditText, and a set of buttons to be used to enter data into the EditText.
What I've done currently is stick a set of button widgets in the XML layout, and I use this code to make the buttons insert stuff into the EditText:
final EditText inputline = (EditText) findViewById(R.id.textentry);
final Button my_button = (Button) findViewById(R.id.my_btn);
my_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
inputline.append("a");
}
});
This kind of works, but I need help with a few issues:
it always appends the character at the end of the string, not at the current cursor position
similarly, when I call inputline.selectAll() and press my button, it inserts the text at the end of the string again; whereas I want it to delete the text first (as it's selected) and then insert the character
it seems tedious to write all that code for each of the buttons I have. Is there a better way to do this altogether?
Thanks for your help!
I have now pretty much solved by replacing inputline.append("a"); etc. with my custom function, lineInsert(), which you can see below.
public void lineInsert(CharSequence text) {
final EditText inputline = (EditText) findViewById(R.id.textentry);
int start = inputline.getSelectionStart();
int end = inputline.getSelectionEnd();
inputline.getText().replace(Math.min(start,end), Math.max(start,end), text, 0, text.length());
inputline.setSelection(inputline.getSelectionEnd());
}
This has the same behavior as the soft keyboard.
Would really appreciate some help with this.
I have an app which consists of a gallery, text and icons. What i want to do is similar to the android market (example can be seen below). In the android market a whole area is hyperlinked rather than just the text. Hold your finger on one of the apps and it will light up green. That whole area is a hyperlink. Like below:
Looking to get something like this: http://img269.imageshack.us/img269/691/09droid3295x440.jpg
Is there anyway i can do that to my app? I have a very similar app, the gallery is right at the top with icons down the left and text besides each icon. I'm a bit of a newbie so i'm using nested linearlayouts with textviews and imageviews in each one.
Is there a way that it can be done? Thanks for any help in advance!
As i misunderstood the question i'll give the correct answer here. If i understand right, you want to have an clickable View (in this case a LinearLayout) :
LinearLayout layout = (LinearLayout)findViewById(R.id.layoutId);
layout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
Or any view for that matter (could be a Button, TextView, etc).
you could do the following to achieve this:
Spannable spans = (Spannable) text;
ClickableSpan clickSpan = new ClickableSpan() {
#Override
public void onClick(View widget) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://"
+ url));
}
public void updateDrawState(TextPaint ds) {
// override link-centric text appearance
}
};
int index = (text.toString()).indexOf(url);
spans.setSpan(clickSpan, index, url.length() + index, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Where text is your CharSequence.
Link not working
To answer ur query, create the link on the layout rather than text