How to edit text drawn by Canvas.drawText()? - android

I have made an Android app in which the user can write on a bitmap. There is an EditText in which the user types the text and a TextWatcher watches this text. The text is written on the bitmap using Canvas.drawText(). This works properly when text is typed in the EditText.
However, when I try to backspace some of the written text and write some other text, the text is not deleted on the bitmap. Instead, the new text is simply overwritten on the 'deleted' text.
Is there any way to solve this?

You can store the text displayed by Canvas.drawText and display that, and if you want to change that just use a setter to set the new text and then update the Canvas using the following methods:
Setting new text:
public void setText(String newText) {
this.currentText = newText;
}
Updating Canvas:
public void update() {
this.invalidate();
}
Just put both of the method into your view class.

Of course you can redraw your bitmap, and then your new text.
But a simpler way to do it (assuming you have a uniform background) is to first draw your current text in the same position using the background color (so erase everything), and then draw your new text.

Related

Android two TextInputEditText and two way addTextChangedListener

I have two TextInputEditText fields:
TextInputEditText fromData = (TextInputEditText) view.findViewById(R.id.fromData);
TextInputEditText toData = (TextInputEditText) view.findViewById(R.id.toData );
Both fields has addTextChangedListener() and should change text of each other. But that cause an error (recursion). Because when I write some text in fromData, toData text also changes, and if toData text changes fromData also should changes.
How can I make textchangelistener to work in two way ?
Override the listener and if the text it is being changed to is the same as it already is don't change it.
So text is changed in fromData and it calls the on text changed listener in on text changed check if the text being changed is equal to the text that already exists in the textview if it is don't do anything.
onTextChanged(String newText) {
if (!newText.equals(fromData.getText())) {
fromData.setText(newText);
}
}
You can define a global variable like focusedTextView and then add on touch listeners to both textviews. Now when a particular view is touched.
You just set the value of this variable to that view.
Finally, in your addTextChangeListener just check what is the current value of this global variable and just change the text of other textView which is not focused.
Hopefully this will give you the two way result that you want.
Let me know if it changes anything for you.

Customize TokenAutoComplete

I needed a recipient EditText in my app, so, I used this TokenAutoComplete library to do the same. However, I am having a few problem with this.
When I type a name, a drop down list comes. I want to give a custom layout to this drop down list. How can I do it? Currently I am showing just text here using toString method.
Below is the code.
#Override
public String toString() { return mContactFirstName }
I want to show here name and photo together. How can I do it?
Changing the layout for selected token. When the token is not selected/default, the token's background is white and it's text color is black. On the other hand, if the token is selected, the token's background should be black and it's text color should be white colored. I used a selector xml to change the token's background, however, I don't know how to change the text's color.
The selector xml is working perfectly fine and the background is changing when the token is selected and unselected, but, the text's color remains same.
I used below setSelected() method to change the text's color. But it's not working.
#Override
public void setSelected(boolean selected) {
super.setSelected(selected);
TextView tv = (TextView) findViewById(R.id.token_name);
if(selected) {
tv.setTextColor(Color.WHITE);
} else {
tv.setTextColor(Color.BLACK);
}
}
Please help me out.
In the example that TokenAutoComplete library provides you can see that the ContactsCompletionView provides setAdapter method. You can set your custom adapter there, any adapter that extends BaseAdapter will do.
so you just do like this:
myAdapter = new MyCustomAdapter(); // extending BaseAdapter
completionView.setAdapter(myAdapter);
in case of a custom adapter there are plenty of resources on the web, but basically in getView method you create custom view by inflatind it from xml. in the xml you can define the layout you desire (image, text, whatever...).
for more info refer to this question: Custom Adapter for List View

How to get the bounds of a text string (with attributes) in Android?

given a string in resources, how can I get the bounding rectangle for it?
For plain text, I could use Paint.getTextBounds(), but this is a string from resources that has newlines and attribute settings in it.
<string name="foobar"><small>Foo</small>\nBar</string>
In other words, I'm implementing a custom view that will be displaying a string like that, and I want to compute the size of my view.
More detail: Basically, I'm implementing a variant of TextView that adjusts its font size to fit the available space rather than one that adjusts its size to fit the text.
You want to show html in your custom view. Do i make it clear?
If you want to implements this all by your own code. It is a very huge work. Extending TextView and overriding some methods is the best choice, but if you insist on writing your own class, you can still get help from some system class.
Showing and measuing html should follow the code of TextView, you can find this in TextView.onDraw() and TextView.onMeasure(), here i just talk about the steps for measuring html.
Parsing html. Use Html.fromHtml() to get a Spanned text.
Create a StaticLayout or DynamicLayout with the spanned text.
Use Layout.getLineWidth() and Layout.getHeight() to measure the text.
Try this :
String.xml
<string name="foobar"><small>Foo</small>\nBar</string>
In Activity
final TextView mytext = (TextView) findViewById(R.id.my_textView);
mytext.setText(getString(R.string.foobar));
Button btn_getTextBounds = (Button) findViewById(R.id.ok);
btn_getTextBounds.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Rect rectf = new Rect();
mytext.getLocalVisibleRect(rectf);
Log.d("WIDTH :",String.valueOf(rectf.width()));
Log.d("HEIGHT :",String.valueOf(rectf.height()));
Log.d("left :",String.valueOf(rectf.left));
Log.d("right :",String.valueOf(rectf.right));
Log.d("top :",String.valueOf(rectf.top));
Log.d("bottom :",String.valueOf(rectf.bottom));
}
});
It will show required attributes used by the TextView on the Visible Screen Area.
Hope it helps you.
Thanks.

Android Add imagine icon as a "hint" in a textview?

is it possible to add a image icon inside a textviews Hint?
at the moment i can only add text in the hint properties but what i want to do is add a small image icon and a text.
is this possible?
You will need to write your own View which could perhaps be based on a FrameLayout which contains both a TextView (with an empty hint) and an ImageView. Alternatively you could simply use a TextView and when it's empty you set a background image to your "hint icon", and clear the background when text has been entered.
You could user setError to display some text and and an image in the textfield but that would be after the user inputs something and you check it.
Alternatives:
Use setBackground with your hint icon on the background image
Subclass TextView and override the drawing to draw your hint icon
String hint = "<center><img src=\"" + R.drawable.search_hint + "\"/>搜索感兴趣的内容, hello world , I'm form China.</center>";
Html.fromHtml(hint, new ImageGetter()
{
#Override
public Drawable getDrawable(String source)
{
int img = Integer.parseInt(source);
Drawable drawable = getResources().getDrawable(img);
drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
return drawable;
}
}, null);
searchTxt.setHint(hint);

Android: Set text color dynamically for string variable to be appended

I have TextView. Using java code I want to set color for string variable that I have to append to the text. Variable is generated at run time.
I explored Spannable but you have to give start and end which is not fixed.
Any other way to fix this. Please help.
Code:
String text; (Filled at runtime)
//but I want it to be different color
textview.append(text);
You can do something like this to set the text in your TextView:
tv1.setText(Html.fromHtml("<font color='red'>R</font><font color='green'>G</font><font color='blue'>B</font>"));
The issue with this is that when you have just one text object you can only have one colour for it. You will have to use more then one text object (each with different colours) and juxtapose them in you design.

Categories

Resources