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

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);

Related

After updating background drawable from host, set a contrast color for text

Let me make this clear: I create a fragment which is used for playing music. I blur the artwork of album and set it as the background of the fragment. And I need to show the music name and artist name on the fragment. Sometimes the updated artwork background has the pretty close color with text (like both them are black). In this case, I want to change the text color to another contrast color to make the text clear. The ios has the similar function when you set the wall paper.
Does anyone know how to achieve that?
Use palette: https://guides.codepath.com/android/Dynamic-Color-using-Palettes
Palette.from(bitmap).maximumColorCount(16).generate(new Palette.PaletteAsyncListener() {
#Override
public void onGenerated(Palette palette) {
// Get the "vibrant" color swatch based on the bitmap
Palette.Swatch vibrant = palette.getVibrantSwatch();
if (vibrant != null) {
// Set the background color of a layout based on the vibrant color
containerView.setBackgroundColor(vibrant.getRgb());
// Update the title TextView with the proper text color
titleView.setTextColor(vibrant.getTitleTextColor());
}
}
});
This is an example of using it. Mess around with grabbing different swatches to see what works best.
If u need black or white theme for text please go through Determine font color based on background color
or otherwise use palette for vibrent colours
https://guides.codepath.com/android/Dynamic-Color-using-Palettes

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

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.

Textview does not change textcolor

I have an problem setting textcolor on some part of a Textview.
The text is defined to have the color white in the layout-xml.
When the user chooses the write answer in the game this method call is triggered:
this.Question = (TextView) findViewById(R.id.layout1Question);
this.Question.setText(Html.fromHtml("<font color = 'green'>CORRECT: </font>") + this.CurrentQuestion.getFillin());
Here I want the CORRECT -part to show green text while the rest should be white as default.
But the whole text is shown in white. What am I doing wrong?
Thanks for any help!
EDIT: It works if I remove the second part, hence:
this.Question.setText(Html.fromHtml("<font color = 'green'>CORRECT: </font>"));
works okay.
Try moving the geFillin() call to fromHtml parameter. May be the string concatenation is casting the Spanned string back to simple string.
this.Question.setText(
Html.fromHtml("<font color='green'>CORRECT: </font>" + this.CurrentQuestion.getFillin()));

Multiple text lines on a button. Each line has different text styles

Hi i'm trying to create a button with multiple lines of text. See below example. Each line of text has a different text size and colour applied to it. The button also needs to be able to send the user to the next page. Is this possible in XML? I've looked everywhere and can't find a solution. Thanks.
BUTTON HEADING TEXT(large green font)
1st button text (small grey font)
2st button text (small white font)
See example:
Picture is not accessible. but according to your problem, you can implement a LinearLayout as main componenet and set its property clickable and focusable, and set its background to android.R.drawable.btn_default. Set orientation of this layout to Vetical.
Add as many TextViews of any style in this linearlayout in xml or dynamically as you want. But set all the TextViews focusable and Clickable property to false.
Html.fromHtml(string) can interpret some of the hmtl tags and you can hence use html to style the text of your buttons.
Assuming that you have defined your buttons in a XML file you can then set the text of the buttons like this in the Java code.
Button button = (Button) findViewById(R.id.button1);
String styledText = "<big> <font color='#008000'>"
+ "My orders" + "</font> </big>" + "<br />"
+ "<small>" + "You have no current orders" + "</small>";
button.setText(Html.fromHtml(styledText));
// Attach a listener to the button that will make something
// happen when the button is clicked
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// Put your Intent code here
Log.d("onclick", "Button click registered");
}
});
You can stack <big> and <small> HTML tags to multiply their effect:
private final String btnInnerHTML = "<big><big><big><font color='#000000'><b>%s</b></font></big></big></big><br/><small>%s</small>";
btn0.setText(Html.fromHtml(String.format(btnInnerHTML, "Line1", "Line2")));

Is there a way to set the text in the text view to invisible?

I can't change its color or use alpha because the background may change and I won't know its color!
Is there any other way to set it to invisible ?
Also I don't want to set the text view invisible since I want the background to be visible!
Try:
textView.setTextColor(getResources().getColor(android.R.color.transparent));
You can set a textview to INVISIBLE:
myTextView.setVisibility(View.INVISIBLE);
That way the textview will still be present but not seen
Something like this..
String text = "Invisible Text";
TextView text1 = new TextView();
text1.setTag(text);
why not, set text value as empty string when you want to make it invisible. keep backup of the original value so that you can use it later when required.

Categories

Resources